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
feat(health): dataflow def/use dialect for Scala (#3580)
Aman690fdb02a34c
diff --git a/ROADMAP.md b/ROADMAP.mdindex c28d22c06..dc32e2cdd 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -53,7 +53,7 @@ ladder covers **42 languages, 28 of them parsed to a full AST**.|---|---|---|| COBOL | Planned | Copybook resolution, source-format normalization, broader dialect coverage, and health markers || Vue, Svelte | In development | Options-API members, `{#each}` and `v-for` head bindings, `.svelte.ts` rune modules |-| C#, Scala, Ruby | Planned | Dataflow dialects, which is what Extract Method needs in order to lift a span safely |+| Ruby | Planned | Dataflow dialects, which is what Extract Method needs in order to lift a span safely || Kotlin | Exploring | Dataflow is blocked on the grammar and needs either a grammar upgrade or a text-based jump seam. We would rather say blocked than planned. || C | Planned | It shares the C++ grammar for parsing but reaches no health walker map, so it gets graph coverage without markers || SQL / dbt | Planned | Column-level blast radius |diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.mdindex ab8d3e549..680eda1ea 100644--- a/docs/layers/LANGUAGE_SUPPORT.md+++ b/docs/layers/LANGUAGE_SUPPORT.md@@ -270,7 +270,8 @@ This table is why a language is Full and not Good.| Java | ✅ | ✅ | ✅ | no | no | ✅ | ✅ || Go | ✅ | n/a | ✅ | no | no | ✅ | ✅ || Rust, C++, C# | ✅ | ✅ | ✅ | no | no | ✅ | ✅ |-| Scala, Ruby | ✅ | ✅ | ✅ | no | no | no | ✅ |+| Scala | ✅ | ✅ | ✅ | no | no | ✅ | ✅ |+| Ruby | ✅ | ✅ | ✅ | no | no | no | ✅ || Kotlin | ✅ | ✅ | ✅ | no | no | no | ✅ || Dart | ✅ | ✅ | ✅ | no | no | no | ✅ || Object Pascal | ✅ | n/a | ✅ | n/a | n/a | no | ✅ |@@ -319,7 +320,7 @@ The full list per language, with the mechanics behind each gap, is in|----------|------|| Vue, Svelte | Template binding forms (`v-for`, `{#each}`) || Kotlin | Extract Method, which needs a grammar that labels control flow |-| Scala, Ruby | Extract Method (dataflow dialect) |+| Ruby | Extract Method (dataflow dialect) || GDScript | Health markers (complexity, performance, dataflow) || Object Pascal | A dedicated `uses` resolver || COBOL | Dialect coverage, health markers beyond complexity |diff --git a/packages/core/src/repowise/core/analysis/health/complexity/body_facts.py b/packages/core/src/repowise/core/analysis/health/complexity/body_facts.pyindex 652a8058d..1f266a161 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/body_facts.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/body_facts.py@@ -41,6 +41,9 @@ class FactKinds:awaits: frozenset[str] = frozenset()await_scopes: frozenset[str] = frozenset()lambdas: frozenset[str] = frozenset()+ #: Exits that leave the enclosing function from inside a lambda (Scala+ #: ``return``): a jump, though the lambda is otherwise a scope of its own.+ lambda_exits: frozenset[str] = frozenset()receiver: frozenset[str] = frozenset()# Every kind above: a walk that sees every node skips the rest with one lookup.watched: frozenset[str] = field(init=False)@@ -55,6 +58,7 @@ class FactKinds:| self.awaits| self.await_scopes| self.lambdas+ | self.lambda_exits| self.receiver)object.__setattr__(self, "watched", watched)@@ -98,6 +102,7 @@ def step(tally: BodyTally, node: Node, scope: int) -> int:if t in kinds.lambdas:return scope | IN_LAMBDAif scope & IN_LAMBDA:+ tally.jump = tally.jump or t in kinds.lambda_exitsreturn scopetally.decisions += t in kinds.decisionsmacro = is_exit_macro(node, kinds.exit_macros)diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.pyindex 1df4d055d..acc06bd85 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py@@ -257,6 +257,16 @@ class LanguageNodeMap:# segment. The slicer treats them as jumps for the same reason. A user# macro of the same name that does not exit only hides a span.yield_kinds: frozenset[str] = frozenset()+ # * ``lambda_exit_kinds`` -- exit kinds that, written inside a lambda,+ # leave the *enclosing function*, not the lambda (Scala ``return`` in+ # ``xs.foreach(x => return x)`` unwinds the surrounding ``def``). A+ # lambda's own jumps are otherwise its own, so without this a span+ # holding such a lambda looks liftable and the helper would return from+ # itself. The slicer counts a hit as a jump, looking through lambda+ # bodies only and stopping at ``function_kinds`` (a ``return`` in a+ # nested ``def`` is that def's own). Empty for languages whose lambda+ # ``return`` is local to the lambda.+ lambda_exit_kinds: frozenset[str] = frozenset()# * ``await_kinds`` -- tokens that suspend the function (Python ``await`` /# ``async with`` / ``async for``, TS/JS ``await`` / ``for await``, Rust# ``.await``, C++ ``co_await``). Matched on the anonymous keyword token,@@ -1124,6 +1134,35 @@ _SCALA = LanguageNodeMap(call_kinds=frozenset({"call_expression", "instance_expression"}),# Scala has no async syntax; the perf dialect's ``is_async_fn`` sniffs a# declared ``Future[...]`` return type instead.+ #+ # Dataflow (Extract Method). ``x = e`` is an ``assignment_expression``;+ # ``val`` / ``var`` (``lazy val`` too) are the declarations. Compound+ # assignment (``x += 1``) is NOT an assignment node: it is an+ # ``infix_expression`` whose operator text ends in ``=``, the same node as+ # ``a + b``, so it cannot be an ``augmented_assign_kinds`` member (the+ # slicer would read every infix as a write) and that set stays empty; the+ # dialect finds the write by the operator text instead. ``if`` / ``match`` /+ # ``try`` / ``return`` / ``throw`` are expressions, mapped to their+ # statement-level kinds. ``break_kinds`` / ``continue_kinds`` stay empty:+ # Scala has no such keywords (``break()`` is a library call).+ assignment_kinds=frozenset({"assignment_expression"}),+ local_decl_kinds=frozenset({"val_definition", "var_definition"}),+ if_kinds=frozenset({"if_expression"}),+ # ``indented_block`` is the Scala 3 brace-less ``block``.+ block_kinds=frozenset({"block", "indented_block"}),+ return_kinds=frozenset({"return_expression"}),+ raise_kinds=frozenset({"throw_expression"}),+ # Scala is expression-oriented: a block's last expression is its value, so+ # a span ending on it would drop that value when lifted. A non-empty+ # ``statement_wrapper_kinds`` is the slicer's marker for such a grammar and+ # makes it refuse those spans. tree-sitter-scala wraps no statement, so the+ # kind named here is one it never emits, which keeps the CFG builder's+ # unwrapping inert. Ceiling: any block whose last statement is not a+ # ``val`` / ``var`` is taken to carry a value (a ``Unit`` loop included).+ statement_wrapper_kinds=frozenset({"expression_statement"}),+ # A ``return`` inside a lambda is a non-local return: it exits the+ # enclosing ``def``, so it keeps the span from being lifted.+ lambda_exit_kinds=frozenset({"return_expression"}),)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/__init__.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/__init__.pyindex 9b4494419..f805cf6c2 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/__init__.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/__init__.py@@ -22,6 +22,7 @@ from . import java as _javafrom . import php as _phpfrom . import python as _pythonfrom . import rust as _rust+from . import scala as _scalafrom . import ts_js as _ts_jsfrom .base import (DEFUSE_DIALECTS,@@ -50,6 +51,7 @@ _REGISTER: tuple[tuple[str, BaseDefUseDialect], ...] = (("rust", _rust.DIALECT),("cpp", _cpp.DIALECT),("csharp", _csharp.DIALECT),+ ("scala", _scala.DIALECT),("razor", _csharp.DIALECT),# A C parse tree carries no C++-only node (``for_range_loop``, lambdas,# ``co_await``), so the C++ dialect reads it whole.diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/scala.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/scala.pynew file mode 100644index 000000000..c6f145891--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/scala.py@@ -0,0 +1,547 @@+"""Scala ``DefUseDialect``.++Classifies each identifier in a statement as a write (def) or a read (use).+Scala is expression-oriented, so the write sites are spread over several node+shapes:++* ``val_definition`` / ``var_definition`` (``lazy val`` too): the ``pattern``+ field binds one name (``identifier``) or several (``tuple_pattern``,+ ``case_class_pattern``, ``x @ p``).+* ``assignment_expression``: ``x = ...``. ``this.n = ...`` / ``obj.f = ...`` /+ ``arr(i) = ...`` bind no *local*, so their bases are reads. The same node is+ a named argument inside an ``arguments`` list (``p.copy(a = 2)``): there only+ the value is read and ``a`` is neither a def nor a use.+* Compound assignment (``x += 1``) has NO node of its own in tree-sitter-scala:+ it is an ``infix_expression`` whose ``operator`` is an ``operator_identifier``+ spelling ``+=``. The write is therefore found by the operator text, never by+ node type (``a + b``, ``a <= b`` and ``a == b`` are the same node type and+ write nothing). An operator is an assignment operator when it ends in ``=``,+ does not start with ``=`` and is not ``<=`` / ``>=`` / ``!=`` (SLS 6.12.4).+* ``for`` enumerators: ``i <- xs`` and ``j = i * 2`` bind their first child in+ the loop body only; a ``guard`` is a filter (reads).+* ``case_clause`` patterns bind in the arm (``typed_pattern``,+ ``tuple_pattern``, ``infix_pattern``, ...). A ``catch { case e: E => }``+ binder is bound like any other arm: ``handler_head`` reports it.++Ceilings, each of which can only make a plan *less* likely, never wrong:++* ``buf += x`` is the same node as ``count += x``. On a ``var`` of an immutable+ type it rebinds the local; on a ``ListBuffer`` it mutates the receiver and+ rebinds nothing. The two cannot be told apart without types, so the left+ name is recorded as BOTH a may-def and a use (a write that may not happen):+ it can add to what reaches a later read and hides nothing.+* An ``if`` / ``match`` / ``try`` / ``for`` / ``while`` met inside an+ expression (``val z = if (c) a else b``, a call argument) stays inside its+ CFG statement, so writes in its arms are may-defs (def and echo use), as for+ a Java ``switch``.+* ``implicit`` / ``using`` parameters and ``implicit`` locals are uses a call+ site never writes. The dialect cannot see which call needs which implicit,+ so every call in a function that has one counts as a use of each: a span+ with a call carries them as parameters. Implicits that come from the+ enclosing class or an import stay in scope for a sibling helper.+* A ``return`` inside a lambda (``xs.foreach(x => return x)``) leaves the+ enclosing ``def``. ``LanguageNodeMap.lambda_exit_kinds`` names it, so the+ slicer counts it as a jump and refuses a span holding one; a ``return`` in a+ ``def`` nested in that lambda is that ``def``'s own and does not count.++Out of scope, because each is an ordinary call to the CFG with no node kind of+its own to hook on (a span holding one can still be offered):++* ``breakable { ... break() }``: ``break()`` is a library call that throws, not+ a keyword, so ``break_kinds`` / ``continue_kinds`` stay empty.+* ``Future`` / ``Await``: Scala has no ``async`` syntax, so a span that+ suspends is not marked as needing an async helper.+"""++from __future__ import annotations++import re+from typing import TYPE_CHECKING++from .base import (+ NO_RECEIVER,+ BaseDefUseDialect,+ Captured,+ Occurrence,+ Receiver,+ StatementDefUse,+ node_text,+)++if TYPE_CHECKING:+ from tree_sitter import Node++ from ...complexity.languages import LanguageNodeMap++_VAL_KINDS = frozenset({"val_definition", "var_definition"})+_ASSIGN = "assignment_expression"+_INFIX = "infix_expression"+_CALL = "call_expression"+_ARGUMENTS = "arguments"+_ENUMERATORS = "enumerators"+_ENUMERATOR = "enumerator"+_GUARD = "guard"+_CASE_CLAUSE = "case_clause"+_CASE_BLOCK = "case_block"+_CATCH = "catch_clause"+_OPEN_BODY_KINDS = frozenset({"lambda_expression", "catch_clause", "case_block"})+_FIELD = "field_expression"++# ``x match { ... }``, ``if``, ``try``, ``for`` and ``while`` reached through an+# expression stay inside one CFG statement: their arm writes are may-defs.+_CONDITIONAL_KINDS = frozenset(+ {+ "match_expression",+ "if_expression",+ "try_expression",+ "for_expression",+ "while_expression",+ "do_while_expression",+ }+)++# Nested scopes whose identifiers belong to a different function. A+# ``template_body`` covers the members of ``new T { ... }`` inside an expression.+_SCOPE_BOUNDARIES = frozenset(+ {+ "lambda_expression",+ "function_definition",+ "class_definition",+ "object_definition",+ "trait_definition",+ "enum_definition",+ "given_definition",+ "template_body",+ }+)++# Pattern nodes whose identifier children are binders (lower-case) or stable+# identifiers / constants (upper-case, ``Nil``, ``Foo``) that are reads.+# Nodes whose named children are real statements.+_STATEMENT_CONTAINERS = frozenset(+ {"template_body", "indented_block", "case_block", "compilation_unit"}+)+# Fields through which a construct holds its arm / body.+_ARM_FIELDS = frozenset({"consequence", "alternative", "body"})++# ``this`` / ``super`` are the receiver, not variables (the Receiver contract+# carries them).+_NOT_VARIABLES = frozenset({"this", "super"})++_PATTERN_SKIP = frozenset({"type_identifier", "stable_type_identifier", "generic_type"})++# SLS 6.12.4: an assignment operator ends in ``=``, does not start with ``=``,+# and is not one of the comparison operators.+_NOT_ASSIGN_OPS = frozenset({"<=", ">=", "!=", "=="})+_ASSIGN_OP = re.compile(r"^[^=\s].*=$")+++def compound_operator(node: Node) -> str | None:+ """The ``op=`` text of an ``infix_expression`` that is a compound+ assignment, else None. ``x += 1`` -> ``+=``; ``a + b`` / ``a <= b`` ->+ None."""+ if node.type != _INFIX:+ return None+ op = node.child_by_field_name("operator")+ text = node_text(op) if op is not None else ""+ if len(text) < 2 or text in _NOT_ASSIGN_OPS or not _ASSIGN_OP.match(text):+ return None+ return text+++def _is_arm(node: Node) -> bool:+ """Whether *node* is the brace-less arm / body of a construct: held through+ ``consequence`` / ``alternative`` / ``body`` by its parent."""+ parent = node.parent+ if parent is None:+ return False+ for i, child in enumerate(parent.children):+ if child.id == node.id:+ return parent.field_name_for_child(i) in _ARM_FIELDS+ return False+++def _binds(name: str) -> bool:+ """A pattern identifier binds a variable when it starts lower-case; an+ upper-case one (``Nil``, ``Foo``) is a stable identifier, a read."""+ return bool(name) and not name[0].isupper()+++def _is_modifier(node: Node, word: bytes) -> bool:+ mods = next((c for c in node.children if c.type in ("modifiers", "access_modifier")), None)+ return mods is not None and word in (mods.text or b"").split()+++class ScalaDefUseDialect(BaseDefUseDialect):+ language = "scala"+ member_access_kinds = frozenset({_FIELD})+ receiver_write_kinds = frozenset({_ASSIGN})+ keyword_kinds = frozenset() # named arguments are an ``assignment_expression``+ enclosing_binder_kinds = frozenset({"function_definition"})++ def _is_scope_boundary(self, node: Node) -> bool:+ t = node.type+ if t in _SCOPE_BOUNDARIES:+ return True+ # ``xs.map { case n => ... }``: a partial-function literal is a closure+ # for capture purposes. A ``match`` / ``catch`` body is also a+ # ``case_block`` but runs in place, so only a call argument counts.+ if t == _CATCH:+ # ``catch_kinds`` is empty for Scala (mapping it would double-count+ # every single-case catch in the complexity numbers), so the CFG+ # builder does not see handler arms. They are read as closures+ # instead: what they read counts as a use at its line, and what+ # they write is a closure-shared write the slicer refuses to cut.+ return True+ return t == _CASE_BLOCK and node.parent is not None and node.parent.type == _CALL++ def _closure_def_use(+ self, node: Node, lmap: LanguageNodeMap+ ) -> tuple[list[Occurrence], list[Occurrence], Captured]:+ """The writes and direct reads inside a nested scope.++ A Scala ``lambda_expression`` and a partial-function ``case_block`` have+ no ``body`` field (the body is whatever follows the parameters), and a+ ``catch_clause`` is just its ``case_block``. The base fallback reads+ them as plain reads, which would hide ``xs.foreach { v => total += v }``'s+ write to ``total``: walk the body with the dialect's own statement walk.+ """+ if node.type not in _OPEN_BODY_KINDS:+ return super()._closure_def_use(node, lmap)+ params = node.child_by_field_name("parameters")+ writes: list[Occurrence] = []+ reads: list[Occurrence] = []+ inner = Captured()+ for child in node.named_children:+ if params is not None and child.id == params.id:+ continue+ self._process(child, writes, reads)+ self.collect_captured(child, inner, lmap)+ return writes, reads, inner++ # -- public contract ------------------------------------------------------++ def statement_def_use(+ self, node: Node, lmap: LanguageNodeMap, *, head_only: bool+ ) -> StatementDefUse:+ defs: list[Occurrence] = []+ uses: list[Occurrence] = []+ if head_only:+ self._head(node, lmap, defs, uses)+ else:+ owner = self._bare_arm_owner(node, lmap)+ if owner is not None:+ if owner.id == node.id: # a fragment past the first: already read+ return StatementDefUse(defs=(), uses=())+ node = owner+ self._process(node, defs, uses)+ return StatementDefUse(defs=tuple(defs), uses=tuple(uses))++ def _bare_arm_owner(self, node: Node, lmap: LanguageNodeMap) -> Node | None:+ """For a CFG "statement" that is really a fragment of a brace-less arm,+ the arm it belongs to; the arm itself for its first fragment and *node*+ for every later one. None for a real statement.++ ``if (c) errors += 1 else buf += r`` and ``def f = x + 1`` have no+ ``block``: the arm is one expression, and the CFG builder, which treats+ an arm's named children as its statements, hands over ``errors``,+ ``+=`` and ``1`` one by one. Taken apart, the write is lost. The whole+ arm is read at its first fragment instead (a nested ``if`` / ``match``+ as a may-def, so nothing becomes a definite write it is not).+ """+ parent = node.parent+ if parent is None or node.type in lmap.return_kinds | lmap.raise_kinds:+ return None+ if parent.type in lmap.block_kinds or parent.type in _STATEMENT_CONTAINERS:+ return None+ if not _is_arm(parent):+ return None+ first = next((c for c in parent.named_children if "comment" not in c.type), None)+ return parent if first is not None and first.id == node.id else node++ def parameter_defs(self, fn_node: Node) -> tuple[Occurrence, ...]:+ """Names bound by every parameter clause (``def f(a: Int)(b: Int)``),+ ``implicit`` / ``using`` clauses included, and by a lambda's bare+ parameter (``v => ...``) or parenthesised ones (``(a, b) => ...``)."""+ out: list[Occurrence] = []+ for clause in self._parameter_clauses(fn_node):+ if clause.type in self.identifier_kinds: # ``v => ...``+ out.append(self._occ(clause))+ continue+ for child in clause.named_children:+ name = child.child_by_field_name("name")+ if name is not None and name.type in self.identifier_kinds:+ out.append(self._occ(name))+ elif child.type in self.identifier_kinds:+ out.append(self._occ(child))+ return tuple(out)++ @staticmethod+ def _parameter_clauses(fn_node: Node) -> list[Node]:+ """Every ``parameters`` clause of *fn_node* (a curried ``def`` has+ several, and ``child_by_field_name`` would see only the first)."""+ clauses = []+ for i, child in enumerate(fn_node.children):+ if fn_node.field_name_for_child(i) in ("parameters", "parameter") and child.is_named:+ clauses.append(child)+ return clauses++ def implicit_names(self, fn_node: Node) -> frozenset[str]:+ """Names of the ``implicit`` / ``using`` parameters of *fn_node* and of+ the ``implicit`` / ``given`` locals in its body. A call anywhere in the+ function may consume one without naming it."""+ names: set[str] = set()+ text = fn_node.text or b""+ if b"implicit" not in text and b"using" not in text and b"given" not in text:+ return frozenset()+ for clause in self._parameter_clauses(fn_node):+ text = (clause.text or b"")[:12]+ if text.lstrip(b"( \t\n").startswith((b"implicit", b"using")):+ names |= {o.name for o in self.parameter_defs_of(clause)}+ stack = list(fn_node.named_children)+ while stack:+ cur = stack.pop()+ if cur.type in ("given_definition",):+ name = cur.child_by_field_name("name")+ if name is not None:+ names.add(node_text(name))+ elif cur.type in _VAL_KINDS and _is_modifier(cur, b"implicit"):+ names |= {o.name for o in self._pattern_names(cur.child_by_field_name("pattern"))}+ if cur.type not in ("function_definition", "lambda_expression") or cur is fn_node:+ stack.extend(cur.named_children)+ return frozenset(names)++ def parameter_defs_of(self, clause: Node) -> list[Occurrence]:+ out: list[Occurrence] = []+ for child in clause.named_children:+ name = child.child_by_field_name("name")+ if name is not None and name.type in self.identifier_kinds:+ out.append(self._occ(name))+ return out++ def _own_receiver(self, fn_node: Node, lmap: LanguageNodeMap) -> Receiver | None:+ """``this`` for a method of a class, trait, object or enum, where a bare+ name can also be a member; a top-level or local ``def`` has none. A+ lambda reaches the enclosing method's (:meth:`receiver`)."""+ if fn_node.type in lmap.lambda_kinds:+ return NO_RECEIVER+ cur = fn_node.parent+ while cur is not None:+ if cur.type in lmap.class_kinds:+ return self._receiver(lmap.self_identifiers, implicit=True)+ if cur.type in lmap.function_kinds:+ return None # a local ``def``: the outer method's receiver, unknown+ cur = cur.parent+ return NO_RECEIVER++ # -- reads ----------------------------------------------------------------++ def collect_reads(self, node: Node | None, out: list[Occurrence]) -> None:+ """Like the base collector, but a named argument (``f(key = x)``)+ contributes only its value, and a call to a plain method name does not+ read that name as a variable unless it is a bare identifier (a local+ function value is called the same way, so it is a read)."""+ if node is None:+ return+ if node.type == _ARGUMENTS:+ for arg in node.named_children:+ value = arg.child_by_field_name("right") if arg.type == _ASSIGN else arg+ if arg.type == _ASSIGN:+ self.collect_reads(value, out)+ else:+ self.collect_reads(arg, out)+ return+ if node.type in self.identifier_kinds and node_text(node) in _NOT_VARIABLES:+ return+ super().collect_reads(node, out)++ # -- head (loop clause / if condition) ------------------------------------++ def _head(+ self, node: Node, lmap: LanguageNodeMap, defs: list[Occurrence], uses: list[Occurrence]+ ) -> None:+ t = node.type+ if t == "for_expression": # ``for (i <- xs if p; j = i * 2) ...``+ # The ``(`` and ``)`` tokens carry the ``enumerators`` field too, so+ # ``child_by_field_name`` would return the paren: look by type.+ self._process(+ next((c for c in node.named_children if c.type == _ENUMERATORS), None), defs, uses+ )+ elif t in lmap.loop_kinds or t in lmap.branch_kinds or t in lmap.if_kinds:+ # while / do-while / if: the condition+ self._process(node.child_by_field_name("condition"), defs, uses)+ else:+ self._process(node, defs, uses)++ # -- the unified expression / statement walk ------------------------------++ def _process(self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ if node is None:+ return+ t = node.type+ if t in _VAL_KINDS:+ self._definition(node, defs, uses)+ return+ if t == _ASSIGN:+ self._assignment(node, defs, uses)+ return+ if t == _INFIX:+ self._infix(node, defs, uses)+ return+ if t == _CALL:+ self._call(node, defs, uses)+ return+ if t == _ARGUMENTS:+ self._arguments(node, defs, uses)+ return+ if t == _ENUMERATORS:+ for child in node.named_children:+ self._process(child, defs, uses)+ return+ if t == _ENUMERATOR:+ self._enumerator(node, defs, uses)+ return+ if t == _CASE_CLAUSE:+ self._case_clause(node, 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 name is not+ return+ if t in self.identifier_kinds:+ if node_text(node) not in _NOT_VARIABLES:+ uses.append(self._occ(node))+ return+ if self._is_scope_boundary(node):+ self.boundary_def(node, defs)+ return+ for child in node.named_children:+ self._process(child, defs, uses)++ def _definition(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """``val`` / ``var`` / ``lazy val``: bind the pattern, then walk the+ initializer (so a write nested in it is not taken for a declaration)."""+ start = len(defs)+ defs.extend(self._pattern_names(node.child_by_field_name("pattern")))+ self._declare(defs, start, node.child_by_field_name("pattern") or node)+ self._process(node.child_by_field_name("value"), defs, uses)++ def _assignment(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ left = node.child_by_field_name("left")+ self._targets(left, defs, uses)+ self._process(node.child_by_field_name("right"), defs, uses)++ def _infix(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """``a + b`` reads both sides; ``x += y`` also writes ``x`` (see the+ module doc for why the write is a may-def and a use)."""+ left = node.child_by_field_name("left")+ if compound_operator(node) is None:+ self._process(left, defs, uses)+ self._process(node.child_by_field_name("right"), defs, uses)+ return+ written: list[Occurrence] = []+ self._targets(left, written, uses)+ defs.extend(may_def for may_def in _as_may(written))+ self.collect_reads(left, uses) # the old value (and ``buf`` itself) is read+ self._process(node.child_by_field_name("right"), defs, uses)++ def _call(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """``f(a)`` / ``recv.m(a)``: the callee name is not a variable unless it+ is a bare identifier (a local function value); a receiver is a read."""+ for name in self._implicits_at(node):+ uses.append(Occurrence(name, node.start_point[0] + 1, node.start_point[1]))+ fn = node.child_by_field_name("function")+ if fn is not None:+ if fn.type in self.member_access_kinds:+ self.collect_reads(fn, uses)+ elif fn.type == _CALL: # ``f(a)(b)`` curried+ self._call(fn, defs, uses)+ elif fn.type in self.identifier_kinds and not _binds(node_text(fn)):+ pass # ``Some(x)`` / ``P(1)``: a constructor or companion ``apply``+ else:+ self._process(fn, defs, uses)+ self._process(node.child_by_field_name("arguments"), defs, uses)++ def _implicits_at(self, call: Node) -> frozenset[str]:+ """The implicit names in scope of the ``def`` that holds *call*."""+ cur = call.parent+ while cur is not None and cur.type != "function_definition":+ cur = cur.parent+ return self.implicit_names(cur) if cur is not None else frozenset()++ def _arguments(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """Arguments: ``name = value`` is a named argument (only ``value`` is a+ read, ``name`` is not a variable)."""+ for arg in node.named_children:+ if arg.type == _ASSIGN:+ self._process(arg.child_by_field_name("right"), defs, uses)+ else:+ self._process(arg, defs, uses)++ def _enumerator(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """``i <- xs`` / ``j = i * 2``: the first child binds in the loop body+ only (``declares``), the rest are reads; a ``guard`` is a filter."""+ kids = node.named_children+ if not kids:+ return+ start = len(defs)+ defs.extend(self._pattern_names(kids[0]))+ self._loop_scoped(defs, start)+ for child in kids[1:]:+ self._process(child, defs, uses)++ def _case_clause(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """A ``case`` pattern binds in its arm; the guard and body read."""+ start = len(defs)+ defs.extend(self._pattern_names(node.child_by_field_name("pattern")))+ self._loop_scoped(defs, start) # an arm-local binding: ``declares``+ for child in node.named_children:+ if child.type != "pattern" and child != node.child_by_field_name("pattern"):+ self._process(child, defs, uses)++ # -- patterns / write targets ---------------------------------------------++ def _pattern_names(self, node: Node | None) -> list[Occurrence]:+ """The variables a pattern binds: lower-case identifiers outside a type+ position. ``Nil`` / ``Foo`` / ``Some(x)``'s ``Some`` are not binders."""+ out: list[Occurrence] = []+ stack = [node] if node is not None else []+ while stack:+ cur = stack.pop()+ if cur.type in _PATTERN_SKIP:+ continue+ if cur.type in self.identifier_kinds:+ if _binds(node_text(cur)):+ out.append(self._occ(cur))+ continue+ stack.extend(reversed(cur.named_children))+ return sorted(out, key=lambda o: (o.line, o.column))++ def _targets(self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """Write targets of ``=`` / ``op=``: a bare name is a def; ``a.f`` and+ ``a(i)`` reach through ``a`` and bind no local, so ``a`` is a read."""+ if node is None:+ return+ t = node.type+ if t in self.identifier_kinds:+ defs.append(self._occ(node))+ return+ if t in (_FIELD, _CALL): # ``obj.f = ...`` / ``arr(i) = ...``+ self.collect_reads(node, uses)+ return+ if t in ("tuple_expression", "tuple_pattern"): # ``(a, b) = pair``+ for child in node.named_children:+ self._targets(child, defs, uses)+ return+ self.collect_reads(node, uses)+++def _as_may(defs: list[Occurrence]) -> list[Occurrence]:+ from .base import may_defs++ return may_defs(defs)+++DIALECT = ScalaDefUseDialect()diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.pyindex dd338612d..cdbc3e63f 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py@@ -182,6 +182,8 @@ class _Scan(NamedTuple):exit_macros: tuple[frozenset[str], frozenset[str]]awaits: tuple[frozenset[str], frozenset[str]]lambdas: frozenset[str] = frozenset()+ #: ``LanguageNodeMap.lambda_exit_kinds``: a jump when found in a lambda.+ lambda_exits: frozenset[str] = frozenset()receiver: Receiver | None = None# Built from the fields above by :func:`_scan_for`; derived when absent.kinds: FactKinds | None = None@@ -526,6 +528,7 @@ def _scan_for(lmap: LanguageNodeMap, receiver: Receiver | None, fn_node: Node) -exit_macros=_exit_macros(lmap),awaits=_awaits(lmap),lambdas=lmap.lambda_kinds,+ lambda_exits=lmap.lambda_exit_kinds,receiver=receiver,exits=lmap.return_kinds,yields=lmap.yield_kinds,@@ -630,6 +633,7 @@ def _span_kinds(scan: _Scan) -> FactKinds:awaits=await_kinds,await_scopes=await_scope_kinds,lambdas=scan.lambdas,+ lambda_exits=scan.lambda_exits,receiver=_receiver_kinds(scan.receiver) if scan.receiver is not None else frozenset(),)@@ -665,8 +669,11 @@ def _push_children(node: Node, stack: list[tuple[Node, int]], scope: int, scan: _Scan) -> None:"""Queue *node*'s children: nested scopes are skipped, except a lambda- when the receiver is looked for (it shares the instance)."""+ when the receiver is looked for (it shares the instance) or the language+ has lambda exits (a ``return`` there leaves this function). A nested+ function is never entered: its exits are its own."""+ into_lambdas = scan.receiver is not None or bool(scan.lambda_exits)for child in node.children:t = child.type- if t not in scan.scopes or (scan.receiver is not None and t in scan.lambdas):+ if t not in scan.scopes or (into_lambdas and t in scan.lambdas):stack.append((child, scope))diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.pyindex b9edb86dd..d9ed359ad 100644--- a/tests/unit/health/test_dataflow_langs.py+++ b/tests/unit/health/test_dataflow_langs.py@@ -3472,3 +3472,475 @@ def test_cpp_condition_declaration_records_use_of_initializer() -> None:while_sdu = dialect.statement_def_use(while_stmt, lmap, head_only=True)assert any(d.name == "w" for d in while_sdu.defs)assert any(u.name == "helper" for u in while_sdu.uses)+++# == Scala ======================================================================++# Every Scala write shape in one method: ``val`` / ``var`` / ``lazy val``, a tuple+# pattern, plain and compound assignment, a ``for`` head with a guard and a ``=``+# binder, a ``case`` binding, and targets that are not locals.+_SCALA_SHAPES = """+class Demo {+ var field = 0+ def shapes(input: List[Int], base: Int): Int = {+ var total = 0+ val (p, q) = (1, 2)+ lazy val w = p + q+ total = total + base+ total += input.head+ this.field = 3+ input(0) = 4+ for (i <- input if i > 0; j = i * 2) { total += j }+ val r = base match { case a if a > 1 => a; case (b, c) => b; case _ => 0 }+ total + w + r+ }+}+"""+++def test_scala_def_use_classification():+ fn = _first("scala", _SCALA_SHAPES)+ defs = {(d.var, d.declares) for d in fn.def_use.definitions}+ assert {("total", True), ("p", True), ("q", True), ("w", True), ("r", True)} <= defs+ assert {("i", True), ("j", True)} <= defs # for generator + ``=`` binder+ assert {("a", True), ("b", True), ("c", True)} <= defs # case pattern bindings+ assert {"input", "base"} <= _def_names(fn) # parameters+ # ``this.field = ...`` and ``input(0) = ...`` bind no local.+ assert "field" not in _def_names(fn)+ uses = _use_names(fn)+ assert {"input", "base", "total", "i", "w", "r"} <= uses+ assert "this" not in uses # the receiver, not a variable+++def test_scala_compound_assignment_is_found_by_operator_text():+ fn = _first(+ "scala",+ """+ class Demo {+ def f(t: Int): Int = {+ var x = 1+ x += t+ val sum = x + t+ val le = x <= t+ val ne = x != t+ x *= 2+ x+ }+ }+ """,+ )+ writes = sorted(d.line for d in fn.def_use.definitions if d.var == "x" and not d.declares)+ # Only ``x += t`` (line 5) and ``x *= 2`` (line 9): ``x + t``, ``x <= t`` and+ # ``x != t`` are the same ``infix_expression`` and write nothing.+ assert writes == [5, 9]+ # A compound write also reads the old value.+ assert any(u.name == "x" and u.line == 5 for b in fn.def_use.blocks.values() for u in b.uses)+ assert not [d for d in fn.def_use.definitions if d.var in ("sum", "le", "ne") and d.may]+++def test_scala_collection_append_is_a_may_def_and_a_use():+ # ``buf += x`` mutates a ListBuffer and rebinds nothing, but it is the same+ # node as ``count += x`` on an ``Int`` var: both are a may-def plus a use.+ fn = _first(+ "scala",+ """+ class Demo {+ def f(t: Int): Int = {+ val buf = scala.collection.mutable.ListBuffer.empty[Int]+ buf += t+ buf.size+ }+ }+ """,+ )+ assert any(d.var == "buf" and d.may for d in fn.def_use.definitions)+ assert any(u.name == "buf" and u.line == 5 for b in fn.def_use.blocks.values() for u in b.uses)+++def test_scala_named_argument_is_not_a_read_or_a_write():+ fn = _first(+ "scala",+ """+ class Demo {+ def f(p: P): Int = {+ val a = 1+ val p2 = p.copy(a = 2)+ a + p2.a+ }+ }+ """,+ )+ # ``a = 2`` is a named argument: it neither rebinds ``a`` nor reads it, and+ # ``p`` is read, not written.+ assert [d.var for d in fn.def_use.definitions if d.var == "a"] == ["a"]+ assert "p" not in {d.var for d in fn.def_use.definitions if not d.declares and d.may}+ assert "p" in _use_names(fn)+++def test_scala_lambda_does_not_leak_its_identifiers():+ fn = _first(+ "scala",+ """+ class Demo {+ def f(xs: List[Int], t: Int): Int = {+ val g = xs.map { v => v + t }+ g.sum+ }+ }+ """,+ )+ assert "v" not in _use_names(fn)+ assert "v" not in _def_names(fn)+ assert "t" in {o.name for o in fn.def_use.captured.reads} # a closure read still counts+++def test_scala_lambda_mutating_a_captured_var_is_a_closure_write():+ fn = _first(+ "scala",+ """+ class Demo {+ def f(xs: List[Int]): Int = {+ var total = 0+ xs.foreach { v => total += v }+ total+ }+ }+ """,+ )+ assert "total" in {o.name for o in fn.def_use.captured.writes}+ assert "total" in {o.name for o in fn.def_use.captured.shared}+++def test_scala_catch_arm_writes_are_closure_writes():+ # ``catch_kinds`` is empty for Scala, so the CFG never sees a handler arm; it+ # is read as a closure instead of being dropped.+ fn = _first(+ "scala",+ """+ class Demo {+ def f(t: Int): Int = {+ var x = 0+ try { x = t } catch { case e: Exception => x = 1 }+ x+ }+ }+ """,+ )+ assert "x" in {o.name for o in fn.def_use.captured.writes}+++def test_scala_braceless_arms_keep_their_writes():+ # ``if (c) errors += 1 else buf += r`` has no ``block``: the CFG builder hands+ # over the arm's tokens one by one, so the dialect reads the arm whole.+ fn = _first(+ "scala",+ """+ class Demo {+ def f(records: List[Int]): Int = {+ var errors = 0+ for (r <- records) if (r < 0) errors += 1 else println(r)+ errors+ }+ }+ """,+ )+ assert any(d.var == "errors" and d.may for d in fn.def_use.definitions)+ assert not [u for b in fn.def_use.blocks.values() for u in b.uses if u.name == "+="]+++_SCALA_PROCESS = """+class Demo {+ def process(records: Array[Int], threshold: Int): Int = {+ var errors = 0+ val results = scala.collection.mutable.ListBuffer.empty[Int]+ for (r <- records) {+ if (r < 0) { errors += 1 } else { results += r }+ }+ var total = 0+ var count = 0+ for (v <- results) {+ if (v > threshold) {+ total += v+ count += 1+ } else {+ total -= v+ }+ }+ var average = 0+ if (count > 0) {+ average = total / count+ }+ average + errors+ }+}+"""+++def test_scala_extract_method_fires():+ lmap = get_language_map("scala")+ extractions = find_extractions(_first("scala", _SCALA_PROCESS), lmap)+ assert extractions, "expected at least one Scala extraction"+ best = extractions[0]+ assert "average" in best.returns+ assert "results" in best.params and "threshold" in best.params+ assert len(best.returns) <= 1+ assert best.ccn_removed >= 1+ assert best.slice_nloc >= 6+++def test_scala_braceless_arm_plans_match_the_braced_ones():+ lmap = get_language_map("scala")+ braceless = _SCALA_PROCESS.replace(+ "if (r < 0) { errors += 1 } else { results += r }", "if (r < 0) errors += 1 else results += r"+ )++ def spans(src):+ return [(e.start_line, e.end_line, e.params, e.returns) for e in find_extractions(_first("scala", src), lmap)]++ assert spans(braceless) == spans(_SCALA_PROCESS)+++def test_scala_span_ending_in_the_final_expression_is_refused():+ lmap = get_language_map("scala")+ src = _SCALA_PROCESS+ last_stmt_line = src.splitlines().index(" average + errors") + 1+ for e in find_extractions(_first("scala", src), lmap):+ # The block's last expression is the function's value: a span holding it+ # would drop it, so none ends on it.+ assert e.end_line < last_stmt_line+ # ...and a span stopping before it returns the local the tail reads.+ assert any("average" in e.returns for e in find_extractions(_first("scala", src), lmap))+++def test_scala_span_with_return_or_throw_is_refused():+ lmap = get_language_map("scala")+ src = _SCALA_PROCESS.replace(+ " average = total / count\n", " average = total / count\n if (average > 99) return 0\n"+ )+ assert "return 0" in src+ ret_line = src.splitlines().index(" if (average > 99) return 0") + 1+ for e in find_extractions(_first("scala", src), lmap):+ assert not e.start_line <= ret_line <= e.end_line+++_SCALA_LAMBDA_LATE = """+class Demo {+ def process(records: List[Int], threshold: Int): Int = {+ var errors = 0+ val results = scala.collection.mutable.ListBuffer.empty[Int]+ for (r <- records) {+ if (r < 0) { errors += 1 } else { results += r }+ }+ var total = 0+ var count = 0+ for (v <- results) {+ if (v > threshold) {+ total += v+ count += 1+ } else {+ total -= v+ }+ }+ records.foreach { x =>+ if (x > 99) RETURN+ }+ var average = 0+ if (count > 0) {+ average = total / count+ }+ average + errors+ }+}+"""+# The same method with the lambda first, so the spans after it stay clear of it.+_SCALA_LAMBDA_EARLY = """+class Demo {+ def process(records: List[Int], threshold: Int): Int = {+ var errors = 0+ records.foreach { x =>+ if (x > 99) RETURN+ }+ val results = scala.collection.mutable.ListBuffer.empty[Int]+ for (r <- records) {+ if (r < 0) { errors += 1 } else { results += r }+ }+ var total = 0+ var count = 0+ for (v <- results) {+ if (v > threshold) {+ total += v+ count += 1+ } else {+ total -= v+ }+ }+ var average = 0+ if (count > 0) {+ average = total / count+ }+ average + errors+ }+}+"""+_SCALA_LAMBDA_BODY = _SCALA_LAMBDA_LATE+_SCALA_LAMBDA_RETURN = _SCALA_LAMBDA_LATE.replace("RETURN", "return x")+_SCALA_LAMBDA_LOCAL = _SCALA_LAMBDA_LATE.replace("RETURN", "println(x)")+++def _scala_span_jump(src: str, needle: str) -> bool:+ """The span walk's jump bit for the one statement holding *needle*."""+ from repowise.core.analysis.health.dataflow.slice import _scan_for, _span_metrics++ lmap = get_language_map("scala")+ fn = _first("scala", src).fn_node+ stmt = next(+ n for n in _walk_nodes(fn) if needle in n.text.decode() and n.parent is not None+ and n.parent.type in lmap.block_kinds+ )+ return _span_metrics([stmt], _scan_for(lmap, None, fn)).jump+++def _walk_nodes(node):+ yield node+ for c in node.children:+ yield from _walk_nodes(c)+++def _scala_spans(src: str):+ return find_extractions(_first("scala", src), get_language_map("scala"))+++def test_scala_lambda_exit_kinds_is_set_for_scala_only():+ assert get_language_map("scala").lambda_exit_kinds == {"return_expression"}+ # Every other language keeps a lambda ``return`` local: the default is empty.+ assert not get_language_map("python").lambda_exit_kinds+ assert not get_language_map("java").lambda_exit_kinds+++def test_scala_return_inside_a_lambda_in_the_span_is_a_jump():+ # ``return x`` in the ``foreach`` lambda leaves ``process``, so a helper+ # holding it would return from itself instead.+ assert _scala_span_jump(_SCALA_LAMBDA_RETURN, "records.foreach")+ assert not _scala_span_jump(_SCALA_LAMBDA_LOCAL, "records.foreach")+++def test_scala_span_holding_a_lambda_return_is_refused():+ ret_line = _SCALA_LAMBDA_RETURN.splitlines().index(" if (x > 99) return x") + 1+ local = [e for e in _scala_spans(_SCALA_LAMBDA_LOCAL) if e.start_line <= ret_line <= e.end_line]+ assert local, "control: without the return, spans over the lambda are offered"+ assert not [+ e for e in _scala_spans(_SCALA_LAMBDA_RETURN) if e.start_line <= ret_line <= e.end_line+ ]+++def test_scala_return_in_a_lambda_outside_the_span_is_still_extractable():+ src = _SCALA_LAMBDA_EARLY.replace("RETURN", "return x")+ ret_line = src.splitlines().index(" if (x > 99) return x") + 1+ spans = _scala_spans(src)+ assert spans, "a span clear of the lambda is still offered"+ assert not [e for e in spans if e.start_line <= ret_line <= e.end_line]+++def test_scala_return_in_a_nested_def_inside_the_span_is_not_a_jump():+ # The ``return`` belongs to the local ``def`` inside the lambda, not to+ # ``process``: the lambda walk stops at a function boundary.+ src = _SCALA_LAMBDA_LATE.replace(+ " if (x > 99) RETURN\n",+ " def local(y: Int): Int = { return y }\n local(x)\n",+ )+ assert "def local" in src and "return y" in src+ assert not _scala_span_jump(src, "records.foreach")+ # ...while a ``return`` beside that def, in the lambda itself, still counts.+ both = src.replace(" local(x)\n", " if (x > 99) return x\n local(x)\n")+ assert _scala_span_jump(both, "records.foreach")+++def test_scala_extractions_are_deterministic():+ lmap = get_language_map("scala")+ fn = _first("scala", _SCALA_PROCESS)++ def serialize():+ return [+ (e.start_line, e.end_line, e.params, e.returns, e.ccn_removed)+ for e in find_extractions(fn, lmap)+ ]++ first = serialize()+ for _ in range(3):+ assert serialize() == first+++def test_scala_flagged_only_gate_skips_small_functions():+ _require("scala")+ lines = ["class Demo {", " def big(x: Int): Int = {", " var y = 0"]+ for i in range(12):+ lines += [f" if (x == {i}) {{", f" y = {i}", " }"]+ lines += [" y", " }", " def tiny(): Int = 1", "}"]+ result = build_cfgs_for_file("m.scala", "scala", "\n".join(lines).encode())+ if result.stats.functions_seen == 0:+ pytest.skip("tree-sitter language pack missing for scala")+ assert result.stats.functions_seen == 2+ assert result.stats.functions_built == 1+ assert [fc.name for fc in result.functions] == ["big"]+++_SCALA_COUNTERS = """+class Demo {+ def f(xs: List[Int]): Int = {+ var total = 0+ val buf = scala.collection.mutable.ListBuffer.empty[Int]+ var pos = 0+ var neg = 0+ for (x <- xs) {+ if (x > 0) { buf += x; pos += 1 } else { buf += -x; neg += 1 }+ }+ var zeros = 0+ for (x <- xs) {+ if (x == 0) { zeros += 1 } else if (x > 100) { zeros -= 1 }+ }+ var seen = 0+ TOTAL_LINE+ seen += 1+ var avg = 0+ if (total > 0) {+ avg = total / 2+ } else {+ avg = 1+ }+ val n = buf.size+ avg + total + n + pos + neg + zeros + seen+ }+}+"""+++def test_scala_closure_write_to_a_captured_var_blocks_a_split():+ # The control reads ``total`` where a plain write sets it, so the tail+ # ``if`` is a clean extraction. When a lambda writes ``total`` instead, the+ # write happens when the closure runs, not where it is written: the span+ # would read a stale copy, so it is refused.+ lmap = get_language_map("scala")++ def spans(total_line):+ fn = _first("scala", _SCALA_COUNTERS.replace("TOTAL_LINE", total_line))+ return {(e.start_line, e.end_line) for e in find_extractions(fn, lmap)}++ assert spans("total += xs.size")+ assert not spans("xs.foreach { v => total += v }")+++def test_scala_implicit_parameter_is_carried_by_a_span_that_calls():+ # An implicit is a use no call site writes: a span holding a call carries it+ # as a parameter (the helper's author marks it ``implicit``).+ src = _SCALA_PROCESS.replace(+ "(records: Array[Int], threshold: Int)", "(records: Array[Int], threshold: Int)(implicit ec: Ec)"+ ).replace("total += v", "total += load(v)")+ call_line = src.splitlines().index(" total += load(v)") + 1+ spans = find_extractions(_first("scala", src), get_language_map("scala"))+ with_call = [e for e in spans if e.start_line <= call_line <= e.end_line]+ assert with_call+ assert all("ec" in e.params for e in with_call)+ # No implicit, no extra parameter.+ plain = _SCALA_PROCESS.replace("total += v", "total += load(v)")+ spans = find_extractions(_first("scala", plain), get_language_map("scala"))+ assert spans and all("ec" not in e.params for e in spans)