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(health): measure functions defined inside another function (#3405)

Yang Baiccbe9f643bfa
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.md
index f704284fd..51a29f680 100644
--- a/docs/architecture/code-health.md
+++ b/docs/architecture/code-health.md
@@ -265,7 +265,10 @@ A single pass over the parsed file list. For each file the analyzer:
`FileComplexity` of functions and classes. Each `FunctionComplexity` carries
name, line range, nloc, ccn, max nesting, cognitive complexity, bumps, and
param count; each `ClassComplexity` carries method count, total nloc, the
- method list, LCOM4, max method ccn, and field count.
+ method list, LCOM4, max method ccn, and field count. A function or method
+ defined inside another function has a `FunctionComplexity` of its own: its
+ branches are counted there and not in the function around it. A lambda has
+ none and counts toward the function that holds it.
2. **Populates symbol complexity** (`_populate_symbol_complexity`): writes
`max(ccn)` into `Symbol.complexity_estimate` as a side effect, so the
`ContextAssembler` symbol ranker benefits even when a caller never touches
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py b/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
index 906264b2b..ad84d1dd4 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
@@ -108,10 +108,11 @@ def _asserting_names(ctx: FileContext) -> frozenset[str]:
method on a test-local double suppresses against a module-level function of
that name. Both directions hide a real finding rather than invent one,
which is the tolerable way round for an advisory marker, but neither is
- free. A helper nested inside the test body is still not collected as a
- function at all, so it suppresses nothing *by name* -- but its calls now
- roll up into the enclosing test's own ``called_names``, so the test it
- sits in is answered directly rather than through this set.
+ free. A helper nested inside a test body has a row of its own and is in
+ this set like any other function, so it answers for the test that calls it
+ and, the file being one namespace, for any other test calling that name.
+ Its calls also roll up into the enclosing test's own ``called_names``,
+ which is what answers a test that hands the helper on without calling it.
Same file only, and by name. The cross-file half of the same question --
a shared JS fixture, an inherited base-class helper -- is answered from the
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/README.md b/packages/core/src/repowise/core/analysis/health/complexity/README.md
index 662e4050c..24186e89c 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/README.md
+++ b/packages/core/src/repowise/core/analysis/health/complexity/README.md
@@ -10,7 +10,7 @@ Tree-sitter AST walker. Single AST pass per file computes:
> | `walker.py` | Orchestration (`walk_file` / `walk_file_complexity`) + public re-exports |
> | `models.py` | Output dataclasses (`FunctionComplexity`, `ClassComplexity`, `PerfHit`, ...) |
> | `ast_utils.py` | Name/text helpers, callee-name reading, function-node collection, parameter counting |
-> | `file_scan.py` | The one descent the whole-file passes share (NLOC index, error handling, class nodes, Rust test spans, perf import names) |
+> | `file_scan.py` | The one descent the whole-file passes share (NLOC index, error handling, class nodes, function nodes at any depth, Rust test spans, perf import names) |
> | `nloc.py` | Non-blank / non-comment line counting (`CodeLineIndex`) |
> | `cyclomatic.py` | The CCN / cognitive / max-nesting engine (`_walk_function_body`) |
> | `assertions.py` | Assertion-block detection (test-quality smells) |
@@ -133,7 +133,10 @@ Consumed by `low_cohesion` and `god_class`.
## Outputs
One `FunctionComplexity` per function/method symbol detected. Caller maps
-back to ingestion `Symbol` objects by overlapping line ranges.
+back to ingestion `Symbol` objects by overlapping line ranges. A function or
+method defined inside another function has its own entry, listed before the
+entries of the file's own level; a lambda has none and counts toward the
+function that holds it.
## Extension points
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/assertions.py b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
index 260af465c..b1bb16280 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
@@ -392,12 +392,12 @@ def _collect_assertion_facts(
*called* is every name called directly in this body, lowercased,
**including nested function bodies**, which is where it parts company with
the counts beside it. Those stop at a nested function because its
- assertions are its own. A name does not work that way here, because a
- function nested inside an already-collected one is never collected as an
- entry of its own -- ``ast_utils._collect_function_nodes`` does not descend
- past a function -- so stopping would attribute its calls to nobody at all,
- and an ``expect`` inside an inline ``function`` helper would be recorded
- nowhere. It rides along on this traversal because the traversal already
+ assertions are its own. A name does not work that way here. The nested
+ function has an entry of its own, with its own names, but the test around
+ it is the one asked whether it checked anything, and a test that hands an
+ inline ``function`` helper to something else (``rows.forEach(verifyRow)``)
+ never calls it by name, so stopping would put the helper's ``expect`` out
+ of that test's reach. It rides along on this traversal because the traversal already
reaches every call node; collecting it separately would mean walking every
function body twice. It counts nothing and so cannot move a calibrated
marker. Empty under the early return above, which is fine only because the
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
index e0da5d2fd..ecdc23547 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
@@ -307,6 +307,32 @@ def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
return out
+def _nested_function_nodes(
+ collected: list[Node], candidates: list[Node], lmap: LanguageNodeMap
+) -> list[Node]:
+ """The functions and methods defined inside a collected function.
+
+ *collected* is what :func:`_collect_function_nodes` returned and
+ *candidates* every ``function_kinds`` node in the file. That collector
+ stops at the first function or lambda it meets and descends everything
+ else, so a function it did not return sits below one it did: a nested
+ ``def``, a function expression, a local function, a method of a local or
+ anonymous class. Each is a unit of its own, which the enclosing function's
+ walk skips. Lambdas are not candidates; they stay with the function that
+ holds them. A local value that shares a function's node type
+ (``lmap.local_value``) is no unit either.
+ """
+ seen = {node.id for node in collected}
+ local_value = lmap.local_value
+ return [
+ node
+ for node in candidates
+ if node.id not in seen
+ and is_function_node(node, lmap)
+ and not (local_value is not None and local_value(node))
+ ]
+
+
def _parameter_list(fn_node: Node) -> Node | None:
"""The node holding *fn_node*'s declared parameters, or ``None``."""
params = fn_node.child_by_field_name("parameters")
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/file_scan.py b/packages/core/src/repowise/core/analysis/health/complexity/file_scan.py
index f5f970531..1329c6040 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/file_scan.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/file_scan.py
@@ -42,6 +42,9 @@ class FileScan:
lines: CodeLineIndex
error_handling_hits: list[ErrorHandlingHit]
class_nodes: list[Node]
+ #: Every ``function_kinds`` node in the file, at any depth: the ones
+ #: ``_collect_function_nodes`` returns and the ones nested below them.
+ function_nodes: list[Node]
#: 1-indexed ``(start_line, end_line)`` spans of Rust test-only code.
rust_test_line_ranges: tuple[tuple[int, int], ...]
#: ``perf.io_boundaries.collect_io_names`` for the file, or ``{}`` when
@@ -95,12 +98,14 @@ def scan_file(
excluded: list[tuple[int, int]] = []
hits: list[ErrorHandlingHit] = []
class_nodes: list[Node] = []
+ function_nodes: list[Node] = []
rust_ranges: list[tuple[int, int]] = []
io_bound: dict[str, str] = {}
io_rebound: set[str] = set()
eh_kinds = _eh_node_kinds(language, lmap)
class_kinds = lmap.class_kinds
+ function_kinds = lmap.function_kinds
is_rust = language == "rust"
stack: list[tuple[Node, int]] = [(root, 0)]
@@ -115,6 +120,8 @@ def scan_file(
_eh_visit(node, language, lmap, hits)
if class_kinds and t in class_kinds and node.is_named:
class_nodes.append(node)
+ if t in function_kinds:
+ function_nodes.append(node)
if (
is_rust
and not ctx & _IN_RUST_TEST
@@ -168,6 +175,7 @@ def scan_file(
lines=CodeLineIndex(nonblank, file_rows, code_rows, leaves, excluded),
error_handling_hits=hits,
class_nodes=class_nodes,
+ function_nodes=function_nodes,
rust_test_line_ranges=tuple(rust_ranges),
io_names=_finish_io_names(io_bound, io_rebound),
)
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
index a64e457e4..de1117f44 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -333,6 +333,15 @@ class LanguageNodeMap:
# function's body never meet one.
misread_scope: Callable[[Node], object] | None = None
+ # -- Local bindings ------------------------------------------------------
+ # * ``local_value`` -- truthy for a ``function_kinds`` node that binds a
+ # value and not a function (F#: ``let v = ...`` shares its node type
+ # with ``let f x = ...``). Inside a function such a node is no unit of
+ # its own, so it gets no entry there. None means every function-kind
+ # node is a function. Checked only for nodes inside a collected
+ # function; at module level the node keeps the entry it always had.
+ local_value: Callable[[Node], object] | None = None
+
# Scalar type names C and C++ share, Win32's typedefs included because C/C++ on
@@ -1159,12 +1168,25 @@ _PASCAL = LanguageNodeMap(
# until function kinds can carry a text predicate, no entry and no rows.
+def _fsharp_binds_a_value(node: Node) -> bool:
+ """``let v = ...`` as opposed to ``let f x = ...``: no parameter list."""
+ return node.type == "function_or_value_defn" and not any(
+ child.type == "function_declaration_left" for child in node.children
+ )
+
+
# Known undercount, in the walker rather than this map: ``walk_file`` walks the
# body node's children, so a body that IS one expression loses it and
# ``let f x = if x > 0 then 1 else 2`` scores 1.
+#
+# A second one: a value bound inside a function (``let v = if a then 1 else
+# 2``) is a ``function_or_value_defn`` like a function, so the enclosing
+# function's walk stops at it, and ``local_value`` keeps it from getting an
+# entry of its own. Its branches are counted nowhere.
_FSHARP = LanguageNodeMap(
function_kinds=frozenset({"function_or_value_defn", "member_defn"}),
lambda_kinds=frozenset(),
+ local_value=_fsharp_binds_a_value,
branch_kinds=frozenset({"if_expression"}),
loop_kinds=frozenset({"for_expression", "while_expression"}),
try_kinds=frozenset({"try_expression"}),
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.py
index 03e352696..8f68cdf52 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py
@@ -95,9 +95,10 @@ class FunctionComplexity:
is_test_case: bool = False
# Every name called in this function's body, lowercased, **including**
# nested function bodies -- where it parts company with the counts above,
- # which stop at one. A function nested inside an already-collected one is
- # never collected as an entry of its own, so stopping would attribute its
- # calls to nobody. Empty for a language with no assertion
+ # which stop at one. A nested function has an entry of its own, but a test
+ # that hands an inline helper to something else never calls it by name, so
+ # stopping would put the helper's calls out of the test's reach. Empty for
+ # a language with no assertion
# vocabulary row, because it rides on that traversal. It answers one
# question: did this function hand its work to something else in the file?
called_names: frozenset[str] = frozenset()
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/walker.py b/packages/core/src/repowise/core/analysis/health/complexity/walker.py
index b8b732d6c..f29c8ad3f 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/walker.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/walker.py
@@ -53,6 +53,7 @@ from .ast_utils import (
_collect_function_nodes,
_count_parameters,
_find_function_entry_name,
+ _nested_function_nodes,
_parameter_list,
)
from .class_analysis import _collect_classes
@@ -177,7 +178,15 @@ def walk_file(
scan = scan_file(tree.root_node, language, lmap, source, io_names=run_perf)
flags = module_false_constants(tree.root_node, source, language)
facts_of = _facts_reader(abs_path, language, lmap)
- for fn_node in _collect_function_nodes(tree.root_node, lmap):
+ fn_nodes = _collect_function_nodes(tree.root_node, lmap)
+ # Nested functions go first, and the rows there have always been follow in
+ # the order they had. Symbol complexity, the stored blame row and the
+ # commit spans key a file's rows by name and keep the last one, so a
+ # nested function must not come after a function of the file's own level
+ # that shares its name.
+ nested = _nested_function_nodes(fn_nodes, scan.function_nodes, lmap)
+ first_top_level = len(nested)
+ for index, fn_node in enumerate(nested + fn_nodes):
body = fn_node.child_by_field_name("body") or fn_node
deepest: list[int] = []
start_facts, finish_facts = facts_of(fn_node)
@@ -216,7 +225,8 @@ def walk_file(
verification_count=verifications,
raise_count=raises,
mock_setup_count=_count_mock_setup(fn_node, body, lmap, mock_dialect, asserts),
- is_test_case=is_test_case(fn_node, name, language),
+ # No runner collects a function defined inside another one.
+ is_test_case=index >= first_top_level and is_test_case(fn_node, name, language),
called_names=called,
bare_called_names=bare_called,
dispatch_share=dispatch_share(dispatch.points, ccn),
diff --git a/tests/unit/health/test_assertion_free_test.py b/tests/unit/health/test_assertion_free_test.py
index 7cd70d997..e7699a59e 100644
--- a/tests/unit/health/test_assertion_free_test.py
+++ b/tests/unit/health/test_assertion_free_test.py
@@ -852,11 +852,11 @@ def test_an_expect_bound_to_a_const_still_counts_as_checking_something() -> None
def test_an_assertion_inside_a_nested_function_helper_is_not_lost() -> None:
- """A function nested in a test body is collected as nobody's entry.
+ """The test hands its inline helper on and never calls it by name.
- ``_collect_function_nodes`` does not descend past a function, so an inline
- ``function`` helper is never a walked function of its own, and the counts
- stop at it. Its calls would otherwise be recorded nowhere at all."""
+ The counts stop at the nested ``function``, whose assertions are its own
+ row's, so the test is answered by the helper's calls rolling up into its
+ own ``called_names``."""
_require("typescript")
src = (
"it('checks in a helper', () => {\n"
diff --git a/tests/unit/health/test_mock_saturated_test.py b/tests/unit/health/test_mock_saturated_test.py
index e4c5ef751..c9af405fb 100644
--- a/tests/unit/health/test_mock_saturated_test.py
+++ b/tests/unit/health/test_mock_saturated_test.py
@@ -174,9 +174,8 @@ def test_double(monkeypatch):
def test_nested_function_setup_does_not_count_against_its_parent():
"""A closure's doubles belong to the closure, not the test around it.
- The walker records no row for a function nested inside a function body, so
- the setup is not attributed anywhere - which is the conservative direction:
- it can only ever suppress a finding, never invent one.
+ The nested function has a row of its own (#3281) and the setup is counted
+ there. It asserts nothing and is not a test case, so no finding comes of it.
"""
counts = _walk(
"""
@@ -192,7 +191,7 @@ def test_outer():
"""
)
assert counts["test_outer"] == (0, 1)
- assert "helper" not in counts
+ assert counts["helper"] == (2, 0)
def test_setup_inside_a_nested_block_is_counted():
diff --git a/tests/unit/health/test_nested_function_complexity.py b/tests/unit/health/test_nested_function_complexity.py
new file mode 100644
index 000000000..1889ce7b5
--- /dev/null
+++ b/tests/unit/health/test_nested_function_complexity.py
@@ -0,0 +1,754 @@
+"""A function defined inside another function is measured, once, as its own entry.
+
+Before #3281 the collector stopped at the first function it met and the outer
+function's walk skipped any function-kind node below it, so a nested ``def``,
+a function expression, a local function or a method of a local or anonymous
+class was counted nowhere: not in an entry of its own and not in its parent's.
+
+Every nested body here is the same three nested ``if``s, which on its own is
+CCN 4 and nesting 3, and every outer function is otherwise empty, CCN 1 and
+nesting 0. Lambdas are the control: they belong to the function that holds
+them and get no entry, before and after.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.health.complexity import walk_file
+from repowise.core.analysis.health.complexity.ast_utils import _collect_function_nodes
+from repowise.core.analysis.health.complexity.languages import get_language_map
+
+_C3 = "if (a) { if (b) { if (c) { } } }"
+_RS3 = "if a { if b { if c { } } }"
+
+
+def _walk(path: str, language: str, source: str):
+ try:
+ from repowise.core.ingestion.parser import _get_language, grammar_tag_for
+ except Exception:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ if _get_language(grammar_tag_for(language, path)) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ return walk_file(path, language, source.encode())
+
+
+def _shape(fcx) -> dict[str, tuple[int, int]]:
+ """``name -> (ccn, max_nesting)``; fails on a name reported twice."""
+ names = [fc.name for fc in fcx.functions]
+ assert len(names) == len(set(names)), names
+ return {fc.name: (fc.ccn, fc.max_nesting) for fc in fcx.functions}
+
+
+# (id, path, language, source, name of the outer entry, name of the nested entry)
+_NESTED = [
+ (
+ "python-def",
+ "a.py",
+ "python",
+ "def outer():\n"
+ " def inner():\n"
+ " if a:\n"
+ " if b:\n"
+ " if c:\n"
+ " pass\n"
+ " return inner\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "python-async-def",
+ "a.py",
+ "python",
+ "async def outer():\n"
+ " async def inner():\n"
+ " if a:\n"
+ " if b:\n"
+ " if c:\n"
+ " pass\n"
+ " return inner\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "python-local-class-method",
+ "a.py",
+ "python",
+ "def outer():\n"
+ " class K:\n"
+ " def m(self):\n"
+ " if a:\n"
+ " if b:\n"
+ " if c:\n"
+ " pass\n"
+ " return K\n",
+ "outer",
+ "m",
+ ),
+ (
+ "javascript-declaration",
+ "a.js",
+ "javascript",
+ f"function outer(){{ function inner(){{ {_C3} }} }}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "javascript-function-expression",
+ "a.js",
+ "javascript",
+ f"function outer(){{ const h = function(){{ {_C3} }}; }}\n",
+ "outer",
+ "<anonymous>",
+ ),
+ (
+ "javascript-generator",
+ "a.js",
+ "javascript",
+ f"function outer(){{ function* g(){{ {_C3} }} }}\n",
+ "outer",
+ "g",
+ ),
+ (
+ "javascript-object-method",
+ "a.js",
+ "javascript",
+ f"function outer(){{ return {{ m(){{ {_C3} }} }}; }}\n",
+ "outer",
+ "m",
+ ),
+ (
+ "javascript-local-class-method",
+ "a.js",
+ "javascript",
+ f"function outer(){{ class K {{ m(){{ {_C3} }} }} return K; }}\n",
+ "outer",
+ "m",
+ ),
+ (
+ "typescript-declaration",
+ "a.ts",
+ "typescript",
+ f"function outer(): void {{ function inner(): void {{ {_C3} }} }}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "typescript-inside-a-method",
+ "a.ts",
+ "typescript",
+ f"class C {{ m() {{ function inner() {{ {_C3} }} }} }}\n",
+ "m",
+ "inner",
+ ),
+ (
+ "java-anonymous-class",
+ "A.java",
+ "java",
+ "class A { void outer() { Runnable r = new Runnable() {"
+ f" public void run() {{ {_C3} }} }}; }} }}\n",
+ "outer",
+ "run",
+ ),
+ (
+ "java-local-class",
+ "A.java",
+ "java",
+ f"class A {{ void outer() {{ class L {{ void m() {{ {_C3} }} }} }} }}\n",
+ "outer",
+ "m",
+ ),
+ (
+ "rust-fn-in-fn",
+ "a.rs",
+ "rust",
+ f"fn outer() {{\n fn inner() {{\n {_RS3}\n }}\n}}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "rust-impl-in-fn",
+ "a.rs",
+ "rust",
+ f"fn outer() {{\n struct S;\n impl S {{ fn m(&self) {{ {_RS3} }} }}\n}}\n",
+ "outer",
+ "m",
+ ),
+ (
+ "kotlin-local-fun",
+ "a.kt",
+ "kotlin",
+ f"fun outer() {{\n fun inner() {{\n {_C3}\n }}\n}}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "kotlin-object-expression",
+ "a.kt",
+ "kotlin",
+ "fun outer() {\n"
+ " val r = object : Runnable {\n"
+ f" override fun run() {{ {_C3} }}\n"
+ " }\n"
+ "}\n",
+ "outer",
+ "run",
+ ),
+ (
+ "cpp-local-class",
+ "a.cpp",
+ "cpp",
+ f"void outer() {{ struct L {{ void m() {{ {_C3} }} }}; }}\n",
+ "outer",
+ "m",
+ ),
+ (
+ "c-nested-function",
+ "a.c",
+ "c",
+ f"void outer() {{ void inner() {{ {_C3} }} }}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "csharp-local-function",
+ "A.cs",
+ "csharp",
+ f"class A {{ void Outer() {{ void Inner() {{ {_C3} }} }} }}\n",
+ "Outer",
+ "Inner",
+ ),
+ (
+ "dart-local-function",
+ "a.dart",
+ "dart",
+ f"void outer() {{ void inner() {{ {_C3} }} }}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "scala-nested-def",
+ "a.scala",
+ "scala",
+ "object O {\n"
+ " def outer(): Unit = {\n"
+ " def inner(): Unit = {\n"
+ f" {_C3}\n"
+ " }\n"
+ " }\n"
+ "}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "ruby-nested-def",
+ "a.rb",
+ "ruby",
+ "def outer\n"
+ " def inner\n"
+ " if a\n"
+ " if b\n"
+ " if c\n"
+ " end\n"
+ " end\n"
+ " end\n"
+ " end\n"
+ "end\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "shell-nested-function",
+ "a.sh",
+ "shell",
+ "outer() {\n inner() {\n if a; then if b; then if c; then :; fi; fi; fi\n }\n}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "pascal-nested-procedure",
+ "a.pas",
+ "pascal",
+ "unit a;\n"
+ "interface\n"
+ "implementation\n"
+ "procedure Outer;\n"
+ " procedure Inner;\n"
+ " begin\n"
+ " if a then if b then if c then x := 1;\n"
+ " end;\n"
+ "begin\n"
+ "end;\n"
+ "end.\n",
+ "Outer",
+ "Inner",
+ ),
+ (
+ "php-nested-function",
+ "a.php",
+ "php",
+ f"<?php\nfunction outer() {{ function inner() {{ {_C3} }} }}\n",
+ "outer",
+ "inner",
+ ),
+ (
+ "php-anonymous-class",
+ "a.php",
+ "php",
+ f"<?php\nfunction outer() {{ return new class {{ function m() {{ {_C3} }} }}; }}\n",
+ "outer",
+ "m",
+ ),
+]
+
+
+ ("path", "language", "source", "outer", "nested"),
+ [pytest.param(*row[1:], id=row[0]) for row in _NESTED],
+)
+def test_a_nested_function_is_an_entry_of_its_own(path, language, source, outer, nested):
+ shape = _shape(_walk(path, language, source))
+
+ assert shape == {outer: (1, 0), nested: (4, 3)}
+
+
+# (id, path, language, source, name of the one entry)
+_LAMBDAS = [
+ (
+ "javascript-arrow",
+ "a.js",
+ "javascript",
+ f"function outer(){{ const h = () => {{ {_C3} }}; }}\n",
+ "outer",
+ ),
+ (
+ "python-lambda",
+ "a.py",
+ "python",
+ "def outer():\n return lambda x: 1 if x else (2 if a else (3 if b else 4))\n",
+ "outer",
+ ),
+ (
+ "go-func-literal",
+ "a.go",
+ "go",
+ f"package m\nfunc outer() {{\n\th := func() {{\n\t\t{_RS3}\n\t}}\n\t_ = h\n}}\n",
+ "outer",
+ ),
+ (
+ "java-lambda",
+ "A.java",
+ "java",
+ f"class A {{ void outer() {{ Runnable r = () -> {{ {_C3} }}; }} }}\n",
+ "outer",
+ ),
+ (
+ "rust-closure",
+ "a.rs",
+ "rust",
+ f"fn outer() {{\n let h = || {{ {_RS3} }};\n}}\n",
+ "outer",
+ ),
+ (
+ "kotlin-lambda",
+ "a.kt",
+ "kotlin",
+ f"fun outer() {{\n val h = {{ {_C3} }}\n}}\n",
+ "outer",
+ ),
+ (
+ "cpp-lambda",
+ "a.cpp",
+ "cpp",
+ f"void outer() {{ auto h = [&]() {{ {_C3} }}; }}\n",
+ "outer",
+ ),
+ (
+ "csharp-lambda",
+ "A.cs",
+ "csharp",
+ f"class A {{ void Outer() {{ System.Action h = () => {{ {_C3} }}; }} }}\n",
+ "Outer",
+ ),
+ (
+ "dart-closure",
+ "a.dart",
+ "dart",
+ f"void outer() {{ var h = () {{ {_C3} }}; }}\n",
+ "outer",
+ ),
+ (
+ "php-closure",
+ "a.php",
+ "php",
+ f"<?php\nfunction outer() {{ $h = function() {{ {_C3} }}; }}\n",
+ "outer",
+ ),
+]
+
+
+ ("path", "language", "source", "outer"),
+ [pytest.param(*row[1:], id=row[0]) for row in _LAMBDAS],
+)
+def test_a_lambda_still_belongs_to_the_function_that_holds_it(path, language, source, outer):
+ shape = _shape(_walk(path, language, source))
+
+ assert shape == {outer: (4, 3)}
+
+
+def test_each_level_of_nesting_is_counted_once():
+ source = (
+ "def outer():\n"
+ " if a:\n"
+ " pass\n"
+ " def middle():\n"
+ " if b:\n"
+ " if c:\n"
+ " pass\n"
+ " def innermost():\n"
+ " for x in xs:\n"
+ " if d:\n"
+ " if e:\n"
+ " pass\n"
+ " return innermost\n"
+ " return middle\n"
+ )
+
+ fcx = _walk("a.py", "python", source)
+
+ assert _shape(fcx) == {"outer": (2, 1), "middle": (3, 2), "innermost": (4, 3)}
+ # Six branch points in the file, each in exactly one entry.
+ assert sum(fc.ccn - 1 for fc in fcx.functions) == 6
+
+
+def test_a_function_inside_a_lambda_inside_a_function():
+ """The arrow's own branch stays with ``outer``; ``deep`` is its own entry."""
+ source = f"function outer(){{ const h = () => {{ if (z) {{ }} function deep(){{ {_C3} }} }}; }}\n"
+
+ assert _shape(_walk("a.js", "javascript", source)) == {"outer": (2, 1), "deep": (4, 3)}
+
+
+def test_a_function_inside_a_module_level_lambda():
+ source = f"const top = () => {{ if (z) {{ }} function inner(){{ {_C3} }} }};\n"
+
+ assert _shape(_walk("a.js", "javascript", source)) == {"top": (2, 1), "inner": (4, 3)}
+
+
+def test_a_lambda_inside_a_nested_function_belongs_to_the_nested_function():
+ source = (
+ "function outer(){\n"
+ " function inner(){\n"
+ f" const h = () => {{ {_C3} }};\n"
+ " }\n"
+ "}\n"
+ )
+
+ assert _shape(_walk("a.js", "javascript", source)) == {"outer": (1, 0), "inner": (4, 3)}
+
+
+def test_a_function_inside_a_test_callback():
+ """The suite callback stays transparent, the case is an entry, and the
+ helper declared inside the case is one too."""
+ source = (
+ "describe('suite', () => {\n"
+ " it('case', () => {\n"
+ f" function helper(){{ {_C3} }}\n"
+ " expect(helper()).toBe(1);\n"
+ " });\n"
+ "});\n"
+ )
+
+ fcx = _walk("a.test.js", "javascript", source)
+
+ assert _shape(fcx) == {"it callback": (1, 0), "helper": (4, 3)}
+ by_name = {fc.name: fc for fc in fcx.functions}
+ assert by_name["it callback"].is_test_case
+ assert not by_name["helper"].is_test_case
+
+
+def test_the_nested_entry_has_its_own_lines_and_the_outer_keeps_its_own():
+ source = (
+ "def outer(p, q):\n"
+ " x = 1\n"
+ " def inner(r):\n"
+ " if r:\n"
+ " return 1\n"
+ " return 2\n"
+ " return inner\n"
+ )
+
+ by_name = {fc.name: fc for fc in _walk("a.py", "python", source).functions}
+
+ outer, inner = by_name["outer"], by_name["inner"]
+ assert (outer.start_line, outer.end_line, outer.param_count) == (1, 7, 2)
+ assert (inner.start_line, inner.end_line, inner.param_count) == (3, 6, 1)
+ # The outer function is still as long as it is written.
+ assert outer.nloc == 6
+ assert inner.nloc == 3
+
+
+def test_the_entries_there_have_always_been_come_last_in_the_order_they_had():
+ source = (
+ "def first():\n"
+ " def nested_one():\n"
+ " pass\n"
+ "def second():\n"
+ " pass\n"
+ "class K:\n"
+ " def third(self):\n"
+ " def nested_two():\n"
+ " pass\n"
+ )
+ without = (
+ "def first():\n pass\ndef second():\n pass\n"
+ "class K:\n def third(self):\n pass\n"
+ )
+
+ with_nested = [fc.name for fc in _walk("a.py", "python", source).functions]
+ plain = [fc.name for fc in _walk("a.py", "python", without).functions]
+
+ assert with_nested[-len(plain) :] == plain
+ assert sorted(with_nested[: -len(plain)]) == ["nested_one", "nested_two"]
+
+
+class TestANestedFunctionThatSharesAName:
+ """Three consumers key a file's rows by name and keep the last one."""
+
+ SOURCE = (
+ "def close():\n"
+ " if a:\n"
+ " pass\n"
+ "def open_all():\n"
+ " def close():\n"
+ " if b:\n"
+ " if c:\n"
+ " if d:\n"
+ " pass\n"
+ " return close\n"
+ )
+
+ def test_the_last_row_of_a_name_is_the_function_at_the_file_s_own_level(self):
+ functions = _walk("a.py", "python", self.SOURCE).functions
+
+ by_name = {fc.name: fc for fc in functions}
+
+ assert sorted((fc.name, fc.start_line, fc.ccn) for fc in functions) == [
+ ("close", 1, 2),
+ ("close", 5, 4),
+ ("open_all", 4, 1),
+ ]
+ assert (by_name["close"].start_line, by_name["close"].ccn) == (1, 2)
+
+ def test_a_symbol_matched_by_name_takes_the_top_level_function_s_complexity(self):
+ """A symbol whose lines match no row falls back to the name."""
+ from types import SimpleNamespace
+
+ from repowise.core.analysis.health.engine import HealthAnalyzer
+
+ functions = _walk("a.py", "python", self.SOURCE).functions
+ by_lines = SimpleNamespace(name="close", start_line=5, end_line=9, complexity_estimate=None)
+ by_name = SimpleNamespace(name="close", start_line=0, end_line=3, complexity_estimate=None)
+
+ HealthAnalyzer._populate_symbol_complexity(
+ None, SimpleNamespace(symbols=[by_lines, by_name]), functions
+ )
+
+ assert by_lines.complexity_estimate == 4
+ assert by_name.complexity_estimate == 2
+
+ def test_the_commit_span_of_a_name_is_the_top_level_function_s(self):
+ from repowise.core.analysis.health.function_blame_rollup import commit_spans
+
+ functions = _walk("a.py", "python", self.SOURCE).functions
+ entries = [(fc.name, fc.start_line, fc.end_line, [f"sha{fc.start_line}"]) for fc in functions]
+
+ spans = commit_spans(functions, entries)
+
+ assert (1, 3, frozenset({"sha1"})) in spans
+ assert all(start != 5 for start, _, _ in spans)
+
+
+def test_a_function_named_like_a_test_inside_a_function_is_not_a_test_case():
+ """pytest collects module-level functions and methods of test classes,
+ never a function defined inside another one."""
+ source = (
+ "def make():\n"
+ " def test_inner():\n"
+ " return 1\n"
+ " return test_inner\n"
+ "def test_outer():\n"
+ " assert make()\n"
+ )
+
+ by_name = {fc.name: fc for fc in _walk("test_a.py", "python", source).functions}
+
+ assert by_name["test_outer"].is_test_case
+ assert not by_name["test_inner"].is_test_case
+
+
+def test_a_junit_annotation_inside_a_method_does_not_make_a_test_case():
+ source = (
+ "class ATest {\n"
+ " @Test void outer() {\n"
+ " Object o = new Object() { @Test void inner() { } };\n"
+ " }\n"
+ "}\n"
+ )
+
+ by_name = {fc.name: fc for fc in _walk("ATest.java", "java", source).functions}
+
+ assert by_name["outer"].is_test_case
+ assert not by_name["inner"].is_test_case
+
+
+class TestATestThatChecksThroughANestedHelper:
+ """``assertion_free_test`` resolves helpers by name, over the whole file."""
+
+ @staticmethod
+ def _flagged(source: str) -> list[str]:
+ from repowise.core.analysis.health.biomarkers.assertion_free_test import BIOMARKER
+ from repowise.core.analysis.health.biomarkers.base import FileContext
+
+ path = "tests/test_sample.py"
+ fcx = _walk(path, "python", source)
+ ctx = FileContext(
+ file_path=path,
+ language="python",
+ nloc=fcx.file_nloc,
+ has_test_file=True,
+ module=None,
+ all_functions=tuple(fcx.functions),
+ )
+ return sorted(finding.function_name for finding in BIOMARKER.detect(ctx))
+
+ def test_a_test_that_calls_its_nested_asserting_helper_checks_something(self):
+ """``assert`` is a statement, so nothing rolled up into the test: it
+ was reported as asserting nothing."""
+ source = "def test_a():\n def check(v):\n assert v\n check(run())\n"
+
+ assert self._flagged(source) == []
+
+ def test_a_helper_that_is_handed_on_and_never_called_does_not_answer(self):
+ source = "def test_a():\n def check(v):\n assert v\n registry.add(check)\n"
+
+ assert self._flagged(source) == ["test_a"]
+
+ def test_a_nested_helper_that_checks_nothing_does_not_answer(self):
+ source = "def test_a():\n def prepare(v):\n return v\n prepare(run())\n"
+
+ assert self._flagged(source) == ["test_a"]
+
+ def test_the_name_answers_for_the_whole_file(self):
+ """The same rule as a helper on one class answering for a same-named
+ helper on another: it can hide a finding and never invents one."""
+ source = (
+ "def test_a():\n"
+ " def check(v):\n"
+ " assert v\n"
+ " check(1)\n"
+ "\n"
+ "def test_b():\n"
+ " check(2)\n"
+ "\n"
+ "def test_c():\n"
+ " other(3)\n"
+ )
+
+ assert self._flagged(source) == ["test_c"]
+
+
+class TestLocalClasses:
+ def test_a_local_class_has_its_methods(self):
+ """It was reported as a class with no methods at all."""
+ source = (
+ "def outer():\n"
+ " class K:\n"
+ " def m(self):\n"
+ " self.x = 1\n"
+ " def n(self):\n"
+ " return self.x\n"
+ " return K\n"
+ )
+
+ fcx = _walk("a.py", "python", source)
+
+ assert [(c.name, c.method_count) for c in fcx.classes] == [("K", 2)]
+ assert sorted(fc.name for fc in fcx.functions) == ["m", "n", "outer"]
+
+ def test_the_enclosing_class_does_not_take_the_local_class_s_methods(self):
+ source = (
+ "class A {\n"
+ " void one() { class L { void m() { } void n() { } } }\n"
+ " void two() { Runnable r = new Runnable() { public void run() { } }; }\n"
+ "}\n"
+ )
+
+ fcx = _walk("A.java", "java", source)
+
+ assert {c.name: c.method_count for c in fcx.classes} == {"A": 2, "L": 2}
+
+
+class TestFSharp:
+ """``let f x = ...`` and ``let v = ...`` are one node type in F#."""
+
+ SOURCE = (
+ "module M\n"
+ "let top = 1\n"
+ "let outer x =\n"
+ " let v = if a then 1 else 2\n"
+ " let inner y =\n"
+ " if y then (if a then 1 else 2) else 3\n"
+ " let (p, q) = x\n"
+ " inner v\n"
+ )
+
+ def test_a_local_function_is_an_entry(self):
+ names = [fc.name for fc in _walk("a.fs", "fsharp", self.SOURCE).functions]
+
+ assert "inner" in names
+
+ def test_a_local_value_is_not(self):
+ fcx = _walk("a.fs", "fsharp", self.SOURCE)
+
+ # ``outer`` (3), ``inner`` (5) and the module-level value (2), which
+ # had an entry before and keeps it; nothing for lines 4 and 7.
+ assert sorted(fc.start_line for fc in fcx.functions) == [2, 3, 5]
+
+
+class TestTheOtherCallersOfTheCollector:
+ """Dataflow, gating and the perf passes still get the list they had."""
+
+ SOURCE = (
+ b"def outer():\n"
+ b" def inner():\n"
+ b" pass\n"
+ b" h = lambda: 1\n"
+ b" return inner\n"
+ b"top = lambda: 2\n"
+ )
+
+ def _root(self):
+ from tree_sitter import Parser
+
+ from repowise.core.ingestion.parser import _get_language
+
+ grammar = _get_language("python")
+ if grammar is None:
+ pytest.skip("tree-sitter language pack missing for python")
+ return Parser(grammar).parse(self.SOURCE).root_node
+
+ def test_the_shared_collector_returns_nothing_below_a_function(self):
+ nodes = _collect_function_nodes(self._root(), get_language_map("python"))
+
+ assert sorted((node.start_point[0] + 1, node.type) for node in nodes) == [
+ (1, "function_definition"),
+ (6, "lambda"),
+ ]
+
+ def test_the_dataflow_pass_sees_the_same_functions_as_before(self):
+ from repowise.core.analysis.health.dataflow.analyze import analyze_file
+
+ result = analyze_file("a.py", "python", self.SOURCE, flagged_only=False)
+
+ assert result.stats.functions_seen == 2