keep js nested functions scoped so same-named calls elsewhere don't bind to them

(cherry picked from commit deb5a1cf56)
This commit is contained in:
smngvlkz authored and safishamsi committed 2026-10-27 17:58:41 +00:00
1 parent 20dbabb47e
commit c4858593c2
2 files changed
+129 -4

No files matched your search

+45 -4
View File
@@ -4012,6 +4012,8 @@ def _scan_js_nested_function_declarations(
container_node, parent_nid: str, *, source: bytes, config,
add_node, add_edge, callable_def_nids: set | None,
local_bound_names: dict | None, function_bodies: list,
scope_parents: dict[str, str] | None = None,
lexical_nids_by_scope: dict[str, dict[str, str]] | None = None,
) -> None:
"""Emit a node + `contains` edge for every named `function`/generator
declaration lexically nested inside *container_node*, scoped under
@@ -4044,6 +4046,14 @@ def _scan_js_nested_function_declarations(
nested_nid = _make_id(parent_nid, func_name)
add_node(nested_nid, f"{func_name}()", line)
add_edge(parent_nid, nested_nid, "contains", line)
# Record the nesting so a call resolves to the nearest `inner`
# in scope, not to whichever same-named function the file-wide
# name map saw last (the Python #3405 scheme).
if scope_parents is not None:
scope_parents[nested_nid] = parent_nid
if lexical_nids_by_scope is not None:
lexical_nids_by_scope.setdefault(parent_nid, {})[func_name] = nested_nid
lexical_nids_by_scope.setdefault(nested_nid, {})[func_name] = nested_nid
if callable_def_nids is not None:
callable_def_nids.add(nested_nid)
if local_bound_names is not None:
@@ -4057,6 +4067,8 @@ def _scan_js_nested_function_declarations(
callable_def_nids=callable_def_nids,
local_bound_names=local_bound_names,
function_bodies=function_bodies,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope,
)
elif child.type in _JS_FUNCTION_VALUE_TYPES:
# An anonymous arrow/function expression is not itself a node, but a
@@ -4068,6 +4080,8 @@ def _scan_js_nested_function_declarations(
callable_def_nids=callable_def_nids,
local_bound_names=local_bound_names,
function_bodies=function_bodies,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope,
)
else:
_scan_js_nested_function_declarations(
@@ -4076,6 +4090,8 @@ def _scan_js_nested_function_declarations(
callable_def_nids=callable_def_nids,
local_bound_names=local_bound_names,
function_bodies=function_bodies,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope,
)
@@ -4354,7 +4370,9 @@ def _js_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,
callable_def_nids: set | None = None,
local_bound_names: dict | None = None,
closure_locals_by_body: dict | None = None,
config=None, js_member_groups: dict | None = None) -> bool:
config=None, js_member_groups: dict | None = None,
scope_parents: dict | None = None,
lexical_nids_by_scope: dict | None = None) -> bool:
"""Handle lexical_declaration (arrow functions, CJS requires, module-level const literals) for JS/TS. Returns True if handled."""
# CommonJS / prototype member assignments whose value is a function:
# exports.X = () => {} → file-contained function X()
@@ -4569,6 +4587,8 @@ def _js_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,
callable_def_nids=callable_def_nids,
local_bound_names=local_bound_names,
function_bodies=function_bodies,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope,
)
# #3408: `this.X = fn` members were captured only
# when the enclosing function was a DECLARATION;
@@ -8372,6 +8392,8 @@ def _extract_generic(
callable_def_nids=callable_def_nids,
local_bound_names=local_bound_names,
function_bodies=function_bodies,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope,
)
if config.ts_module == "tree_sitter_python":
_scan_python_nested_function_declarations(
@@ -8530,7 +8552,9 @@ def _extract_generic(
parent_class_nid, add_node, add_edge,
callable_def_nids, local_bound_names,
closure_locals_by_body, config=config,
js_member_groups=js_member_groups):
js_member_groups=js_member_groups,
scope_parents=scope_parents,
lexical_nids_by_scope=lexical_nids_by_scope):
return
# TS enum members, and namespace / module containers
@@ -8773,6 +8797,12 @@ def _extract_generic(
if n["id"] not in scope_parents:
label_to_nid[normalised] = n["id"]
label_to_nid_ci[normalised.lower()] = n["id"]
elif config.ts_module in ("tree_sitter_javascript", "tree_sitter_typescript"):
# A JS/TS nested function is only visible inside its parent, and the
# scoped lookup already finds it there. Kept out of the file-wide map,
# a same-named call elsewhere (top-level, imported, or another
# function's inner) can no longer bind to it.
continue
else:
label_to_nid.setdefault(normalised, n["id"])
label_to_nid_ci.setdefault(normalised.lower(), n["id"])
@@ -8907,7 +8937,16 @@ def _extract_generic(
string names an ATTRIBUTE and is never shadowed by a local, so that path passes
the name straight through. ``loc_node`` supplies the source line.
"""
ref_nid = label_to_nid.get(ident_name)
ref_nid = None
if config.ts_module in ("tree_sitter_javascript", "tree_sitter_typescript"):
# A nested function in scope wins over the file-wide name map, the
# same lookup the direct-call path does.
curr_scope = scope_nid
while curr_scope and ref_nid is None:
ref_nid = lexical_nids_by_scope.get(curr_scope, {}).get(ident_name)
curr_scope = scope_parents.get(curr_scope)
if ref_nid is None:
ref_nid = label_to_nid.get(ident_name)
# Defer to the cross-file resolver when the name is not defined in this file
# (`from .h import fn`), or resolves to an import-surfaced FOREIGN symbol whose
# definition (and callability) lives in another file (JS/TS named imports map
@@ -9815,7 +9854,9 @@ def _extract_generic(
):
tgt_nid = None
else:
if config.ts_module == "tree_sitter_python" and not is_member_call:
if config.ts_module in (
"tree_sitter_python", "tree_sitter_javascript", "tree_sitter_typescript",
) and not is_member_call:
curr_scope = caller_nid
tgt_nid = None
while curr_scope:
+84
View File
@@ -0,0 +1,84 @@
"""A JS/TS function nested inside another (#2653) went into the file-wide name
map, so a call to that name from anywhere in the file could bind to it: two
functions with their own `run` both called the last one, and a top-level or
imported `helper` lost its callers to an unrelated inner `helper`.
"""
from __future__ import annotations
from graphify.extract import extract
def _calls(tmp_path, files):
paths = []
for name, code in files.items():
f = tmp_path / name
f.write_text(code, encoding="utf-8")
paths.append(f)
result = extract(sorted(paths), root=tmp_path)
return {(e["source"], e["target"], e["relation"]) for e in result["edges"]
if e["relation"] in ("calls", "indirect_call")}
def test_same_inner_name_in_two_functions_binds_to_its_own(tmp_path):
edges = _calls(tmp_path, {"x.ts": (
"export function first() { function run() { return 1; } register(run); return run(); }\n"
"export function second() { function run() { return 2; } register(run); return run(); }\n"
"function register(f: any) { return f; }\n"
)})
assert ("x_first", "x_first_run", "calls") in edges
assert ("x_second", "x_second_run", "calls") in edges
assert ("x_first", "x_first_run", "indirect_call") in edges
assert ("x_second", "x_second_run", "indirect_call") in edges
assert not {e for e in edges if e[0] == "x_first" and e[1] == "x_second_run"}
assert not {e for e in edges if e[0] == "x_second" and e[1] == "x_first_run"}
def test_inner_function_does_not_take_a_top_level_functions_callers(tmp_path):
edges = _calls(tmp_path, {"x.ts": (
"export function helper() { return 0; }\n"
"export function a() { function helper() { return 1; } return helper(); }\n"
"export function b() { return helper(); }\n"
)})
assert ("x_a", "x_a_helper", "calls") in edges
assert ("x_b", "x_helper", "calls") in edges
assert ("x_b", "x_a_helper", "calls") not in edges
def test_inner_function_does_not_take_an_imported_functions_callers(tmp_path):
edges = _calls(tmp_path, {
"lib.ts": "export function helper() { return 0; }\n",
"x.ts": (
'import { helper } from "./lib";\n'
"export function a() { function helper() { return 1; } return helper(); }\n"
"export function b() { return helper(); }\n"
),
})
assert ("x_b", "lib_helper", "calls") in edges
assert ("x_b", "x_a_helper", "calls") not in edges
def test_siblings_and_deeper_inner_functions_still_resolve(tmp_path):
edges = _calls(tmp_path, {"x.js": (
"function outer() {\n"
" function load() { return parse(); }\n"
" function parse() {\n"
" function step() { return load(); }\n"
" return step();\n"
" }\n"
" return load();\n"
"}\n"
)})
assert ("x_outer", "x_outer_load", "calls") in edges
assert ("x_outer_load", "x_outer_parse", "calls") in edges
assert ("x_outer_parse", "x_outer_parse_step", "calls") in edges
assert ("x_outer_parse_step", "x_outer_load", "calls") in edges
def test_arrow_components_keep_their_own_inner_functions(tmp_path):
edges = _calls(tmp_path, {"Panels.tsx": (
"export const Left = () => { function onClick() { return 1; } return onClick(); };\n"
"export const Right = () => { function onClick() { return 2; } return onClick(); };\n"
)})
assert ("panels_left", "panels_left_onclick", "calls") in edges
assert ("panels_right", "panels_right_onclick", "calls") in edges
assert ("panels_left", "panels_right_onclick", "calls") not in edges