fix: preserve export cycles and legacy declaration facts

(cherry picked from commit 0a1f06be8b)
This commit is contained in:
Vasu Bansal authored and safishamsi committed 2026-09-05 21:55:33 +01:00
1 parent b961460d48
commit 3993351c60
2 files changed
+152 -15

No files matched your search

+26 -15
View File
@@ -1042,25 +1042,31 @@ def _apply_symbol_resolution_facts(
def exported_candidates(
key: tuple[Path, str], seen: frozenset[tuple[Path, str]]
) -> set[tuple[Path, str]]:
# Preserve explicitly exported origins even without owned declarations.
# Missing extraction must not turn an ambiguous export into a unique one.
) -> tuple[set[tuple[Path, str]], bool]:
# Distinguish a cycle cutoff from an absent export. A cycle contributes
# no origin; a named export of an unrepresented binding remains unknown.
if key in seen:
return set()
return set(), True
seen = seen | {key}
origins = export_origins.get(key)
candidates: set[tuple[Path, str]] = set()
cyclic = False
if origins is None:
candidates: set[tuple[Path, str]] = set()
for path in star_exports_by_file.get(key[0], []):
candidates.update(exported_candidates((path, key[1]), seen))
return candidates
candidates = set()
branch, branch_cyclic = exported_candidates((path, key[1]), seen)
candidates.update(branch)
cyclic |= branch_cyclic
return candidates, cyclic
for origin in origins:
if origin == key:
candidates.add(origin)
else:
candidates.update(exported_candidates(origin, seen) or {origin})
return candidates
branch, branch_cyclic = exported_candidates(origin, seen)
candidates.update(branch)
cyclic |= branch_cyclic
if not branch and not branch_cyclic:
candidates.add(origin)
return candidates, cyclic
# The structural extractor names the immediate barrel's symbol. Resolve
# that exact authored export site through the shared import/export facts,
@@ -1074,7 +1080,6 @@ def _apply_symbol_resolution_facts(
export_fact.target_path.resolve(),
)
export_sites.setdefault(site, []).append(export_fact)
node_sources = {node["id"]: node["source_file"] for node in nodes if node.get("id") and node.get("source_file")}
owned_ids = {node.get("id") for node in nodes}
for edge in edges:
if edge.get("relation") != "re_exports" or edge.get("target") in owned_ids:
@@ -1089,14 +1094,15 @@ def _apply_symbol_resolution_facts(
expected = _make_id(_file_stem(target_path), export_fact.target_name)
if edge.get("target") != expected:
continue
candidates = exported_candidates(
candidates, _ = exported_candidates(
(export_fact.target_path.resolve(), export_fact.target_name), frozenset()
)
if len(candidates) == 1:
target_id = symbol_nodes.get(next(iter(candidates)))
origin = next(iter(candidates))
target_id = symbol_nodes.get(origin)
if target_id in owned_ids:
edge["target"] = target_id
edge["target_file"] = node_sources[target_id]
edge["target_file"] = str(path_by_resolved.get(origin[0], origin[0]))
break
for import_fact in facts.imports:
@@ -1295,12 +1301,17 @@ def _js_exported_declaration_names(node, source: bytes) -> list[str]:
return names
if declaration.type in ("lexical_declaration", "variable_declaration"):
# Preserve legacy aggregate-pattern facts for identifier-valued lexical
# declarations; individual destructured bindings are a separate feature.
names.extend(alias for alias, _target in _js_lexical_aliases(declaration, source))
for declarator in declaration.named_children:
if declarator.type != "variable_declarator":
continue
name_node = declarator.child_by_field_name("name")
if name_node is not None and name_node.type == "identifier":
names.append(_read_text(name_node, source))
name = _read_text(name_node, source)
if name not in names:
names.append(name)
return names
if declaration.type in (
+126
View File
@@ -0,0 +1,126 @@
from pathlib import Path
from graphify.extract import extract
def graph_for(root, files):
for name, text in files.items():
(root / name).write_text(text)
return extract([root / name for name in files], root=root, cache_root=root, parallel=False)
def test_star_cycle_with_real_definition_does_not_mask_unique_origin(tmp_path):
g = graph_for(
tmp_path,
{
"base.ts": "export const x=1;",
"a.ts": 'export * from "./b.js"; export * from "./base.js";',
"b.ts": 'export {x} from "./a.js";',
"api.ts": 'export {x} from "./a.js";',
},
)
edges = [
e
for e in g["edges"]
if e["source"] == "api" and e["relation"] == "re_exports" and e["context"] == "re-export"
]
assert {e["target"] for e in edges} == {"base_x"}
def test_sourceless_owned_node_is_not_an_export_definition(tmp_path):
from graphify.extractors.resolution import _apply_symbol_resolution_facts
from graphify.extractors.models import _SymbolResolutionFacts, _SymbolExportFact
from graphify.extractors.base import _make_id, _file_stem
base = tmp_path / "base.ts"
api = tmp_path / "api.ts"
nodes = [{"id": "orphan", "label": "x"}]
edge = {
"source": _make_id(str(api)),
"target": _make_id(_file_stem(base), "x"),
"relation": "re_exports",
"context": "re-export",
"source_file": str(api),
"source_location": "L1",
"target_file": str(base),
}
facts = _SymbolResolutionFacts()
facts.exports.extend(
[
_SymbolExportFact(base, "x", 1, local_name="x"),
_SymbolExportFact(api, "x", 1, target_path=base, target_name="x"),
]
)
_apply_symbol_resolution_facts([base, api], nodes, [edge], tmp_path, facts)
assert edge["target"] != "orphan"
assert edge["target_file"] == str(base)
def test_legacy_aggregate_pattern_facts_and_nodes_are_preserved(tmp_path):
from graphify.extractors.resolution import (
_parse_js_tree,
_walk_js_tree,
_js_exported_declaration_names,
)
for name, pattern, value in [("object", "{x}", "{x:1}"), ("array", "[x]", "[1]")]:
root = tmp_path / name
root.mkdir()
g = graph_for(
root,
{
"decl.ts": f"const holder={value}; export const {pattern}=holder;",
"consumer.ts": 'import {x} from "./decl.js";',
},
)
source, tree = _parse_js_tree(root / "decl.ts")
assert [
_js_exported_declaration_names(n, source)
for n in _walk_js_tree(tree)
if n.type == "export_statement"
] == [[pattern]]
assert any(n["id"] == "decl_x" and n["label"] == pattern for n in g["nodes"])
def test_identifier_alias_fact_and_binding_are_preserved(tmp_path):
from graphify.extractors.resolution import (
_parse_js_tree,
_walk_js_tree,
_js_exported_declaration_names,
)
g = graph_for(
tmp_path,
{
"decl.ts": "const original=1; export const alias=original;",
"consumer.ts": 'import {alias} from "./decl.js";',
},
)
source, tree = _parse_js_tree(tmp_path / "decl.ts")
assert [
_js_exported_declaration_names(n, source)
for n in _walk_js_tree(tree)
if n.type == "export_statement"
] == [["alias"]]
assert any(
e["source"] == "consumer" and e["target"] == "decl_alias" and e["relation"] == "imports"
for e in g["edges"]
)
def test_longer_export_cycle_with_definition_remains_resolvable(tmp_path):
g = graph_for(
tmp_path,
{
"base.ts": "export const x=1;",
"a.ts": 'export * from "./b.js"; export * from "./base.js";',
"b.ts": 'export {x} from "./c.js";',
"c.ts": 'export {x} from "./a.js";',
"api.ts": 'export {x} from "./a.js";',
},
)
edges = [
e
for e in g["edges"]
if e["source"] == "api" and e["relation"] == "re_exports" and e["context"] == "re-export"
]
assert {e["target"] for e in edges} == {"base_x"}