|
| 1 | +"""A block-scoped `let`/`const` binding must not shadow indirect_call |
| 2 | +references outside the block it is actually scoped to. |
| 3 | +
|
| 4 | +`_js_local_bound_names` collected `let`/`const` declarator targets (and a |
| 5 | +`for`/`for-of` loop's own binding) into one FUNCTION-WIDE shadow set, but |
| 6 | +both are block-scoped in JS, unlike `var` (function-scoped, hoisted). A |
| 7 | +reference to a same-named module callable made OUTSIDE the block that binds |
| 8 | +the name had its genuine indirect_call edge suppressed (#2822): |
| 9 | +
|
| 10 | +- a `const`/`let` declared inside any nested block (`if`, `try`, a bare `{}`) |
| 11 | +- a `for...of`/`for...in` loop binding, referenced after the loop ends |
| 12 | +- a closure that outlives the loop and still references the loop binding |
| 13 | +
|
| 14 | +`var` is exempt from all of this — it is genuinely function-scoped |
| 15 | +regardless of how deeply it is nested, so a `var` with the same shapes must |
| 16 | +still correctly shadow a same-named module callable everywhere in the |
| 17 | +function, including outside the block/loop that declares it. And the #2606 |
| 18 | +fix (a loop binding shadows references INSIDE its own loop) must not |
| 19 | +regress. |
| 20 | +""" |
| 21 | +import os |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +from graphify.extract import extract, extract_js |
| 25 | + |
| 26 | + |
| 27 | +def _extract_js_dir(tmp_path, files: dict[str, str]): |
| 28 | + base = tmp_path / "src" |
| 29 | + base.mkdir() |
| 30 | + for name, body in files.items(): |
| 31 | + (base / name).write_text(body) |
| 32 | + old = os.getcwd() |
| 33 | + try: |
| 34 | + os.chdir(tmp_path) |
| 35 | + r = extract( |
| 36 | + [Path("src") / name for name in files], |
| 37 | + cache_root=Path(".cache"), parallel=False, |
| 38 | + ) |
| 39 | + finally: |
| 40 | + os.chdir(old) |
| 41 | + nid = {n["label"].rstrip("()"): n["id"] for n in r["nodes"]} |
| 42 | + return r, nid |
| 43 | + |
| 44 | + |
| 45 | +def _indirect(r): |
| 46 | + return {(e["source"], e["target"]) for e in r["edges"] if e["relation"] == "indirect_call"} |
| 47 | + |
| 48 | + |
| 49 | +def test_block_scoped_const_does_not_shadow_a_reference_outside_the_block(tmp_path): |
| 50 | + """Case 1 from the issue: a `const` declared inside an `if` block must not |
| 51 | + suppress a reference to a same-named module callable outside that block.""" |
| 52 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 53 | + "function k(x){ return x; }\n" |
| 54 | + "export function run(flag, p){\n" |
| 55 | + " if (flag) { const k = 1; void k; }\n" |
| 56 | + " return p.submit(k);\n" |
| 57 | + "}\n" |
| 58 | + )}) |
| 59 | + assert (nid["run"], nid["k"]) in _indirect(r) |
| 60 | + |
| 61 | + |
| 62 | +def test_for_of_binding_does_not_shadow_a_reference_after_the_loop(tmp_path): |
| 63 | + """Case 2 from the issue: referenced after the loop ends, not inside it.""" |
| 64 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 65 | + "function k(x){ return x; }\n" |
| 66 | + "export function run(p){ for (const k of [1]) void k; p.submit(k); }\n" |
| 67 | + )}) |
| 68 | + assert (nid["run"], nid["k"]) in _indirect(r) |
| 69 | + |
| 70 | + |
| 71 | +def test_for_of_binding_does_not_shadow_a_closure_that_outlives_the_loop(tmp_path): |
| 72 | + """Case 3 from the issue: a closure returned after the loop still refers |
| 73 | + to the module callable, not the loop's own binding.""" |
| 74 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 75 | + "function k(x){ return x; }\n" |
| 76 | + "export function run(p){ for (const k of [1]) void k; return () => p.submit(k); }\n" |
| 77 | + )}) |
| 78 | + assert (nid["run"], nid["k"]) in _indirect(r) |
| 79 | + |
| 80 | + |
| 81 | +def test_for_of_binding_still_shadows_a_reference_inside_its_own_loop(tmp_path): |
| 82 | + """Control from the issue (#2606 must not regress): a reference INSIDE the |
| 83 | + loop still refers to the loop's own binding, not the module callable.""" |
| 84 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 85 | + "function k(x){ return x; }\n" |
| 86 | + "export function run(p){ for (const k of [1]) p.submit(k); }\n" |
| 87 | + )}) |
| 88 | + assert (nid["run"], nid["k"]) not in _indirect(r) |
| 89 | + |
| 90 | + |
| 91 | +def test_c_style_for_let_still_shadows_a_reference_inside_its_own_loop(tmp_path): |
| 92 | + """A C-style `for (let i = ...)` binding shadows a reference inside the |
| 93 | + loop, exactly like for-of, while a reference to the same name outside the |
| 94 | + loop still correctly resolves. Both calls read identically as source |
| 95 | + text (`p.submit(k)`), so this asserts on `source_location` (the outside |
| 96 | + call, L4) rather than just edge presence -- a pair-membership check |
| 97 | + alone cannot distinguish "only the outside call resolved" from "both |
| 98 | + wrongly resolved".""" |
| 99 | + f = tmp_path / "a.js" |
| 100 | + f.write_text( |
| 101 | + "function k(x){ return x; }\n" |
| 102 | + "export function run(p){\n" |
| 103 | + " for (let k = 0; k < 3; k++) { p.submit(k); }\n" |
| 104 | + " return p.submit(k);\n" |
| 105 | + "}\n" |
| 106 | + ) |
| 107 | + r = extract_js(f) |
| 108 | + indirect = [e for e in r["edges"] if e["relation"] == "indirect_call"] |
| 109 | + assert len(indirect) == 1, ( |
| 110 | + f"expected exactly one indirect_call edge (the outside call only), got {indirect}" |
| 111 | + ) |
| 112 | + assert indirect[0]["source_location"] == "L4", ( |
| 113 | + "the resolved edge must be the outside-the-loop call, not the inside one" |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def test_var_inside_a_block_still_shadows_a_reference_outside_the_block(tmp_path): |
| 118 | + """var is genuinely function-scoped (hoisted): unlike the let/const case |
| 119 | + above, a var declared inside an if-block must still shadow a reference |
| 120 | + outside that block, everywhere in the function.""" |
| 121 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 122 | + "function k(x){ return x; }\n" |
| 123 | + "export function run(flag, p){\n" |
| 124 | + " if (flag) { var k = 1; void k; }\n" |
| 125 | + " return p.submit(k);\n" |
| 126 | + "}\n" |
| 127 | + )}) |
| 128 | + assert (nid["run"], nid["k"]) not in _indirect(r) |
| 129 | + |
| 130 | + |
| 131 | +def test_var_for_of_binding_still_shadows_a_reference_after_the_loop(tmp_path): |
| 132 | + """Same as above for a var-form for-of loop binding: hoisted, so it must |
| 133 | + still shadow a reference made after the loop ends.""" |
| 134 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 135 | + "function k(x){ return x; }\n" |
| 136 | + "export function run(p){ for (var k of [1]) void k; return p.submit(k); }\n" |
| 137 | + )}) |
| 138 | + assert (nid["run"], nid["k"]) not in _indirect(r) |
| 139 | + |
| 140 | + |
| 141 | +def test_genuine_reference_elsewhere_still_emits(tmp_path): |
| 142 | + """Widening the shadow scope must not blanket-suppress: a same-named |
| 143 | + callable referenced from a function that does not bind it at all still |
| 144 | + resolves.""" |
| 145 | + r, nid = _extract_js_dir(tmp_path, {"a.js": ( |
| 146 | + "function k(x){ return x; }\n" |
| 147 | + "export function run(flag, p){\n" |
| 148 | + " if (flag) { const k = 1; void k; }\n" |
| 149 | + " return p.submit(k);\n" |
| 150 | + "}\n" |
| 151 | + "export function elsewhere(pool) { pool.submit(k); }\n" |
| 152 | + )}) |
| 153 | + assert (nid["elsewhere"], nid["k"]) in _indirect(r) |
0 commit comments