Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(dataflow): count a closure's reads when inferring Extract Method params and returns (#2953)
Raghav Chamadiya4d3fca3c7487
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.pyindex 5072283b7..c2f5a69a9 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py@@ -59,12 +59,16 @@ class FunctionDefUse:``blocks`` is keyed by block id; ``definitions`` is every write site orderedby ``index`` (so ``definitions[i].index == i``); ``params`` is the parameter- occurrences seeded at the entry block.+ occurrences seeded at the entry block. ``captured`` holds the reads made+ inside nested closures, kept apart from the per-block uses because they+ run when the closure is called, not where it is written; code that moves+ statements (the Extract Method slicer) still has to count them."""blocks: dict[int, BlockDefUse]definitions: list[Definition]params: tuple[Occurrence, ...]+ captured: tuple[Occurrence, ...] = ()def block(self, block_id: int) -> BlockDefUse | None:return self.blocks.get(block_id)@@ -118,4 +122,8 @@ def compute_def_use(_add_def(occ, block.id)bdu.uses.extend(sdu.uses)- return FunctionDefUse(blocks=blocks, definitions=definitions, params=params)+ captured: list[Occurrence] = []+ dialect.collect_captured_reads(fn_node.child_by_field_name("body"), captured)+ return FunctionDefUse(+ blocks=blocks, definitions=definitions, params=params, captured=tuple(captured)+ )diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.pyindex 524433f4c..2b82af357 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py@@ -172,6 +172,69 @@ class BaseDefUseDialect:for child in node.named_children:self.collect_reads(child, out)+ def collect_captured_reads(self, node: Node | None, out: list[Occurrence]) -> None:+ """Append the reads made inside nested scopes under *node* to *out*.++ :meth:`collect_reads` stops at a nested function or lambda because its+ reads are not the statement's own. A closure still reads the enclosing+ function's variables, though, so code that moves one of them has to+ see those reads: a span whose closure reads a local needs it passed in,+ and a span defining a local a later closure reads has to return it.+ Left out: names the nested scope binds as its own parameters, its own+ name (a nested ``def``), and locals it writes on a line before it first+ reads them (``const t2 = v * 2``), which are its own variables. A local+ written and read on one line is kept, which can only add a parameter or+ a return, never drop one.+ """+ if node is None:+ return+ if not self._is_scope_boundary(node):+ for child in node.named_children:+ self.collect_captured_reads(child, out)+ return+ writes, reads = self._closure_def_use(node)+ bound = self._closure_bound_names(node) | _written_before_read(writes, reads)+ out.extend(occ for occ in reads if occ.name not in bound)++ def _closure_def_use(self, node: Node) -> tuple[list[Occurrence], list[Occurrence]]:+ """Writes and reads inside nested scope *node*, deeper closures included.++ The body goes through the dialect's own walk, which tells a declaration+ from a read; a scope with no ``body`` field falls back to plain reads.+ """+ writes: list[Occurrence] = []+ reads: list[Occurrence] = []+ body = node.child_by_field_name("body")+ process = getattr(self, "_process", None)+ if body is not None and process is not None:+ process(body, writes, reads)+ self.collect_captured_reads(body, reads)+ return writes, reads+ name = node.child_by_field_name("name")+ for child in node.named_children:+ if name is None or child.id != name.id:+ self.collect_reads(child, reads)+ self.collect_captured_reads(child, reads)+ return writes, reads++ def _closure_bound_names(self, node: Node) -> set[str]:+ """Names a nested scope binds as its own parameters.++ A closure's parameter list is often shaped unlike a function's+ (``x => ...``, ``|x| ...``), so the identifiers under it are read+ directly on top of what :meth:`parameter_defs` finds.+ """+ bound = {occ.name for occ in self.parameter_defs(node)}+ stack = [node.child_by_field_name("parameters"), node.child_by_field_name("parameter")]+ while stack:+ cur = stack.pop()+ if cur is None:+ continue+ if cur.type in self.identifier_kinds and cur.text:+ bound.add(cur.text.decode("utf-8", "replace"))+ stack.extend(cur.named_children)+ return bound+def _is_scope_boundary(self, node: Node) -> bool:"""True if *node* opens a nested scope whose reads are not thisstatement's (a nested function / lambda). Default: never. Subclasses@@ -222,6 +285,14 @@ class BaseDefUseDialect:raise NotImplementedError+def _written_before_read(writes: list[Occurrence], reads: list[Occurrence]) -> set[str]:+ """Names whose first write comes on an earlier line than their first read."""+ first_read: dict[str, int] = {}+ for occ in reads:+ first_read[occ.name] = min(occ.line, first_read.get(occ.name, occ.line))+ return {w.name for w in writes if w.line < first_read.get(w.name, w.line + 1)}++# The registry, populated by ``dialects/__init__.py`` from each language module.# Keyed by ``LanguageTag``; a missing key => the def/use pass is silent for that# language (no dialect = no signal).diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.pyindex 752684e1c..eb25045e3 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py@@ -236,7 +236,8 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,"""Per-variable sorted def lines and use lines from D2's facts.Parameter definitions are included (seeded at the signature line), so a- parameter naturally counts as "defined before" any body span.+ parameter naturally counts as "defined before" any body span. Reads inside+ nested closures (``def_use.captured``) count as uses at their own line."""def_lines: dict[str, list[int]] = defaultdict(list)use_lines: dict[str, list[int]] = defaultdict(list)@@ -245,6 +246,11 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,for bdu in def_use.blocks.values():for u in bdu.uses:use_lines[u.name].append(u.line)+ # A closure's read counts where the closure is written: lifting the code+ # around it moves the read with it. Only names this function binds matter.+ for u in def_use.captured:+ if u.name in def_lines:+ use_lines[u.name].append(u.line)for lines in def_lines.values():lines.sort()for lines in use_lines.values():diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.pyindex ac383342f..284013267 100644--- a/tests/unit/health/test_dataflow_langs.py+++ b/tests/unit/health/test_dataflow_langs.py@@ -1650,3 +1650,169 @@ def test_a_local_declared_and_read_on_one_line_in_the_span_is_not_a_parameter():assert extractions, "expected the second loop as an extraction"assert all("t" not in x.params for x in extractions)assert (("a", "n"), ("total",)) in [(x.params, x.returns) for x in extractions]+++# == Reads inside nested closures ===============================================+++def _covering(fn, lmap, first: int, last: int):+ return [e for e in find_extractions(fn, lmap) if e.start_line <= first and e.end_line >= last]+++def test_go_closure_read_of_a_parameter_makes_it_a_param():+ # ``ps`` is read only inside the func literal; the span still needs it.+ src = """+ package main++ func load(m *Map, ps *Page) []int {+ key := ps.Path()+ if key == "/" {+ key = ""+ }+ v, err := m.cache.GetOrCreate(key, func(string) ([]int, error) {+ res := m.find(ps)+ if len(res) > 2 {+ res = res[:2]+ }+ return res, nil+ })+ if err != nil {+ panic(err)+ }+ m.count+++ m.last = key+ m.seen = true+ return v+ }+ """+ lmap = get_language_map("go")+ spans = _covering(_first("go", src), lmap, 9, 18)+ assert spans+ assert all("ps" in e.params for e in spans)+++def test_ts_closure_read_after_the_span_makes_a_return():+ # ``seen`` is read after the span only inside the arrow function.+ src = """+ function prune(messages: Msg[], limit: number): Msg[] {+ const seen = new Set<string>()+ for (const msg of messages) {+ if (msg.id && msg.size < limit) {+ seen.add(msg.id)+ }+ }+ log(messages.length)+ log(limit)+ log(seen.size)+ return messages.filter((m) => seen.has(m.parent))+ }+ """+ lmap = get_language_map("typescript")+ spans = _covering(_first("typescript", src), lmap, 3, 8)+ assert spans+ assert all("seen" in e.returns for e in spans)+++def test_ts_closure_parameter_is_not_a_read_of_the_outer_name():+ src = """+ function scale(items: number[], x: number): number[] {+ let out: number[] = []+ if (x > 1) {+ out = items.map((x) => x * 2)+ out.push(0)+ } else {+ out = items.slice()+ }+ log(out.length)+ log(items.length)+ log(out.length)+ return out+ }+ """+ lmap = get_language_map("typescript")+ spans = _covering(_first("typescript", src), lmap, 4, 9)+ assert spans+ assert all("x" in e.params for e in spans) # the condition reads the outer x+ fn = _first("typescript", src)+ captured = {u.name for u in fn.def_use.captured}+ assert "x" not in captured and "items" not in captured+++ ("language", "src", "captured", "not_captured"),+ [+ (+ "python",+ """+ def f(rows, k):+ total = 0+ def inner(v):+ return v + total + k+ return list(map(lambda total: total * 2, rows)), inner+ """,+ {"total", "k"},+ {"v", "inner"},+ ),+ (+ "java",+ """+ class A {+ int f(java.util.List<Integer> rows, int k) {+ int total = 1;+ rows.forEach(v -> System.out.println(v + total + k));+ return total;+ }+ }+ """,+ {"total", "k"},+ {"v"},+ ),+ (+ "rust",+ """+ fn f(rows: Vec<i32>, k: i32) -> i32 {+ let total = 1;+ let s: i32 = rows.iter().map(|v| v + total + k).sum();+ s+ }+ """,+ {"total", "k"},+ {"v"},+ ),+ (+ "typescript",+ """+ function f(rows: number[], k: number) {+ const total = 1+ return rows.map((v) => rows.filter((w) => w + v + total > k))+ }+ """,+ {"total", "k", "rows"},+ {"v", "w"},+ ),+ ],+)+def test_closure_reads_are_captured_per_language(language, src, captured, not_captured):+ fn = _first(language, src)+ names = {u.name for u in fn.def_use.captured}+ assert captured <= names+ assert not (not_captured & names)+++def test_a_closure_local_shadowing_an_outer_name_is_not_a_capture():+ src = """+ function f(items: number[], t: number): number[] {+ let t2 = 0+ if (t > 1) {+ t2 = t * 3+ log(t2)+ }+ log(t)+ return items.map((v) => {+ const t2 = v * 2+ return t2 + t+ })+ }+ """+ fn = _first("typescript", src)+ assert "t2" not in {u.name for u in fn.def_use.captured}