Skip to content

Commit 1af5104

Browse files
bercedevsafishamsi
authored andcommitted
fix(js): shadow a file-scope callback's own params (#4448)
A closure passed in a module-level call statement is walked under the file node (#3124), but its params and locals were never registered for that walk. A parameter passed on as a call argument then resolved against the corpus-wide label index and produced an indirect_call to any function with the same name, including module-private ones in other packages. Record the closure's bindings in closure_locals_by_body, the same way the #2568 branch already does for const-assigned closures. (cherry picked from commit 60cfd51)
1 parent e7772b2 commit 1af5104

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎graphify/extractors/engine.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,14 @@ def _js_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,
38093809
# `_js_scan_member_assignments`, so it lands in
38103810
# `_tracked_body_ids` and walk_calls' own boundary check
38113811
# skips re-descending into it from here.
3812+
# #4448: the walk runs under the file node, so the
3813+
# closure's own params/locals must ride along as this
3814+
# body's extra_locals (as in the #2568 branch); otherwise
3815+
# `forEach(function (value) { s.on(value) })` resolves the
3816+
# parameter `value` to any same-named function in the corpus.
3817+
if closure_locals_by_body is not None:
3818+
closure_locals_by_body[id(closure_body)] = (
3819+
_js_local_bound_names(closure, source))
38123820
function_bodies.append((file_nid, closure_body))
38133821
assign = next((c for c in node.children
38143822
if c.type == "assignment_expression"), None)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""A parameter of a file-scope anonymous callback must shadow same-named
2+
functions elsewhere in the corpus (#4448).
3+
4+
#3124 walks the body of a closure passed in a module-level call statement
5+
(`items.forEach(function (value) { ... })`, AngularJS `.controller(...)`,
6+
`it("...", () => { ... })`) under the file node, but did not register the
7+
closure's own params/locals for that walk. A parameter passed on as a call
8+
argument was then resolved against the corpus-wide label index and became an
9+
`indirect_call` from the file to any function named like it. Inside a named
10+
function the same code was already handled (#2241, #2568).
11+
"""
12+
from __future__ import annotations
13+
14+
from graphify.extract import extract
15+
16+
_TARGET = {"b.js": "function value(x) { return x; }\n"}
17+
18+
19+
def _indirect(tmp_path, a_js: str):
20+
files = {"a.js": a_js, **_TARGET}
21+
for name, body in files.items():
22+
(tmp_path / name).write_text(body)
23+
r = extract([tmp_path / n for n in files],
24+
cache_root=tmp_path / "graphify-out", parallel=False)
25+
lbl = {n["id"]: n["label"] for n in r["nodes"]}
26+
return {(lbl.get(e["source"]), lbl.get(e["target"])) for e in r["edges"]
27+
if e["relation"] == "indirect_call"}
28+
29+
30+
def test_callback_param_passed_to_a_member_call_is_not_a_reference(tmp_path):
31+
assert _indirect(tmp_path, "items.forEach(function (value) { s.on(value); });\n") == set()
32+
33+
34+
def test_callback_param_passed_to_a_bare_call_is_not_a_reference(tmp_path):
35+
assert _indirect(tmp_path, "items.forEach(function (value) { emit(value); });\n") == set()
36+
37+
38+
def test_param_of_a_nested_file_scope_callback_is_not_a_reference(tmp_path):
39+
src = "m.c(function (s) { items.forEach(function (value) { s.on(value); }); });\n"
40+
assert _indirect(tmp_path, src) == set()
41+
42+
43+
def test_arrow_callback_params_are_not_references(tmp_path):
44+
src = "items.forEach((value) => emit(value));\nitems.map(value => emit(value));\n"
45+
assert _indirect(tmp_path, src) == set()
46+
47+
48+
def test_callback_var_is_not_a_reference(tmp_path):
49+
src = "items.forEach(function () { var value = 1; emit(value); });\n"
50+
assert _indirect(tmp_path, src) == set()
51+
52+
53+
def test_unbound_name_inside_a_file_scope_callback_still_resolves(tmp_path):
54+
assert _indirect(tmp_path, "items.forEach(function (x) { emit(value); });\n") == {("a.js", "value()")}
55+
56+
57+
def test_a_sibling_callbacks_param_does_not_shadow(tmp_path):
58+
src = "m.c(function (value) { emit(1); }, function () { emit(value); });\n"
59+
assert _indirect(tmp_path, src) == {("a.js", "value()")}
60+
61+
62+
def test_param_of_a_callback_inside_a_test_block_is_not_a_reference(tmp_path):
63+
# The inner arrow is itself an expression statement inside the `it`
64+
# callback, so the #3124 branch tracks it as its own body too.
65+
src = (
66+
"describe('d', () => {\n"
67+
" it('t', async () => {\n"
68+
" intercept((value) => respond(value, 1));\n"
69+
" });\n"
70+
"});\n"
71+
)
72+
assert _indirect(tmp_path, src) == set()

0 commit comments

Comments
 (0)