Skip to content

JS: parameters of a file-scope anonymous callback are not shadowed, so a callback argument fabricates an indirect_call to any same-named function in the corpus #4448

Open

Description

@laurism-openelms

Behavior

When JS code sits inside an anonymous callback with no named enclosing function (the AngularJS .controller('X', function ($scope) { ... }) shape, items.forEach(function (value) { ... }) at file scope, a test's it('...', () => { ... })), a bare identifier that is one of that callback's own parameters, passed on as a call argument, is resolved against the corpus-wide label index. It produces an indirect_call edge (INFERRED, 0.85, context argument) from the file node to an unrelated same-named function in another file, including a module-private function in a separate package that the caller never imports.

The same code inside a named function is handled correctly.

Observed with graphify 0.9.90.

Minimal reproduction

a.js:

items.forEach(function (value) { s.on(value); });

b.js:

function value(x) { return x; }
graphify extract . --code-only

Result:

a.js --indirect_call [INFERRED, context=argument]--> value()

value at that location is the callback's parameter, not a reference to b.js's function.

Variants (all on 0.9.90, two-file corpus as above)

a.js edge to value()
items.forEach(function (value) { s.on(value); }); fabricated
items.forEach(function (value) { emit(value); }); fabricated
m.c(function (s) { items.forEach(function (value) { s.on(value); }); }); fabricated
function run(items){ items.forEach(function (value) { s.on(value); }); } none (correct)
function run(items){ items.forEach(function (value) { emit(value); }); } none (correct)
function run(items){ items.forEach(value); } (real reference, control) kept (correct)

The target does not have to be in the same package or language. With the target in a separate TypeScript package as a module-private function:

app/package.json: { "name": "app", "type": "module" }

app/src/transport.ts:

async function value(response: Response): Promise<unknown> {
  return response.json();
}
export async function load(response: Response): Promise<unknown> {
  return value(response);
}

legacy/controller.js:

angular.module('app').controller('ListCtrl', function ($scope) {
	angular.forEach($scope.events, function (value) {
		$scope.$on(value, function () {});
	});
});

Result: controller.js --indirect_call [INFERRED, argument]--> value() in transport.ts. The function is not exported and nothing imports the file.

Expected

No edge. A parameter of the enclosing closure shadows the name, exactly as it already does when the closure sits inside a named function (#2241).

Probable cause (read, not patched)

The #3124 path in extractors/engine.py (inside the expression_statement branch that #3408 added) collects each topmost closure of a module-level call statement and queues it for the call walk as function_bodies.append((file_nid, closure_body)). It queues the closure's body, so the closure's own parameters never enter closure_locals_by_body, and local_bound_names has no entry for file_nid. Inside a named function the same closure is reached through the walk_calls boundary branch instead, which folds _js_local_bound_names(node, source) into extra_locals, so the shadow works there.

Seeding closure_locals_by_body[id(closure_body)] with _js_local_bound_names(closure, source) at that append looks like the fix for the direct case. I have not traced why the nested variant (third row) still fabricates, so that row is worth its own regression test.

Impact

On a 33k-node mixed PHP / AngularJS / React-TS repository this produced 110 phantom edges from AngularJS controllers into the separate React apps, almost all from generic callback parameter names: slug (34), value (27), entry (25), scope (16), row, item, title. The two apps are separate bundles and cannot call each other. The edges also moved targets into the wrong community: the TypeScript value() above was clustered into the AngularJS "Admin Learning Controllers" community.

Related but distinct: #2606 (block-scoped for...of binding in object shorthand), #2569 (unparenthesised arrow parameter), #2517 (catch bindings).

Suggested regression test

The two-file corpus above: assert no indirect_call from a.js to b_value for the three file-scope rows, and keep the run() -> value() edge for the control row.

Activity

  1. github-actions commented on Oct 14, 2026

    @github-actions

    Thanks for opening this issue, @laurism-openelms. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. omsenjalia commented on Oct 18, 2026

    @omsenjalia
    Contributor

    This looks fixed on the v8 branch by 1af5104b (fix(js): shadow a file-scope callback's own params (#4448)). Could a maintainer confirm and close?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions