Repository navigation
fix(typescript): resolve calls through immutable local const aliases - #5622
Komorebi-GZM wants to merge 6 commits into
Conversation
…raphify-Labs#5613) Inside an indexed TS scoped callable, a call through a const alias (`const persist = save; persist()`) hit the local-binding arm, failed the bindings lookup (the alias is not a function-valued const), and was dropped entirely: raw_calls requires ts_local_binding to be None. Follow the alias one hop when it is an immutable const initialized to a bare identifier, re-resolving the aliased name through the nearest lexical scope, then the file-wide label map, then raw_calls. The emitted edge keeps the caller as the scoped callable and the real alias call site as its location. Reassigned (`let`) or self-referential bindings stay unresolved, matching the existing fail-closed rule. Regressions prove the old failure first: the issue's repro asserted prepare() -> save() (L6) is absent before the fix, alongside a same-file alias case and a mutable-let fail-closed guard.
|
Thanks for the pull request, @Komorebi-GZM. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify review 
1 bug · 3 possible issues to check · High impact
The extractor now tries to resolve calls through a one-hop const alias in TypeScript and JavaScript.
A local variable can make an alias call point to an outer function ·
engine.py:13180
3 possible issues we couldn't confirm
- Module-level
save()is picked by node-id text, so the nestedsaveininnercan be chosen instead ·tests/test_ts_const_alias_calls.py:153 - Closure alias test passes even when the aliased call from
inner()produces no edge at all ·tests/test_ts_const_alias_calls.py:163 - Parameter-shadowing alias test passes if the file yields no call edges at all ·
tests/test_ts_const_alias_calls.py:128
They don't change the score.
Code graph · 4822 functions depend on this change
- 3 functions became more coupled.
extract()(1432 callers),_js_enclosing_bound_names()(4 callers),walk_calls()(1 callers). - 58 of 659 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed f4cc2c5 · 6 earlier findings not re-checked
| # stays unresolved. | ||
| aliased = _ts_immutable_alias_target(ts_local_binding, callee_name, source) | ||
| if aliased is not None: | ||
| tgt_nid = ts_callable_scopes.bindings.get(ts_local_binding, {}).get(aliased) |
There was a problem hiding this comment.
An alias of a parameter can resolve to a same-named global function
A call through a const alias can get a call edge to a global function even when the alias refers to a parameter instead.
Resolve the alias initializer against lexical bindings in _ts_immutable_alias_target.
Evidence · checked against the code
Expected: The call edge should not identify the same-named file-level function when the parameter shadows it.
Actually: If the callable-scope binding lookup has no entry for the aliased name, the fallback can select the file-level label and attribute the call to that function.
- Independent source review: Inspected the nested-callable scanner, which records nested callable declarations in its binding and lexical-name maps, and the alias branch. Neither establishes that the alias initializer's identifier resolves to the selected file-level function rather than a parameter. The existing tests cover ordinary aliases and local-callable aliases, but do not test a parameter shadowing a same-named file-level function.
- I did not run a reproduction against the extractor.
- Also reported as: Calls through a local alias can link to an unrelated outer function with the same name
Prompt to fix with AI
In graphify/extractors/engine.py, _ts_immutable_alias_target (lines 13149-13152):
Problem: An alias of a parameter can resolve to a same-named global function
Expected: The call edge should not identify the same-named file-level function when the parameter shadows it.
Actually: If the callable-scope binding lookup has no entry for the aliased name, the fallback can select the file-level label and attribute the call to that function.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
… local binding Following an immutable const alias fell through to the file-wide label map even when the aliased name was bound by an intermediate scope -- e.g. a parameter shadowing a same-named file-level function -- so the edge could point at the global it does not name. Gate the label fallback on the call site's enclosing bound names: a shadowed initializer is not a known callable, so the call fails closed. Also strengthen the reassigned-let regression: assert no edge at all from the reassigned call plus a positive const-alias control in the same fixture, and add a parameter-shadowing regression (red before the fix). Co-Authored-By: Claude <[email protected]>
|
Fixed the review findings in de22a26:\n\n- P2 (shadowed alias): the const-alias path now only falls back to the file-wide label map when the aliased name is not bound by any enclosing scope (parameter/local); a shadowed initializer fails closed with no edge.\n- Test A: the reassigned-let test now asserts no edge at all from the reassigned call, plus a positive const-alias control in the same fixture so it cannot pass vacuously.\n- Issue B: added a parameter-shadowing-same-named-global regression (fails on the old code, passes now).\n\nTarget suite plus TS/indirect-call/shadow suites and a sampled set of unrelated suites all pass. |
| tgt_nid = lexical_nids_by_scope[curr_scope][aliased] | ||
| break | ||
| curr_scope = scope_parents.get(curr_scope) | ||
| if not tgt_nid: |
There was a problem hiding this comment.
An outer alias can resolve to an inner shadowing function
A call through an alias captured from an outer scope can point to a same-named function declared inside the calling function.
Start fallback lookup for captured aliases from the alias’s binding scope, not caller_nid.
Evidence · checked against the code
Expected: The call edge identifies the callable named by the alias initializer, or remains unresolved if that target cannot be established.
Actually: The lexical lookup starts at the caller and can select its same-named nested function before reaching the alias's target scope.
- Independent source review: The parameter-shadow test checks an alias initialized from a parameter and verifies it does not fall through to a file-level function. That does not block the claimed case: a nested function declaration is recorded in lexical_nids_by_scope for the caller's scope, which the alias fallback consults first. No read test covers this nested-function case.
- Also reported as: A local
constalias can call the wrong same-named function when the call is nested in an inner function
Prompt to fix with AI
In graphify/extractors/engine.py, _extract_generic (lines 13164-13167):
Problem: An outer alias can resolve to an inner shadowing function
Expected: The call edge identifies the callable named by the alias initializer, or remains unresolved if that target cannot be established.
Actually: The lexical lookup starts at the caller and can select its same-named nested function before reaching the alias's target scope.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
…g scope Following an immutable const alias, the lexical fallback walk started at the caller's scope. When the call sits in an inner function that declares a same-named nested function, the walk named that nested function instead of the alias initializer's real target (Graphify-Labs#5622 review, round 2). Start the walk at the scope where the alias is declared, so the inner caller's lexical table never participates. Adds a closure-shadowing regression (red before, green after); the parameter-shadowing regression from the previous round stays green.
|
Addressed the round-2 review finding: the lexical fallback after a const alias now starts at the alias's binding scope instead of the caller's scope, so an inner caller with a same-named nested function can no longer shadow the initializer's real target. Added a closure-shadowing regression test (red before the fix, green after); the parameter-shadowing regression from the previous round still passes. |
| # call-site location (#5613). | ||
| curr_scope = alias_scope | ||
| while curr_scope: | ||
| if curr_scope in lexical_nids_by_scope and aliased in lexical_nids_by_scope[curr_scope]: |
There was a problem hiding this comment.
An aliased call can resolve to the wrong same-named function
When a const alias refers to a function declared in an enclosing scope, the lookup can skip that local function and emit a call edge to a same-named file-level function instead.
Resolve aliases through the enclosing callable's scope in _extract_generic.
Evidence · checked against the code
Expected: The persist() call edge targets the enclosing callable's nested save.
Actually: Alias lookup can fail to traverse from its AST scope to the enclosing callable ID, miss the nested declaration, and fall back to the file-level save.
- Independent source review: Inspected the existing closure test where
persistaliases a file-levelsaveand the inner closure declares anothersave. That is the reverse arrangement: the expected target is the file-level function, so it does not refute this claim. The inspected tests do not cover an enclosing callable's nestedsavealiased by a closure when a file-levelsavealso exists. - Some lookups asked for files or lines that do not exist; the review was told and carried on.
- Also reported as: Aliases to callables in an outer scope are left unresolved
Prompt to fix with AI
In graphify/extractors/engine.py, _extract_generic (lines 13166-13169):
Problem: An aliased call can resolve to the wrong same-named function
Expected: The `persist()` call edge targets the enclosing callable's nested `save`.
Actually: Alias lookup can fail to traverse from its AST scope to the enclosing callable ID, miss the nested declaration, and fall back to the file-level `save`.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
…lable's nested declarations The alias re-resolution climbed scope_parents/lexical_nids_by_scope from the alias's AST scope node, but those maps are keyed by callable nid, so the walk never matched and a miss fell through to the file-wide label map. When the aliased name names a function nested in the enclosing callable (with a same-named file-level function also present), the call edge pointed at the wrong function. Climb the AST scope chain from the alias's own block via ts_scopes.bindings instead (keyed by AST body block, where nested declarations are registered), stopping at any parameter/var/let-const shadow along the way; the label fallback stays gated on the enclosing bound names.
|
Round 3 fix: the const-alias re-resolution climbed The fix climbs the AST scope chain outward from the alias's own block via |
| # round 3); the caller-nid climb would miss it | ||
| # and the file-wide label map would name an | ||
| # unrelated same-named module function. | ||
| shadowed = False |
There was a problem hiding this comment.
A local variable can make an alias call point to an outer function
In TypeScript, const persist = save; persist() can be recorded as a call to an enclosing save() even when a local const save shadows it.
Check the alias’s own scope for shadowing declarations before climbing in walk_calls.
Evidence · checked against the code
Expected: The alias call should not resolve to the enclosing save function because the initializer refers to the block-local value.
Actually: Alias resolution skips its own scope when searching for save, so it can select the enclosing callable and emit a false call edge.
- Independent source review: The existing alias tests cover parameter shadowing and an alias declared outside a caller that has a same-named nested function. They do not cover a non-callable
let/constin the alias’s own scope shadowing an enclosing callable; the implementation’s ancestor walk omits that scope. - No test was run; the supplied tests do not cover a non-callable lexical binding in the alias’s own scope shadowing an enclosing callable.
- Also reported as: Alias lookup skips the alias's own scope, so a local non-function
savecan resolve to an outer function namedsave
Prompt to fix with AI
In graphify/extractors/engine.py, walk_calls (lines 13177-13180):
Problem: A local variable can make an alias call point to an outer function
Expected: The alias call should not resolve to the enclosing `save` function because the initializer refers to the block-local value.
Actually: Alias resolution skips its own scope when searching for `save`, so it can select the enclosing callable and emit a false call edge.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
…ng before the alias re-resolution climb The const-alias re-resolution started its outward shadow walk at the alias's parent scope, so a non-callable local (`const save = someValue`) declared beside the alias itself was never seen. When the aliased name also resolves to a callable through the enclosing callable's nested declarations, the climb named that callable even though the initializer never refers to it. The alias's own scope is now checked with the same per-scope shadow logic the climb applies (parameter/var via _js_local_bound_names, let/const via _js_direct_lexical_names), and an outer binding is skipped when that check shadows the name, failing closed (Graphify-Labs#5622 round 4).
|
Round 4 fix: the const-alias re-resolution's outward shadow walk started at the alias's parent scope, so a non-callable local declared beside the alias itself — e.g. The fix runs the exact same per-scope shadow logic the climb uses (parameters/ Red→green proven: Alias-resolution semantic matrix — four review rounds (#5622)
Mutable |
The file-wide fallback after a const-alias walk consulted the CALL node's enclosing bindings, so an outer `const persist = save` called inside an inner function that declared a same-named parameter or `let`/`const` failed closed instead of targeting the initializer. The climb also skipped `catch` / `for-in` bindings, so an alias of a catch parameter could resolve to an enclosing same-named nested function. Fail-closed now follows the alias's own scope chain (including catch and for-in), matching `_js_enclosing_bound_names`. Adds red→green regressions for the call-site parameter/const cases and for a catch binding; prior alias tests stay green.
|
Thanks for this. Closing in favor of #5614 for the const-alias resolution. The issue with the name-rewrite-into- |
Fixes #5613.
Root cause
Inside an indexed TS scoped callable, a call through a local const alias (
const persist = save; persist()) enters the TS local-binding arm of the call-edge resolver._js_receiver_binding_scopecorrectly finds the binding scope forpersist, butts_callable_scopes.bindingsonly registers function-valued consts and function declarations, so the lookup returnsNone. The raw_calls fallback requirests_local_binding is None, so the call is dropped entirely — no edge, no raw_call.Fix
_ts_immutable_alias_target(scope, name, source): one-hop follow ofconst name = <bare identifier>inside the binding scope. Deterministic AST-only lookup, no environment or ordering dependence.lexical_nids_by_scope/scope_parents, then the file-wide label map, then raw_calls (which resolves imports cross-file). Non-const (reassignable) bindings and self-referential aliases stay unresolved, preserving the existing fail-closed rule.Provenance is preserved: the emitted edge keeps the scoped callable as caller and the real alias call's location (
L6in the issue's repro) assource_location.Testing
New
tests/test_ts_const_alias_calls.py(red before the fix, green after — run in that order):prepare() → save()atL6andcontrol() → save()atL8;letalias stays fail-closed (no fabricated edge to either candidate).Full targeted suites (
test_ts_factory_call_ownership,test_extract, indirect-call shadow suites, TS receiver/member/new-expression suites, etc.): 519 passed, 4 skipped, 1 xfailed. A broad-k "ts or ... call ..."sweep shows only failures that reproduce identically on unmodifiedv8(baseline: 84 → with fix: 82, the delta being the 3 new passing tests), so no regressions.