fix: resolve calls through destructured CommonJS barrel requires

A destructured require that reaches a symbol through a barrel
(`const { SourceCode } = require('./index')`, where `index.js` re-exports
`SourceCode` from another module) left the symbol import edge on the
barrel. The barrel defines no node, so cross-file call resolution found no
import evidence and dropped the `calls` edge.

Keep the required target file and imported name on the structural require
edge, then follow the target's exports to the declaring symbol before
cross-file resolution runs. The keys are internal and removed from the
final graph.

Fixes #5034

(cherry picked from commit 689e18a87e)
This commit is contained in:
sen authored and safishamsi committed 2026-11-09 14:09:31 +00:00
1 parent 8a5756266a
commit bce80814d7
3 files changed
+73

No files matched your search

+2
View File
@@ -4245,6 +4245,8 @@ def _require_imports_js(node, source: bytes, importer_nid: str, stem: str, edges
"source_file": str_path,
"source_location": f"L{line}",
"weight": 1.0,
"_require_target_file": str(resolved_path),
"_require_imported_name": sym,
})
return found
+21
View File
@@ -1281,6 +1281,9 @@ def _apply_symbol_resolution_facts(
or facts.uses
or facts.module_imports
):
for edge in edges:
edge.pop("_require_imported_name", None)
edge.pop("_require_target_file", None)
return
path_by_resolved = {_resolve_cached(path): path for path in paths}
@@ -1634,6 +1637,24 @@ def _apply_symbol_resolution_facts(
candidates.add(origin)
return candidates, cyclic
# A destructured `require` names the immediate barrel's symbol. When that
# target is not a node (the barrel defines nothing), follow the barrel to
# the file that declares the symbol, so cross-file call resolution sees
# import evidence instead of a dangling barrel reference.
for edge in edges:
imported_name = edge.pop("_require_imported_name", None)
target_file = edge.pop("_require_target_file", None)
if edge.get("relation") != "imports" or not imported_name or not target_file:
continue
origin_path, origin_symbol = resolve_exported_origin(
Path(str(target_file)), str(imported_name)
)
target_id = symbol_nodes.get((origin_path, origin_symbol))
if target_id is None or edge.get("target") == target_id:
continue
edge["target"] = target_id
edge["target_file"] = str(path_by_resolved.get(origin_path, origin_path))
# The structural extractor names the immediate barrel's symbol. Resolve
# that exact authored export site through the shared import/export facts,
# including `import {x}; export {x}` bridges with no local declaration.
+50
View File
@@ -0,0 +1,50 @@
"""#5034: a constructor reached through a destructured require of a CommonJS
barrel must resolve to the class in the module the barrel re-exports.
`const { SourceCode } = require('./index')` where `index.js` does
`const SourceCode = require('./source'); module.exports = { SourceCode }`
used to leave the call's reference on the barrel, which defines nothing, so
the `calls` edge disappeared even though the direct-require case resolved.
"""
from __future__ import annotations
from pathlib import Path
from graphify.extract import extract
def test_constructor_call_resolves_through_destructured_barrel_require(tmp_path):
files = {
"source.js": "class SourceCode {}\nmodule.exports = SourceCode;\n",
"index.js": (
"const SourceCode = require('./source');\n"
"module.exports = { SourceCode };\n"
),
"consumer.js": (
"const { SourceCode } = require('./index');\n"
"function create() { return new SourceCode(); }\n"
),
}
for name, body in files.items():
(tmp_path / name).write_text(body, encoding="utf-8")
graph = extract(
[tmp_path / name for name in files],
cache_root=tmp_path / "cache",
root=tmp_path,
parallel=False,
)
nodes = {node["id"]: node for node in graph["nodes"]}
calls = [
(
nodes[edge["source"]]["label"],
nodes[edge["target"]]["label"],
Path(nodes[edge["target"]]["source_file"]).name,
)
for edge in graph["edges"]
if edge["relation"] == "calls"
and edge["source"] in nodes
and edge["target"] in nodes
]
assert ("create()", "SourceCode", "source.js") in calls, calls