Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(dataflow): keep a name the span declares out of Extract Method params (#2927)

Yang Bai2345fb26d836
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
index c40a223d4..5072283b7 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
@@ -40,6 +40,8 @@ class Definition:
block_id: int
index: int
line: int # 1-indexed
+ #: Where on ``line`` a declared name starts to exist; see ``Occurrence``.
+ declared_at: int | None = None
@dataclass
@@ -87,7 +89,13 @@ def compute_def_use(
def _add_def(occ: Occurrence, block_id: int) -> None:
nonlocal counter
bdu = blocks.setdefault(block_id, BlockDefUse(block_id))
- definition = Definition(var=occ.name, block_id=block_id, index=counter, line=occ.line)
+ definition = Definition(
+ var=occ.name,
+ block_id=block_id,
+ index=counter,
+ line=occ.line,
+ declared_at=occ.declared_at,
+ )
counter += 1
bdu.defs.append(definition)
definitions.append(definition)
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.py
index be5b36d35..524433f4c 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
@@ -40,7 +40,7 @@ precision-first contract the perf pillar depends on.
from __future__ import annotations
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from typing import TYPE_CHECKING, Protocol, runtime_checkable
if TYPE_CHECKING:
@@ -55,6 +55,13 @@ class Occurrence:
name: str
line: int # 1-indexed
+ column: int = 0 # 0-indexed, where the reference starts
+ #: A declaration only: the column on ``line`` where its declarator ends,
+ #: which is where the new name starts to exist. A read on that line at or
+ #: past it reads the new variable; one before it (the declaration's own
+ #: initializer) reads an outer variable of the same name. None for any
+ #: other write, and for a declarator that ends on a later line.
+ declared_at: int | None = None
@dataclass(frozen=True)
@@ -116,9 +123,23 @@ class BaseDefUseDialect:
def _occ(self, node: Node) -> Occurrence:
return Occurrence(
- name=(node.text or b"").decode("utf-8", "replace"), line=node.start_point[0] + 1
+ name=(node.text or b"").decode("utf-8", "replace"),
+ line=node.start_point[0] + 1,
+ column=node.start_point[1],
)
+ def _declare(self, defs: list[Occurrence], start: int, declarator: Node) -> None:
+ """Mark ``defs[start:]`` as names *declarator* declares.
+
+ Call it right after the declarator's targets are collected and before
+ its initializer is walked, so a write nested in the initializer is not
+ taken for a declaration.
+ """
+ end_row, end_col = declarator.end_point
+ for i in range(start, len(defs)):
+ if defs[i].line == end_row + 1:
+ defs[i] = replace(defs[i], declared_at=end_col)
+
# -- read (use) collection ------------------------------------------------
def collect_reads(self, node: Node | None, out: list[Occurrence]) -> None:
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
index b1e6857dd..84219e526 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
@@ -196,16 +196,19 @@ class CppDefUseDialect(BaseDefUseDialect):
return
if t in _DECL_KINDS:
for child in node.named_children:
+ start = len(defs)
if child.type == _INIT_DECLARATOR:
binder = self._binder_identifier(child.child_by_field_name("declarator"))
if binder is not None:
defs.append(self._occ(binder))
+ self._declare(defs, start, child)
self._process(child.child_by_field_name("value"), defs, uses)
elif child.type in self.identifier_kinds or child.type in _DECLARATOR_WRAPPERS:
# ``int x;`` / ``int* p;`` — a binding with no initialiser.
binder = self._binder_identifier(child)
if binder is not None:
defs.append(self._occ(binder))
+ self._declare(defs, start, child)
return
if t == _CALL:
self.collect_reads(node, uses)
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
index b430d761e..cda3c96ad 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
@@ -119,7 +119,9 @@ class GoDefUseDialect(BaseDefUseDialect):
self._process(node.child_by_field_name("right"), defs, uses)
return
if t == _SHORT_VAR: # ``a, b := ...``
+ start = len(defs)
self._targets(node.child_by_field_name("left"), defs, uses)
+ self._declare(defs, start, node)
self._process(node.child_by_field_name("right"), defs, uses)
return
if t in _VAR_DECL_KINDS: # ``var x int = ...`` / ``const c = ...``
@@ -146,9 +148,11 @@ class GoDefUseDialect(BaseDefUseDialect):
def _var_spec(self, spec: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
"""A ``var_spec`` / ``const_spec``: its direct ``identifier`` children are
the bound names (defs); its ``value`` is a read."""
+ start = len(defs)
for child in spec.named_children:
if child.type in self.identifier_kinds and child.text != b"_":
defs.append(self._occ(child))
+ self._declare(defs, start, spec)
self._process(spec.child_by_field_name("value"), defs, uses)
# -- write-target extraction ----------------------------------------------
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
index da17423e7..1b0f38bf3 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
@@ -159,7 +159,9 @@ class JavaDefUseDialect(BaseDefUseDialect):
for declarator in node.named_children:
if declarator.type != _DECLARATOR:
continue
+ start = len(defs)
self._targets(declarator.child_by_field_name("name"), defs, uses)
+ self._declare(defs, start, declarator)
self._process(declarator.child_by_field_name("value"), defs, uses)
return
if t == _METHOD_INVOCATION: # the ``name`` is a method, not a variable
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
index 71058f788..558657c1b 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
@@ -149,7 +149,9 @@ class TsJsDefUseDialect(BaseDefUseDialect):
for declarator in node.named_children:
if declarator.type != _DECLARATOR:
continue
+ start = len(defs)
self._targets(declarator.child_by_field_name("name"), defs, uses)
+ self._declare(defs, start, declarator)
self._process(declarator.child_by_field_name("value"), defs, uses)
return
if t in self.member_access_kinds:
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
index 2b4ef809e..c027262ce 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
@@ -99,6 +99,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
body_container = _unwrap_container(body, lmap.block_kinds)
def_lines, use_lines = _var_lines(analysis.def_use)
+ declared_first = _declared_before_read(analysis.def_use)
hoisted = _hoisted_bindings(def_lines, use_lines)
decision_kinds = (
lmap.branch_kinds
@@ -176,7 +177,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
span = stmts[i : j + 1]
s = span[0].start_point[0] + 1
e = span[-1].end_point[0] + 1
- params, returns = _infer_in_out(def_lines, use_lines, s, e)
+ params, returns = _infer_in_out(def_lines, use_lines, s, e, declared_first)
if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:
continue
if not _outs_definitely_assigned(span, returns, def_lines, lmap):
@@ -239,17 +240,45 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,
return def_lines, use_lines
+def _declared_before_read(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:
+ """Per variable, the lines where a declaration of it comes before its first
+ read on the same line.
+
+ Lines alone cannot order a write and a read that share one. A C-style
+ ``for (int i = 0; i < n; i++)`` declares ``i`` and then reads it, while
+ ``total = total + a[i]`` reads ``total`` and then writes it. Only a
+ declaration is ordered here, by where its declarator ends: a read past that
+ point sees the new name, and a read inside the declaration's own
+ initializer (Go's ``x := x + 1`` in an inner scope) still sees the outer
+ one. A plain assignment keeps the line rule.
+ """
+ first_read: dict[tuple[str, int], int] = {}
+ for bdu in def_use.blocks.values():
+ for u in bdu.uses:
+ key = (u.name, u.line)
+ first_read[key] = min(u.column, first_read.get(key, u.column))
+ declared: dict[str, set[int]] = defaultdict(set)
+ for d in def_use.definitions:
+ # No read on the line sorts before any declarator, so it never matches.
+ if d.declared_at is not None and d.declared_at <= first_read.get((d.var, d.line), -1):
+ declared[d.var].add(d.line)
+ return {var: frozenset(lines) for var, lines in declared.items()}
+
+
def _infer_in_out(
def_lines: dict[str, list[int]],
use_lines: dict[str, list[int]],
s: int,
e: int,
+ declared_first: dict[str, frozenset[int]] | None = None,
) -> tuple[tuple[str, ...], tuple[str, ...]]:
"""Infer IN (parameters) and OUT (return) variables for span ``[s, e]``.
IN: a variable read in the span whose first in-span read is not preceded by
an in-span write, and which has a definition before the span (a parameter or
- an earlier assignment). OUT: a variable written in the span and read after
+ an earlier assignment). A write on the same line as that read precedes it
+ only where *declared_first* (:func:`_declared_before_read`) says the line
+ declares the name first. OUT: a variable written in the span and read after
it, with no redefinition between the span and that first later read.
"""
params: list[str] = []
@@ -262,7 +291,8 @@ def _infer_in_out(
if in_uses and any(ln < s for ln in dl):
first_use = in_uses[0]
- if not any(ln < first_use for ln in in_defs):
+ declared = first_use in (declared_first or {}).get(var, ())
+ if not declared and not any(ln < first_use for ln in in_defs):
params.append(var)
if in_defs:
diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.py
index d32c9793b..ac383342f 100644
--- a/tests/unit/health/test_dataflow_langs.py
+++ b/tests/unit/health/test_dataflow_langs.py
@@ -1408,3 +1408,245 @@ def test_tsx_parses_without_error_recovery() -> None:
as_ts = parse_source("src/C.ts", "typescript", source)
assert as_ts is not None
assert as_ts[0].has_error
+
+
+# == A name the span itself declares is not a parameter =========================
+
+# Two loops reuse the counter ``i`` and the span holds the second. Its header
+# declares ``i`` and reads it on one line, and the first loop's ``i`` is a
+# definition of the same name before the span, so the counter read as an input
+# of the helper although the call site has no such variable.
+_TWO_LOOPS = {
+ "java": """
+ class Demo {
+ int run(int[] a, int n) {
+ int sum = 0;
+ for (int i = 0; i < n; i++) {
+ sum += a[i];
+ }
+ int total = 0;
+ for (int i = 0; i < n; i++) {
+ if (a[i] > 0) {
+ total += a[i];
+ } else {
+ total -= a[i];
+ }
+ }
+ return sum + total;
+ }
+ }
+ """,
+ "typescript": """
+ function run(a: number[], n: number): number {
+ let sum = 0;
+ for (let i = 0; i < n; i++) {
+ sum += a[i];
+ }
+ let total = 0;
+ for (let i = 0; i < n; i++) {
+ if (a[i] > 0) {
+ total += a[i];
+ } else {
+ total -= a[i];
+ }
+ }
+ return sum + total;
+ }
+ """,
+ "cpp": """
+ int run(int* a, int n) {
+ int sum = 0;
+ for (int i = 0; i < n; i++) {
+ sum += a[i];
+ }
+ int total = 0;
+ for (int i = 0; i < n; i++) {
+ if (a[i] > 0) {
+ total += a[i];
+ } else {
+ total -= a[i];
+ }
+ }
+ return sum + total;
+ }
+ """,
+ "go": """
+ package main
+ func run(a []int, n int) int {
+ sum := 0
+ for i := 0; i < n; i++ {
+ sum += a[i]
+ }
+ total := 0
+ for i := 0; i < n; i++ {
+ if a[i] > 0 {
+ total += a[i]
+ } else {
+ total -= a[i]
+ }
+ }
+ return sum + total
+ }
+ """,
+}
+
+
[email protected]("language", sorted(_TWO_LOOPS))
+def test_a_counter_declared_by_the_spans_own_loop_header_is_not_a_parameter(language):
+ lmap = get_language_map(language)
+ extractions = find_extractions(_first(language, _TWO_LOOPS[language]), lmap)
+
+ assert extractions, f"expected the second loop as a {language} extraction"
+ assert [(x.params, x.returns) for x in extractions] == [(("a", "n"), ("total",))]
+
+
+# The case the fix must not disturb: ``total`` is written and read on one line
+# too, but there the read comes first, so the helper does need it handed in.
+_READ_THEN_WRITE = {
+ "java": """
+ class Demo {
+ int run(int[] a, int n, int total) {
+ int sum = 0;
+ for (int j = 0; j < n; j++) {
+ sum += a[j];
+ }
+ total = total + a[0];
+ for (int k = 1; k < n; k++) {
+ if (a[k] > 0) {
+ total += a[k];
+ } else {
+ total -= a[k];
+ }
+ }
+ return sum + total;
+ }
+ }
+ """,
+ "typescript": """
+ function run(a: number[], n: number, total: number): number {
+ let sum = 0;
+ for (let j = 0; j < n; j++) {
+ sum += a[j];
+ }
+ total = total + a[0];
+ for (let k = 1; k < n; k++) {
+ if (a[k] > 0) {
+ total += a[k];
+ } else {
+ total -= a[k];
+ }
+ }
+ return sum + total;
+ }
+ """,
+ "cpp": """
+ int run(int* a, int n, int total) {
+ int sum = 0;
+ for (int j = 0; j < n; j++) {
+ sum += a[j];
+ }
+ total = total + a[0];
+ for (int k = 1; k < n; k++) {
+ if (a[k] > 0) {
+ total += a[k];
+ } else {
+ total -= a[k];
+ }
+ }
+ return sum + total;
+ }
+ """,
+ "go": """
+ package main
+ func run(a []int, n int, total int) int {
+ sum := 0
+ for j := 0; j < n; j++ {
+ sum += a[j]
+ }
+ total = total + a[0]
+ for k := 1; k < n; k++ {
+ if a[k] > 0 {
+ total += a[k]
+ } else {
+ total -= a[k]
+ }
+ }
+ return sum + total
+ }
+ """,
+}
+
+
[email protected]("language", sorted(_READ_THEN_WRITE))
+def test_a_variable_read_before_its_same_line_write_stays_a_parameter(language):
+ lmap = get_language_map(language)
+ extractions = find_extractions(_first(language, _READ_THEN_WRITE[language]), lmap)
+
+ assert [(x.params, x.returns) for x in extractions] == [(("a", "n", "total"), ("total",))]
+
+
+def test_go_redeclaration_that_reads_the_outer_name_keeps_it_as_a_parameter():
+ """``x := x + v`` in an inner scope declares a new ``x`` from the outer one:
+ the read sits in the declaration's own initializer, before the new name
+ exists, so the outer ``x`` is still an input."""
+ fn = _first(
+ "go",
+ """
+ package main
+ func run(a []int, x int) int {
+ sum := 0
+ for j := 0; j < x; j++ {
+ sum += a[j]
+ }
+ total := 0
+ for _, v := range a {
+ x := x + v
+ if x > 0 {
+ total += x
+ } else {
+ total -= x
+ }
+ }
+ return sum + total
+ }
+ """,
+ )
+
+ extractions = find_extractions(fn, get_language_map("go"))
+
+ assert [(x.params, x.returns) for x in extractions] == [(("a", "x"), ("total",))]
+
+
+def test_a_local_declared_and_read_on_one_line_in_the_span_is_not_a_parameter():
+ """The same shape without a loop header: ``t`` is declared and then read on
+ one line inside the span, and an earlier block has its own ``t``."""
+ fn = _first(
+ "java",
+ """
+ class Demo {
+ int run(int[] a, int n) {
+ int sum = 0;
+ for (int j = 0; j < n; j++) {
+ int t = a[j];
+ sum += t;
+ }
+ int total = 0;
+ for (int k = 0; k < n; k++) {
+ int t = a[k]; total += t;
+ if (t > 0) {
+ total += 1;
+ } else {
+ total -= 1;
+ }
+ }
+ return sum + total;
+ }
+ }
+ """,
+ )
+
+ extractions = find_extractions(fn, get_language_map("java"))
+
+ 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]
diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.py
index 6a2a7817c..4908e65d6 100644
--- a/tests/unit/health/test_refactoring_extract_method.py
+++ b/tests/unit/health/test_refactoring_extract_method.py
@@ -158,6 +158,7 @@ def _find_extractions_reference(analysis, lmap):
_MIN_STMTS,
Extraction,
_all_blocks,
+ _declared_before_read,
_infer_in_out,
_loop_carry_free,
_outs_definitely_assigned,
@@ -175,6 +176,7 @@ def _find_extractions_reference(analysis, lmap):
return []
body_container = _unwrap_container(body, lmap.block_kinds)
def_lines, use_lines = _var_lines(analysis.def_use)
+ declared_first = _declared_before_read(analysis.def_use)
decision_kinds = (
lmap.branch_kinds
| lmap.loop_kinds
@@ -221,7 +223,7 @@ def _find_extractions_reference(analysis, lmap):
continue
s = span[0].start_point[0] + 1
e = span[-1].end_point[0] + 1
- params, returns = _infer_in_out(def_lines, use_lines, s, e)
+ params, returns = _infer_in_out(def_lines, use_lines, s, e, declared_first)
if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:
continue
if not _outs_definitely_assigned(span, returns, def_lines, lmap):
@@ -648,3 +650,48 @@ def test_a_best_span_below_the_floor_yields_to_the_next_one(monkeypatch):
(s,) = ExtractMethodDetector().detect(ctx)
assert s.plan["span"] == {"start": 20, "end": 25}
assert s.impact_delta == round(1.2 * 3 / 12, 3)
+
+
+# A plan that names the extracted loop's own counter as a parameter cannot
+# compile: the call site has no such variable. Two loops sharing ``i`` used to
+# produce exactly that, because the first loop's ``i`` looked like a definition
+# before the span and the header's same-line read looked like an input.
+_TWO_LOOPS_JAVA = """
+class Demo {
+ int run(int[] a, int n) {
+ int sum = 0;
+ for (int i = 0; i < n; i++) {
+ sum += a[i];
+ }
+ int total = 0;
+ for (int i = 0; i < n; i++) {
+ if (a[i] > 0) {
+ total += a[i];
+ } else {
+ total -= a[i];
+ }
+ }
+ return sum + total;
+ }
+}
+"""
+
+
+def test_plan_params_leave_out_a_loop_counter_the_span_declares():
+ res = analyze_file("Demo.java", "java", _TWO_LOOPS_JAVA.encode(), flagged_only=False)
+ if res.stats.functions_seen == 0:
+ pytest.skip("tree-sitter language pack missing for java")
+ ctx = RefactoringContext(
+ file_path="Demo.java",
+ language="java",
+ nloc=100,
+ findings=[_Finding("complex_method", "run", line_start=3, health_impact=1.5)],
+ function_analyses=res.functions,
+ )
+
+ suggestions = ExtractMethodDetector().detect(ctx)
+
+ assert len(suggestions) == 1
+ plan = suggestions[0].plan
+ assert plan["params"] == ["a", "n"]
+ assert plan["returns"] == ["total"]