Skip to content

fix(ts): never guess a same-named function for a locally bound callee (#1355) - #3012

Open
DryadeCore wants to merge 1 commit into
DeusData:mainfrom
DryadeCore:fix/tsx-local-binding-name-fallback
Open

DryadeCore wants to merge 1 commit into
DeusData:mainfrom
DryadeCore:fix/tsx-local-binding-name-fallback

Conversation

@DryadeCore

Copy link
Copy Markdown

What does this PR do?

A bare call whose callee the calling function binds locally to a value that is not a
definition (const submitForm = useCallback(...), a useState setter, a callback
parameter) was bound by short name to an unrelated function of the same name, in a
file the caller never imports. Found while indexing a large React codebase, where
handlers declared this way are the norm. This is one more shape of #1355: the caller's
own source already says the callee is not that project symbol.

Minimal repro

src/Elsewhere.tsx   export function submitForm(v: string) { return v; }    (imported by nobody)
src/Caller.tsx      import { useCallback } from "react";
                    export function Caller() {
                      const submitForm = useCallback(async (v: string) => v.length, []);
                      const go = () => { void submitForm("x"); };
                      return <button onClick={go} />;
                    }
codebase-memory-mcp cli index_repository --repo-path repro --name fixture
codebase-memory-mcp cli query_graph --project fixture \
  --query "MATCH (a)-[r:CALLS]->(b) WHERE b.name = 'submitForm' RETURN a.name, b.qualified_name, r.strategy"

On main (cdfdf26): go fixture.src.Elsewhere.submitForm unique_name. Identical with
CBM_INDEX_SINGLE_THREAD=1.

Root cause

What changed

  • internal/cbm/extract_usages.c: the lexical binding table the unified walk already
    keeps for JS/TS (parameters, let/const/var, with their scope and active range)
    records one more fact per binding, names_no_definition: a parameter, a declarator
    whose initializer is not a function or class, or an array-pattern element. After the
    walk, cbm_finalize_lexical_usages flags every bare call whose nearest binding is such
    a local. It runs after the walk because let/const/var bind their whole scope, so a
    handler may call a local that is declared further down.
  • Not flagged, on purpose: a function-valued declarator (const save = () => {}, also
    behind parentheses, as, satisfies, !) and a nested function declaration, which are
    real definitions; an import; a module-scope binding; a declarator without initializer.
    A scope that binds the name twice is flagged only when every active binding agrees.
  • src/pipeline/registry.c: cbm_weak_local_binding_guard_lang() holds the language set
    (Python, JS, TS, TSX, ArkTS). pass_calls.c and pass_parallel.c both take the guard's
    enabled from it, so the two lists cannot drift.
  • The guard itself is unchanged: it only drops the plain-CALLS fall-through of
    suffix_match / unique_name / field_type_hint / fuzzy. LSP, import-map and
    same_module matches are kept, and route / HTTP classification still runs.

No new node kinds, no change to what is extracted as a definition: #1220 and #415 stay
with the maintainer.

Tests

  • tests/test_extraction.c extract_tsx_bare_call_flags_locally_bound_callee: flagged:
    useCallback const, the same declared after its caller, array-pattern element,
    parameter, call result behind as. Not flagged: shorthand object-pattern name,
    arrow-valued const, arrow behind parentheses and as, nested function declaration,
    unbound name, module-scope const, import, unbound name in a sibling function. Each
    callee must be extracted exactly once.
  • tests/test_pipeline.c pipeline_tsx_local_binding_not_bound_to_homonym_issue1355: one
    tree, indexed with CBM_INDEX_SINGLE_THREAD=1 and with CBM_WORKERS=4 (64 padding
    files):
    • go -> Elsewhere.submitForm = 0 (useCallback const)
    • go -> Elsewhere.setOpen = 0 (useState setter)
    • finish -> Elsewhere.onDone = 0 (callback parameter of the enclosing hook)
    • control, arrow-valued local: go -> localStep = 1
    • control, a name the caller never binds: callsUnbound -> submitForm = 1
    • control, destructured hook member: ListPage -> removeItem = 1
  • tests/test_registry.c: rows for cbm_weak_local_binding_guard_lang.

Results (linux arm64, gcc 13.3, base cdfdf26)

Fixture above, release build (scripts/build.sh), 64 padding files added so that both
resolvers can be forced:

main  CBM_INDEX_SINGLE_THREAD=1   go  fixture.src.Elsewhere.submitForm  unique_name
main  CBM_WORKERS=4               go  fixture.src.Elsewhere.submitForm  unique_name
fix   CBM_INDEX_SINGLE_THREAD=1   rows: 0
fix   CBM_WORKERS=4               rows: 0

A public tree to check it against: this repository's own graph-ui/ at cdfdf26 (React +
TypeScript, plus .mjs tools), indexed by main and by this branch into separate cache dirs,
then every edge compared:

nodes                      20183 -> 20183
CALLS                       9008 ->  8849   (159 removed: 109 suffix_match, 50 unique_name; 0 added)
JS/TS weak-strategy CALLS    286 ->   127
other edge types           identical, except SEMANTICALLY_RELATED (5 removed, 12 added; it is
                           derived from the call graph, and two runs of main give identical edges)

For none of the 159 removed edges is the target in the caller's file, and for none does the
caller's file have an IMPORTS edge to the target's file. I read 9 of them in the source;
all 9 are local bindings, for example:

  • ProjectsPanel -> AddProjectIndexDialog.loadProjects: ProjectsPanel declares
    const loadProjects = useCallback(...); the dialog has its own local arrow of that name.
  • useSystemPoll.poll -> IndexesPanel.read: read is a parameter of the hook.
  • NodeLabels -> GalaxyPanel.measure: const measure = useCallback(...) in NodeLabels.
  • SpatialArchitecture -> SystemArchitecture.setEntryChoice: a useState setter.
  • 39 edges from tools/*.mjs functions to adr-strings.done: done is the parameter of a
    new Promise((done) => ...) executor (read in one of the scripts).

Tests (ASan + UBSan, scripts/test.sh --suites <every suite of --list-suites>):
10501 passed, 25 failed, 11 skipped. All 25 failures are in the cli suite and come from
this host: the installer refuses a group-writable build directory
(install_dir_group_or_world_writable (mode 0775), umask 0002 here). No file of that suite
or of src/cli is touched; I did not build main's test runner to confirm they fail there
too. The three suites this PR touches (extraction, registry, pipeline) are green.

Reproduce-first: with only the call to flag_js_locally_bound_callees disabled and the
tests kept:

extract_tsx_bare_call_flags_locally_bound_callee
    bare-call flag mismatch for submitForm(): got 0, expected 1
    FAIL tests/test_extraction.c:10016: r->calls.items[i].callee_is_locally_bound == 0, expected cases[c].expect_bound == 1
pipeline_tsx_local_binding_not_bound_to_homonym_issue1355
    FAIL tests/test_pipeline.c:15402: sequential == 4, expected 0 == 0

Not run: the default scripts/test.sh leg (its clean step and contract checks), the TSan
leg, cppcheck, clang-tidy.

Limits

Checklist

  • Every commit is signed off (git commit -s)
  • Tests pass locally (make -f Makefile.cbm test) -- see results above for what was run
  • Lint passes (make -f Makefile.cbm lint-ci) -- clang-format 20.1.8: no drift on the
    changed hunks; cppcheck and clang-tidy not run
  • New behavior is covered by a test (reproduce-first for bug fixes)

Refs #1355

Written by an AI coding agent (Claude Code) operating this account on behalf of its owner, who authorised this submission and is accountable for it.

🤖 Generated with Claude Code

…DeusData#1355)

`const submitForm = useCallback(...)` inside a component is no graph node,
so the TS LSP declines a later `submitForm()`. The registry then fell back
to a project-wide short-name match and bound the call to any function that
shares the name, in a file the caller never imports: a fabricated CALLS
edge at unique_name / suffix_match confidence. The same happened for a
useState setter and for a callback parameter.

Python already drops such a guess for a callee that an enclosing scope
binds as a parameter (callee_is_locally_bound, DeusData#1912). This extends the
same flag to JS/TS/TSX/ArkTS:

- The walk's lexical binding table records, per binding, whether it names
  no definition: a parameter, a declarator whose initializer is not a
  function or class, or an array-pattern element. Function-valued
  declarators and nested function declarations are real definitions and
  stay unflagged, as do imports and module-scope bindings.
- cbm_finalize_lexical_usages flags every bare call whose nearest binding
  is such a local. It runs after the walk because let/const/var bind
  their whole scope (a handler may call a local declared further down).
- The weak-local-binding guard's language set moves into
  cbm_weak_local_binding_guard_lang, so pass_calls.c and pass_parallel.c
  share one list. Only the plain-CALLS fall-through of a weak strategy is
  dropped; LSP, import and same-module matches are untouched.

Tests: extraction flags for each binding shape, both directions; a
pipeline test on the sequential and the parallel resolver (homonyms in a
never-imported file, with an arrow-valued local, an unbound name and a
destructured hook member as controls); registry rows for the shared gate.

Co-Authored-By: Dammerzone <dammerzone@users.noreply.github.com>
Signed-off-by: Dryade <contact@dryade.ai>
@DryadeCore
DryadeCore requested a review from DeusData as a code owner November 16, 2026 12:39
@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant