Skip to content

fix(js): keep nested functions scoped so same-named calls don't bind to them - #4767

Closed
smngvlkz wants to merge 1 commit into
Graphify-Labs:v8from
smngvlkz:fix/js-nested-function-scope
Closed

smngvlkz wants to merge 1 commit into
Graphify-Labs:v8from
smngvlkz:fix/js-nested-function-scope

Conversation

@smngvlkz

Copy link
Copy Markdown
Contributor

What this fixes

Since #2653, a JS/TS function declared inside another function gets its own node, which is great. But it also went into the file-wide name lookup, so a call to that name from anywhere in the file could get linked to it, even from outside the function it lives in. Two ways this goes wrong:

  • two functions that each have their own inner run: both get linked to the last one
  • a function with an inner helper steals the callers of a top-level or imported helper

So the graph says things call functions they can't even see.

How to recreate

mkdir repro && cd repro
cat > x.ts <<'EOF'
export function helper() { return 0; }
export function a() { function helper() { return 1; } return helper(); }
export function b() { return helper(); }
EOF
graphify extract . --code-only --no-cluster
python -c "import json; g=json.load(open('graphify-out/graph.json')); [print(e['source'], e['relation'], e['target']) for e in g['edges'] if e['relation'] == 'calls']"

Before:

x_a calls x_a_helper
x_b calls x_a_helper

b() is linked to the helper hidden inside a(). After:

x_a calls x_a_helper
x_b calls x_helper

Same thing with two inner functions of the same name:

export function first() { function run() { return 1; } return run(); }
export function second() { function run() { return 2; } return run(); }

Before, first() calls x_second_run. After, each calls its own.

What changed

Python already handles this the right way (#3405), so I reused the same approach for JS/TS:

  • each inner function is recorded under the function it's declared in
  • a call (or a function passed by name, like register(run)) looks in its own scope first, then the outer ones
  • inner functions are kept out of the file-wide lookup, since they can't be called from outside anyway

Top-level functions, imports and methods are looked up the same as before.

How I tested it

  • added tests/test_js_nested_function_scope.py with the cases above, an inner function named like an import, siblings and deeper nesting, and inner functions inside arrow components. The four that check the wrong links fail on current v8 and pass with this change, the siblings one checks those still resolve
  • ran it on five of my own JS/TS projects before and after. Four didn't change at all. The fifth has a minified Prisma runtime full of one-letter names, where calls that pointed into other functions' inner functions now don't
  • all 1,256 JS/TS related tests pass
  • full test suite: 8,422 passed. The 8 that fail (7 in test_ext_tagged_id_clash.py, 1 in test_skill_no_backtick_substitution.py) fail the same way on v8 without this change, they need the SQL parser which my local setup doesn't have

Not in this one

  • in the import example, a() also gets a calls lib_helper link from the separate import pass, which doesn't know about inner functions. That's on v8 too, so I left it for now
  • inner functions written as const helper = () => {} still don't get nodes at all (TypeScript extraction emits internal edges whose endpoint nodes are absent #2143). I have that fix ready and will open it after this one, since it builds on this

@smngvlkz
smngvlkz requested a review from safishamsi as a code owner October 25, 2026 18:59
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 25, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @smngvlkz. 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.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 3 changed function(s) (3 not verified).

Formal verification. PR-changed functions: 0/3 verified (0 proven, 0 may-equivalent, 0 distinguished) · 3 not verified (2 vacuous, 1 unsupported).

Not verified on this run: \_extract\_generic (unsupported), \_js\_extra\_walk (vacuous: never exercised), \_scan\_js\_nested\_function\_declarations (vacuous: never exercised).


Graphify review — findings

Fixes JS/TS call resolution so a nested function binds only inside its own lexical scope. Nested declarations are now recorded in scope_parents and lexical_nids_by_scope, so both direct calls and indirect references (e.g. register(run)) resolve to the nearest in-scope function before the file-wide name map is consulted, the same scheme Python already uses. Nested JS/TS functions are also kept out of the file-wide label_to_nid map, so a same-named top-level, imported, or sibling-inner function no longer has its callers stolen, but any call that falls outside the recorded scope chain can no longer reach the nested function.

Worth a look

  • Nested JS function becomes unreachable from callers outside the recorded scope chain — graphify/extractors/engine.py:8725 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3253 functions depend on the 376 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 1016 callers, 62 callees

Verification — 3253 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1678 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

40 of 454 test file(s) selected (9%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_js_nested_function_scope.py — impact, changed-test
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_pytest_fixture_receivers.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_context_bounds.py — impact
  • tests/test_scala_given_instances.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact
  • tests/test_scala_type_definition.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_vue_extraction.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

Could not verify: Could not verify \_js\_extra\_walk.

The verifier did not have enough to check \_js\_extra\_walk, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 264 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify \_scan\_js\_nested\_function\_declarations.

The verifier did not have enough to check \_scan\_js\_nested\_function\_declarations, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 244 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

· 1 more finding(s) on lines outside this diff (see the check run).

@bercedev bercedev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran the new tests (4 fail on v8, all 5 pass here) and threw a few extra shapes at it:

  • sibling inner functions (a() calling b() inside the same outer) still link
  • a call from an arrow callback inside the outer still resolves to the outer's inner a
  • a nested function inside a class method still links from that method
  • caller() { return top(); } with a top-level top and another function's inner top: v8 bound it to other_top, this PR binds it to the top-level one

All as expected. LGTM.

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.97, now on PyPI (pip install graphifyy==0.9.97). Nested functions stay scoped so a same-named call elsewhere binds to the top-level function. Thanks for the fix, @smngvlkz.

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants