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): a write in a TypeScript, Go or Python switch arm is a may-def (#3522)

Yang Bai150ef6d83544
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 669d3baea..292f050a1 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
@@ -18,6 +18,7 @@ unwraps that, so this dialect only ever sees individual statements.
from __future__ import annotations
+from dataclasses import replace
from typing import TYPE_CHECKING
from .base import (
@@ -47,6 +48,13 @@ _INC_DEC_KINDS = frozenset({"inc_statement", "dec_statement"})
_EXPRESSION_LIST = "expression_list"
_RANGE_CLAUSE = "range_clause"
_FOR_CLAUSE = "for_clause"
+# A ``switch`` / ``select`` is one CFG statement; writes inside its arms are
+# may-defs.
+_CONDITIONAL_KINDS = frozenset(
+ {"expression_switch_statement", "type_switch_statement", "select_statement"}
+)
+# ``case v := <-ch:`` / ``case x = <-ch:`` in a ``select``.
+_RECEIVE = "receive_statement"
# Selector (``obj.field``) and index (``arr[i]``) targets bind no local.
_SELECTOR = "selector_expression"
_INDEX = "index_expression"
@@ -173,6 +181,21 @@ class GoDefUseDialect(BaseDefUseDialect):
self._targets(child, defs, uses)
self.collect_reads(child, uses)
return
+ if t in _CONDITIONAL_KINDS:
+ self._process_may_def(node, defs, uses)
+ return
+ if t == _RECEIVE:
+ start = len(defs)
+ self._targets(node.child_by_field_name("left"), defs, uses)
+ if any(tok.type == ":=" for tok in node.children):
+ self._declare(defs, start, node)
+ # Bound only if this case is the one taken, and a ``:=`` name lives
+ # only in its case: neither hides the value from before the
+ # ``select``. ``may_defs`` leaves a declared name certain, so the
+ # flag is set here for both.
+ defs[start:] = [replace(occ, may=True) for occ in defs[start:]]
+ self._process(node.child_by_field_name("right"), defs, uses)
+ return
if t in self.member_access_kinds:
self.collect_reads(node, uses) # receiver is a read; field name is not
return
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
index a643dee33..4be873583 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
@@ -56,6 +56,8 @@ _SCOPE_BOUNDARIES = frozenset({"lambda", "function_definition", "async_function_
_COMPREHENSIONS = frozenset(
{"list_comprehension", "set_comprehension", "dictionary_comprehension", "generator_expression"}
)
+# A ``match`` is one CFG statement; writes inside its arms are may-defs.
+_CONDITIONAL_KINDS = frozenset({"match_statement"})
# A module path is not a read; an import writes only the names it binds.
_IMPORT_KINDS = frozenset({"import_statement", "import_from_statement"})
@@ -215,6 +217,9 @@ class PythonDefUseDialect(BaseDefUseDialect):
if value is None or child.id != value.id:
self._process(child, defs, uses)
return
+ if t in _CONDITIONAL_KINDS:
+ self._process_may_def(node, defs, uses)
+ return
if t in self.member_access_kinds:
self.collect_reads(node, uses) # receiver is a read; member is not
return
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 c15c01c43..1ed3c646d 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
@@ -53,6 +53,8 @@ _REST_PATTERN = "rest_pattern"
_ASSIGN_PATTERN = "assignment_pattern" # default value ``a = 1`` in a pattern
_CATCH = "catch_clause"
_PARAM_PATTERNS = frozenset({_ARRAY_PATTERN, _OBJECT_PATTERN, _REST_PATTERN, _ASSIGN_PATTERN})
+# A ``switch`` is one CFG statement; writes inside its arms are may-defs.
+_CONDITIONAL_KINDS = frozenset({"switch_statement"})
# Member / subscript targets bind no local.
_MEMBER = "member_expression"
_SUBSCRIPT = "subscript_expression"
@@ -254,6 +256,9 @@ class TsJsDefUseDialect(BaseDefUseDialect):
defs.extend(sdu.defs)
self._process(node.child_by_field_name("body"), defs, uses)
return
+ if t in _CONDITIONAL_KINDS:
+ self._process_may_def(node, defs, uses)
+ return
if t in self.member_access_kinds:
self.collect_reads(node, uses) # receiver is a read; property is not
return
diff --git a/tests/unit/health/test_extract_method_span_interface.py b/tests/unit/health/test_extract_method_span_interface.py
index fc14a2796..777bdd312 100644
--- a/tests/unit/health/test_extract_method_span_interface.py
+++ b/tests/unit/health/test_extract_method_span_interface.py
@@ -15,6 +15,7 @@ import pytest
from repowise.core.analysis.health.complexity.languages import get_language_map
from repowise.core.analysis.health.dataflow import find_extractions
+from repowise.core.analysis.health.dataflow.stages import SWITCH_ARM_WRITE, find_stages
from repowise.core.analysis.health.refactoring.extract_method import (
STAGE_REFUSED_DETAIL,
ExtractMethodDetector,
@@ -275,9 +276,16 @@ def test_a_global_refusal_is_counted_under_its_reason():
assert _refused(src)["resolve"] == ["outer_write"]
-def test_a_switch_arm_refusal_is_counted_and_a_dead_arm_name_is_not_refused():
- hazard = _ts_loop("switch (mode) { case 1: last = item; break; }")
- assert _refused(hazard, "typescript", "ts")["run"] == ["switch_arm_write"]
+def test_a_switch_arm_write_is_taken_and_returned_and_a_dead_arm_name_is_not_refused():
+ # The arm may not run, so ``last`` after it is the arm's value or the one
+ # from before (a may-def, #3451): the stage that holds the arm and the
+ # read takes ``last`` and hands it back, and nothing is refused.
+ carried = _ts_loop("switch (mode) { case 1: last = item; break; }")
+ assert _refused(carried, "typescript", "ts")["run"] == []
+ served = _plans(carried, "typescript", "ts")["run"].plan["stages"]
+ read = next(i for i, ln in enumerate(carried.splitlines(), 1) if "note(last)" in ln)
+ holding = next(s for s in served if s["span"]["start"] <= read <= s["span"]["end"])
+ assert "last" in holding["params"] and holding["returns"] == ["last"]
# An arm writing a name nothing reads past the edge decides nothing.
quiet = _ts_loop("switch (mode) { case 1: tally = item; break; }").replace(
"mode, last,", "mode, last, tally,"
@@ -288,6 +296,44 @@ def test_a_switch_arm_refusal_is_counted_and_a_dead_arm_name_is_not_refused():
assert any(s["span"]["start"] <= arm <= s["span"]["end"] for s in stages)
+_ARMS_ONLY = """
+function pick(client, options, raw) {
+ const model = options.model;
+ let input, format;
+ switch (model) {
+ case 'small':
+ input = raw.a || options.a;
+ format = raw.fa || options.fa;
+ break;
+ case 'large':
+ input = raw.b || options.b;
+ format = raw.fb || options.fb;
+ break;
+ case 'old':
+ input = raw.c || options.c;
+ break;
+ }
+
+ const made = client.create({
+ model: model,
+ input: input,
+ format: format,
+ ...(raw.d && { d: Number(raw.d) }),
+ });
+ return made.data[0];
+}
+"""
+
+
+def test_a_stage_reading_what_only_the_arms_wrote_is_still_refused():
+ # ``input`` is declared without a value and written in the arms; the stage
+ # from the ``switch`` to the call reads it and is not given it, because a
+ # stage's inputs are read off line order and a write earlier in the stage
+ # covers the read. The refusal is what keeps that stage from being served.
+ fn = _functions(_ARMS_ONLY, "typescript", "ts")[0]
+ assert find_stages(fn, get_language_map("typescript")).refused == {SWITCH_ARM_WRITE}
+
+
# -- closures and outer scopes ----------------------------------------------------
_NONLOCAL = """
diff --git a/tests/unit/health/test_go_select_receive.py b/tests/unit/health/test_go_select_receive.py
new file mode 100644
index 000000000..5f01d19b7
--- /dev/null
+++ b/tests/unit/health/test_go_select_receive.py
@@ -0,0 +1,230 @@
+"""Go: the receive of a ``select`` case binds its names (#3469, #3451).
+
+``case v := <-ch:`` and ``case x = <-ch:`` are ``receive_statement`` nodes,
+which were no write kind: ``v`` had no definition and ``x`` no write on its
+case line. A ``select`` is one CFG statement, and the receive happens only if
+its case is the one taken, so both are may-defs: they add to what a later
+read sees and hide nothing. That holds for the ``:=`` form too, whose name
+lives only in its case and often repeats one from outside (``case err :=
+<-errCh:`` beside an outer ``err``).
+"""
+
+from __future__ import annotations
+
+import textwrap
+
+import pytest
+
+from repowise.core.analysis.health.complexity.languages import get_language_map
+from repowise.core.analysis.health.dataflow import analyze_file, find_extractions
+
+
+def _f(src: str, name: str = "f"):
+ try:
+ from repowise.core.ingestion.parser import _get_language
+ except ImportError:
+ pytest.skip("tree-sitter language pack missing for go")
+ if _get_language("go") is None:
+ pytest.skip("tree-sitter language pack missing for go")
+ functions = analyze_file(
+ "m.go", "go", ("package m\n" + textwrap.dedent(src).lstrip("\n")).encode(), flagged_only=False
+ ).functions
+ return next(fn for fn in functions if fn.name == name)
+
+
+def _defs(fn) -> set[tuple[str, int]]:
+ return {(d.var, d.line) for d in fn.def_use.definitions}
+
+
+def _reads(fn) -> set[tuple[str, int]]:
+ return {(u.name, u.line) for b in fn.def_use.blocks.values() for u in b.uses if not u.echo}
+
+
+def _declared(fn) -> set[str]:
+ return {d.var for d in fn.def_use.definitions if d.declares}
+
+
+def _may(fn) -> set[tuple[str, int]]:
+ return {(d.var, d.line) for d in fn.def_use.definitions if d.may}
+
+
+# -- the receive of a select case --------------------------------------------------
+
+_SELECT = """
+func f(ch chan int, done chan bool, s *S) int {
+ var got int
+ select {
+ case v := <-ch:
+ got = v
+ case got = <-ch:
+ case x, ok := <-ch:
+ got = x
+ _ = ok
+ case s.last = <-ch:
+ case <-done:
+ case ch <- got:
+ }
+ return got
+}
+"""
+
+
+def test_a_select_case_receive_binds_its_names():
+ fn = _f(_SELECT)
+ defs = _defs(fn)
+ assert {("v", 5), ("got", 7), ("x", 8), ("ok", 8)} <= defs
+ # ``:=`` declares; ``=`` assigns a variable that exists.
+ assert {"v", "x", "ok"} <= _declared(fn)
+ line_7 = [d for d in fn.def_use.definitions if (d.var, d.line) == ("got", 7)]
+ assert len(line_7) == 1 and not line_7[0].declares
+
+
+def test_a_select_case_receive_is_a_may_def_in_both_forms():
+ fn = _f(_SELECT)
+ assert {("v", 5), ("got", 7), ("x", 8), ("ok", 8)} <= _may(fn)
+ # The declaration before the ``select`` is certain, as it was.
+ assert ("got", 3) not in _may(fn)
+
+
+def test_a_select_case_receive_reads_its_channel_and_its_target_base():
+ fn = _f(_SELECT)
+ reads = _reads(fn)
+ assert {("ch", 5), ("ch", 7), ("ch", 8), ("ch", 11), ("done", 12)} <= reads
+ # ``s.last = <-ch`` writes through a pointer: ``s`` is read, not written.
+ assert ("s", 11) in reads
+ assert "s" not in {name for name, line in _defs(fn) if line == 11}
+ # A send reads both sides and binds nothing.
+ assert {("ch", 13), ("got", 13)} <= reads
+ assert not {name for name, line in _defs(fn) if line in (12, 13)}
+
+
+def test_a_name_bound_by_a_receive_is_read_in_its_case():
+ fn = _f(_SELECT)
+ assert {("v", 6), ("x", 9), ("ok", 10)} <= _reads(fn)
+
+
+def test_a_span_in_a_case_is_given_the_name_the_receive_bound():
+ fn = _f(
+ """
+ func f(ch chan int, out *Sink) {
+ select {
+ case v := <-ch:
+ if v > 10 {
+ out.add(v)
+ } else {
+ out.add(0)
+ }
+ out.note(v)
+ out.done()
+ println(ch)
+ case <-out.stop:
+ }
+ }
+ """
+ )
+ spans = [
+ (x.start_line, x.end_line, x.params)
+ for x in find_extractions(fn, get_language_map("go"))
+ if x.start_line == 5
+ ]
+ assert (5, 10, ("out", "v")) in spans
+ assert all("v" in params for _start, _end, params in spans)
+
+
+# The value from before the ``select`` is still the one a later read can see:
+# a span that holds the earlier write must return it. The slicer returns one
+# value, so in the first shape (from the review of #3472) a span holding the
+# write has ``n`` or ``total`` to return as well and is not offered at all; in
+# the second it can return ``err`` alone.
+_OUTER = """
+func f(xs []int, ch chan error) error {
+ err := g(xs[0])
+ n := len(xs)
+ if n > 3 {
+ err = h(n)
+ n = n * 2
+ }
+ total := n + 1
+ log(total)
+ {STATEMENT}
+ use(total)
+ return err
+}
+"""
+_OUTER_ALONE = """
+func f(xs []int, ch chan error) error {
+ err := g(xs[0])
+ n := len(xs)
+ if n > 3 {
+ err = h(n)
+ n = n * 2
+ }
+ if n > 9 {
+ log(n)
+ } else {
+ log(0)
+ }
+ {STATEMENT}
+ return err
+}
+"""
+_SELECT_DECLARES = "select {\n case err := <-ch:\n log(err)\n default:\n log(nil)\n }"
+_SELECT_ASSIGNS = "select {\n case err = <-ch:\n log(err)\n default:\n log(nil)\n }"
+_IF_TWIN = "if len(ch) > 0 {\n err = <-ch\n log(err)\n } else {\n log(nil)\n }"
+_STATEMENTS = pytest.mark.parametrize(
+ "statement", [_SELECT_DECLARES, _SELECT_ASSIGNS, _IF_TWIN], ids=[":=", "=", "if twin"]
+)
+
+
+def _spans_holding_the_earlier_write(shape: str, statement: str):
+ fn = _f(shape.replace("{STATEMENT}", statement))
+ write = next(
+ d.line for d in fn.def_use.definitions if d.var == "err" and not d.declares and not d.may
+ )
+ last_read = max(line for name, line in _reads(fn) if name == "err")
+ spans = find_extractions(fn, get_language_map("go"))
+ return [x for x in spans if x.start_line <= write <= x.end_line < last_read]
+
+
+@_STATEMENTS
+def test_no_span_holds_the_earlier_write_and_drops_it(statement):
+ for shape in (_OUTER, _OUTER_ALONE):
+ holding = _spans_holding_the_earlier_write(shape, statement)
+ assert [(x.start_line, x.end_line) for x in holding if "err" not in x.returns] == []
+
+
+@_STATEMENTS
+def test_a_span_that_holds_the_earlier_write_returns_it(statement):
+ holding = _spans_holding_the_earlier_write(_OUTER_ALONE, statement)
+ assert [(x.start_line, x.end_line, x.returns) for x in holding] == [(3, 13, ("err",))]
+
+
+def test_the_read_after_the_select_sees_the_writes_from_before_it():
+ from repowise.core.analysis.health.dataflow.reaching import observed_definitions
+
+ for statement in (_SELECT_DECLARES, _SELECT_ASSIGNS):
+ fn = _f(_OUTER.replace("{STATEMENT}", statement))
+ defs = fn.reaching.definitions
+ last_read = max(line for name, line in _reads(fn) if name == "err")
+ seen = {
+ defs[i].line
+ for use, indices in observed_definitions(fn.def_use, fn.reaching)
+ if (use.name, use.line) == ("err", last_read) and not use.echo
+ for i in indices
+ }
+ assert {3, 6} <= seen, statement
+
+
+def test_a_plain_receive_assignment_outside_select_is_unchanged():
+ """The control: ``v := <-ch`` as a statement is a short var declaration."""
+ fn = _f(
+ """
+ func f(ch chan int) int {
+ v := <-ch
+ var w int
+ w = <-ch
+ return v + w
+ }
+ """
+ )
+ assert {("v", 3), ("w", 4), ("w", 5)} <= _defs(fn)
diff --git a/tests/unit/health/test_may_def_reaching.py b/tests/unit/health/test_may_def_reaching.py
index e6ea299f7..eb83e413b 100644
--- a/tests/unit/health/test_may_def_reaching.py
+++ b/tests/unit/health/test_may_def_reaching.py
@@ -23,10 +23,22 @@ from repowise.core.analysis.health.dataflow.reaching import _observe, observed_d
from repowise.core.analysis.health.refactoring.extract_method import ExtractMethodDetector
from repowise.core.analysis.health.refactoring.models import RefactoringContext
-# One function per language whose dialect marks an arm's write: a loop that raises ``best`` and counts ``seen``,
+# One function per language with a dialect: a loop that raises ``best`` and counts ``seen``,
# then ``{COND}`` which may overwrite ``best``, then a read of both. The span
# from ``seen = 0`` through the loop writes two values the code reads after
# it, so no helper with one return can replace it.
+_PYTHON = '''def f(v, xs):
+ best = -1
+ seen = 0
+ for a in xs:
+ seen += 1
+ if a > best:
+ best = a
+ else:
+ print(a)
+ {COND}
+ return best + seen
+'''
_JAVA = '''class A {
int f(int v, int[] xs) {
int best = -1;
@@ -44,6 +56,38 @@ _JAVA = '''class A {
}
}
'''
+_TYPESCRIPT = '''function f(v: number, xs: number[]): number {
+ let best = -1;
+ let seen = 0;
+ for (const a of xs) {
+ seen++;
+ if (a > best) {
+ best = a;
+ } else {
+ console.log(a);
+ }
+ }
+ {COND}
+ return best + seen;
+}
+'''
+_GO = '''package p
+
+func f(v int, xs []int) int {
+ best := -1
+ seen := 0
+ for _, a := range xs {
+ seen++
+ if a > best {
+ best = a
+ } else {
+ println(a)
+ }
+ }
+ {COND}
+ return best + seen
+}
+'''
_RUST = '''fn f(v: i32, xs: &[i32]) -> i32 {
let mut best = -1;
let mut seen = 0;
@@ -94,12 +138,30 @@ _CSHARP = '''class A {
# language -> (file name, source, the arm form, its ``if`` twin)
_SHAPES: dict[str, tuple[str, str, str, str]] = {
+ "python": (
+ "m.py",
+ _PYTHON,
+ "match v:\n case 1:\n best = 0",
+ "if v == 1:\n best = 0",
+ ),
"java": (
"A.java",
_JAVA,
"switch (v) {\n case 1:\n best = 0;\n break;\n }",
"if (v == 1) {\n best = 0;\n }",
),
+ "typescript": (
+ "a.ts",
+ _TYPESCRIPT,
+ "switch (v) {\n case 1:\n best = 0;\n break;\n }",
+ "if (v === 1) {\n best = 0;\n }",
+ ),
+ "go": (
+ "a.go",
+ _GO,
+ "switch v {\n\tcase 1:\n\t\tbest = 0\n\t}",
+ "if v == 1 {\n\t\tbest = 0\n\t}",
+ ),
"rust": (
"a.rs",
_RUST,
@@ -153,8 +215,9 @@ def _line_of(source: str, text: str) -> int:
def _best_writes(language: str, source: str) -> tuple[int, int, int]:
"""The lines of the three writes of ``best``: the first, the loop's and
the arm's."""
+ first = "best := -1" if language == "go" else "best = -1"
loop = "best = *a" if language == "rust" else "best = a"
- return _line_of(source, "best = -1"), _line_of(source, loop), _line_of(source, "best = 0")
+ return _line_of(source, first), _line_of(source, loop), _line_of(source, "best = 0")
def _observed_lines(analysis, name: str, line: int) -> set[int]:
@@ -242,6 +305,29 @@ def test_a_write_in_an_if_branch_is_a_certain_def_of_its_own_block(language):
assert writes == {first: False, loop: False, arm: False}
+def test_go_type_switch_and_select_arms_are_may_defs():
+ source = '''package p
+
+func f(v interface{}, ch chan int) int {
+ n := 0
+ switch v.(type) {
+ case int:
+ n = 1
+ }
+ select {
+ case m := <-ch:
+ n = m
+ default:
+ }
+ return n
+}
+'''
+ analysis = _analyses("go", "a.go", source)[0]
+ writes = {d.line: d.may for d in analysis.def_use.definitions if d.var == "n"}
+ assert writes == {4: False, 7: True, 11: True}
+ assert _observed_lines(analysis, "n", 14) == {4, 7, 11}
+
+
# A name an arm declares is a new binding for the rest of that arm, not a
# write to the variable the code before the ``switch`` set. Arms reuse such
# names, so these stay certain: the read in an arm sees its own declaration.
@@ -266,6 +352,46 @@ _DECLARED_IN_AN_ARM: dict[str, tuple[str, str, str]] = {
return out;
}
}
+""",
+ "out = n + 1",
+ ),
+ "go": (
+ "a.go",
+ """package p
+
+func f(v int) int {
+ out := 0
+ switch v {
+ case 1:
+ n := 1
+ out = n
+ case 2:
+ n := 2
+ out = n + 1
+ }
+ return out
+}
+""",
+ "out = n + 1",
+ ),
+ "typescript": (
+ "a.ts",
+ """function f(v: number): number {
+ let out = 0;
+ switch (v) {
+ case 1: {
+ const n = 1;
+ out = n;
+ break;
+ }
+ case 2: {
+ const n = 2;
+ out = n + 1;
+ break;
+ }
+ }
+ return out;
+}
""",
"out = n + 1",
),
@@ -307,6 +433,27 @@ def test_a_name_an_arm_declares_is_not_a_may_def(language):
assert [d.may for d in analysis.def_use.definitions if d.var == "out"] == [False, True, True]
+def test_a_var_in_an_arm_is_a_may_def_since_it_binds_nothing_new():
+ """A TS / JS ``var`` hoists to the function: in an arm it is a write to
+ the one variable the code before the ``switch`` set."""
+ source = """function f(v) {
+ var out = 0;
+ switch (v) {
+ case 1:
+ var out = 1;
+ break;
+ }
+ return out;
+}
+"""
+ analysis = _analyses("javascript", "a.js", source)[0]
+ assert {d.line: d.may for d in analysis.def_use.definitions if d.var == "out"} == {
+ 2: False,
+ 5: True,
+ }
+ assert _observed_lines(analysis, "out", 8) == {2, 5}
+
+
def test_rust_if_let_binder_is_left_as_it_was():
"""An ``if let`` binder is a new variable for its arm, as a name an arm
declares is. Marked as a write that may not happen, the second arm's read
@@ -462,8 +609,10 @@ def test_the_if_twin_has_no_such_span_either(language):
assert _spans_dropping_best(language, arm=False) == []
[email protected]("language", _LANGUAGES)
[email protected]("language", [lang for lang in _LANGUAGES if lang != "python"])
def test_the_detector_offers_no_plan_for_the_arm_form(language):
+ """Python is left out: its detector gives this function no plan in either
+ form, so it would pass before and after."""
path, source, analysis = _shape(language, arm=True)
ctx = RefactoringContext(
file_path=path,