fix: resolve explicit named exports through imported bindings
(cherry picked from commit 08dd8c1fb7)
This commit is contained in:
1 parent
2a8b97af65
commit
b961460d48
2 files changed
+305
-2
No files matched your search
@@ -935,6 +935,7 @@ def _apply_symbol_resolution_facts(
|
||||
changed = True
|
||||
|
||||
named_exports_by_file: dict[Path, dict[str, tuple[Path, str]]] = {}
|
||||
export_origins: dict[tuple[Path, str], set[tuple[Path, str]]] = {}
|
||||
star_exports_by_file: dict[Path, list[Path]] = {}
|
||||
|
||||
for star_fact in facts.star_exports:
|
||||
@@ -996,7 +997,14 @@ def _apply_symbol_resolution_facts(
|
||||
if origin is None and (file_path, export_fact.local_name) in symbol_nodes:
|
||||
origin = (file_path, export_fact.local_name)
|
||||
if origin is None:
|
||||
# An explicit export remains evidence even when extraction did not
|
||||
# materialize its binding. Do not mistake it for an absent export.
|
||||
if export_fact.local_name is not None:
|
||||
export_origins.setdefault((file_path, export_fact.exported_name), set()).add(
|
||||
(file_path, export_fact.local_name)
|
||||
)
|
||||
continue
|
||||
export_origins.setdefault((file_path, export_fact.exported_name), set()).add(origin)
|
||||
named_exports_by_file.setdefault(file_path, {})[export_fact.exported_name] = origin
|
||||
if origin[0] != file_path:
|
||||
source_id = source_file_id.get(file_path)
|
||||
@@ -1032,6 +1040,65 @@ def _apply_symbol_resolution_facts(
|
||||
return resolved
|
||||
return key
|
||||
|
||||
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.
|
||||
if key in seen:
|
||||
return set()
|
||||
seen = seen | {key}
|
||||
origins = export_origins.get(key)
|
||||
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()
|
||||
for origin in origins:
|
||||
if origin == key:
|
||||
candidates.add(origin)
|
||||
else:
|
||||
candidates.update(exported_candidates(origin, seen) or {origin})
|
||||
return candidates
|
||||
|
||||
# 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.
|
||||
export_sites: dict[tuple[Path, str, Path], list[_SymbolExportFact]] = {}
|
||||
for export_fact in facts.exports:
|
||||
if export_fact.target_path is not None and export_fact.target_name is not None:
|
||||
site = (
|
||||
export_fact.file_path.resolve(),
|
||||
f"L{export_fact.line}",
|
||||
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:
|
||||
continue
|
||||
target_file = edge.get("target_file")
|
||||
source_path = _js_source_path(str(edge.get("source_file", "")), root)
|
||||
if not target_file or source_path is None:
|
||||
continue
|
||||
target_path = Path(target_file)
|
||||
site = (source_path, str(edge.get("source_location", "")), target_path.resolve())
|
||||
for export_fact in export_sites.get(site, []):
|
||||
expected = _make_id(_file_stem(target_path), export_fact.target_name)
|
||||
if edge.get("target") != expected:
|
||||
continue
|
||||
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)))
|
||||
if target_id in owned_ids:
|
||||
edge["target"] = target_id
|
||||
edge["target_file"] = node_sources[target_id]
|
||||
break
|
||||
|
||||
for import_fact in facts.imports:
|
||||
source_id = source_file_id.get(import_fact.file_path.resolve())
|
||||
if source_id is None:
|
||||
@@ -1227,8 +1294,13 @@ def _js_exported_declaration_names(node, source: bytes) -> list[str]:
|
||||
if declaration is None:
|
||||
return names
|
||||
|
||||
if declaration.type == "lexical_declaration":
|
||||
names.extend(alias for alias, _target in _js_lexical_aliases(declaration, source))
|
||||
if declaration.type in ("lexical_declaration", "variable_declaration"):
|
||||
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))
|
||||
return names
|
||||
|
||||
if declaration.type in (
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
from graphify.extract import extract
|
||||
|
||||
|
||||
def test_imported_then_exported_binding_has_owned_target(tmp_path):
|
||||
files = {
|
||||
"base.ts": "export const VALUE=1;",
|
||||
"bridge.ts": 'import {VALUE} from "./base.js"; export {VALUE};',
|
||||
"api.ts": 'export {VALUE} from "./bridge.js";',
|
||||
}
|
||||
paths = []
|
||||
for name, text in files.items():
|
||||
p = tmp_path / name
|
||||
p.write_text(text)
|
||||
paths.append(p)
|
||||
graph = extract(paths, root=tmp_path, cache_root=tmp_path, parallel=False)
|
||||
ids = {n["id"] for n in graph["nodes"]}
|
||||
exports = [e for e in graph["edges"] if e["relation"] == "re_exports" and e["source"] == "api"]
|
||||
assert exports
|
||||
assert all(e["target"] in ids for e in exports), exports
|
||||
assert any(e["target"] == "base_value" for e in exports)
|
||||
|
||||
|
||||
def graph_for(tmp_path, files):
|
||||
paths = []
|
||||
for name, text in files.items():
|
||||
p = tmp_path / name
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(text)
|
||||
paths.append(p)
|
||||
return extract(paths, root=tmp_path, cache_root=tmp_path, parallel=False)
|
||||
|
||||
|
||||
def symbol_exports(graph, source):
|
||||
return [
|
||||
e
|
||||
for e in graph["edges"]
|
||||
if e["relation"] == "re_exports" and e["context"] == "re-export" and e["source"] == source
|
||||
]
|
||||
|
||||
|
||||
def test_renamed_import_and_export_keep_original_definition(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"base.ts": "export const VALUE=1;",
|
||||
"bridge.ts": 'import {VALUE as local} from "./base.js"; export {local as publicValue};',
|
||||
"second.ts": 'export {publicValue as renamed} from "./bridge.js";',
|
||||
"api.ts": 'export {renamed as finalValue} from "./second.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"base_value"}
|
||||
assert {e["target"] for e in symbol_exports(g, "second")} == {"base_value"}
|
||||
|
||||
|
||||
def test_same_line_exports_resolve_each_explicit_binding(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "export const x=2;",
|
||||
"bridge.ts": 'import {x as a} from "./left.js"; import {x as b} from "./right.js"; export {a,b};',
|
||||
"api.ts": 'export {a,b} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"left_x", "right_x"}
|
||||
|
||||
|
||||
def test_imported_but_not_exported_name_stays_unresolved(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"base.ts": "export const VALUE=1;",
|
||||
"bridge.ts": 'import {VALUE} from "./base.js"; export const other=2;',
|
||||
"api.ts": 'export {VALUE} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"bridge_value"}
|
||||
|
||||
|
||||
def test_existing_local_declaration_remains_owned(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"base.ts": "export const VALUE=1;",
|
||||
"bridge.ts": 'import {VALUE as imported} from "./base.js"; export const VALUE=2;',
|
||||
"api.ts": 'export {VALUE} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"bridge_value"}
|
||||
|
||||
|
||||
def test_type_only_forwarding_preserves_edge_metadata(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"base.ts": "export interface Shape {x:number;}",
|
||||
"bridge.ts": 'import type {Shape} from "./base.js"; export type {Shape};',
|
||||
"api.ts": 'export type {Shape} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
edges = symbol_exports(g, "api")
|
||||
assert {e["target"] for e in edges} == {"base_shape"}
|
||||
assert all(
|
||||
e.get("type_only") is True and e["source_file"] == "api.ts" and e["source_location"] == "L1"
|
||||
for e in edges
|
||||
)
|
||||
|
||||
|
||||
def test_export_cycle_does_not_invent_definition(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path, {"a.ts": 'export {VALUE} from "./b.js";', "b.ts": 'export {VALUE} from "./a.js";'}
|
||||
)
|
||||
ids = {n["id"] for n in g["nodes"]}
|
||||
assert all(e["target"] not in ids for e in symbol_exports(g, "a") + symbol_exports(g, "b"))
|
||||
|
||||
|
||||
def test_conflicting_named_exports_do_not_choose_last_definition(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "export const x=2;",
|
||||
"bridge.ts": 'export {x} from "./left.js"; export {x} from "./right.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "bridge")} == {"left_x", "right_x"}
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"bridge_x"}
|
||||
|
||||
|
||||
def test_conflicting_star_exports_do_not_choose_first_definition(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "export const x=2;",
|
||||
"bridge.ts": 'export * from "./left.js"; export * from "./right.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"bridge_x"}
|
||||
|
||||
|
||||
def test_unrepresented_explicit_star_export_blocks_false_unique_target(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "const {x}=getValues(); export {x};",
|
||||
"bridge.ts": 'export * from "./left.js"; export * from "./right.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"bridge_x"}
|
||||
|
||||
|
||||
def test_star_branch_without_export_does_not_block_unique_target(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "export const other=2;",
|
||||
"bridge.ts": 'export * from "./left.js"; export * from "./right.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"left_x"}
|
||||
|
||||
|
||||
def test_explicit_named_export_overrides_star_ambiguity(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "export const x=2;",
|
||||
"bridge.ts": 'export * from "./left.js"; export * from "./right.js"; export {x} from "./left.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"left_x"}
|
||||
|
||||
|
||||
def test_private_name_in_star_branch_is_not_an_export(tmp_path):
|
||||
g = graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"left.ts": "export const x=1;",
|
||||
"right.ts": "const x=2; export const other=3;",
|
||||
"bridge.ts": 'export * from "./left.js"; export * from "./right.js";',
|
||||
"api.ts": 'export {x} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
assert {e["target"] for e in symbol_exports(g, "api")} == {"left_x"}
|
||||
|
||||
|
||||
def test_repointed_target_file_tracks_definition_and_preserves_source_site(tmp_path, monkeypatch):
|
||||
from pathlib import Path
|
||||
import graphify.extractors.resolution as resolution
|
||||
|
||||
original = resolution._apply_symbol_resolution_facts
|
||||
observed = []
|
||||
|
||||
def tracked(paths, nodes, edges, root, facts):
|
||||
def authored():
|
||||
return next(
|
||||
e
|
||||
for e in edges
|
||||
if e.get("relation") == "re_exports"
|
||||
and e.get("context") == "re-export"
|
||||
and Path(e["source_file"]).name == "api.ts"
|
||||
)
|
||||
|
||||
before = dict(authored())
|
||||
original(paths, nodes, edges, root, facts)
|
||||
observed.append((before, dict(authored())))
|
||||
|
||||
monkeypatch.setattr(resolution, "_apply_symbol_resolution_facts", tracked)
|
||||
graph_for(
|
||||
tmp_path,
|
||||
{
|
||||
"base.ts": "export const VALUE=1;",
|
||||
"bridge.ts": 'import {VALUE} from "./base.js"; export {VALUE};',
|
||||
"api.ts": 'export {VALUE} from "./bridge.js";',
|
||||
},
|
||||
)
|
||||
before, after = observed[0]
|
||||
assert Path(before["target_file"]).resolve() == (tmp_path / "bridge.ts").resolve()
|
||||
assert Path(after["target_file"]).resolve() == (tmp_path / "base.ts").resolve()
|
||||
assert {k: v for k, v in before.items() if k not in {"target", "target_file"}} == {
|
||||
k: v for k, v in after.items() if k not in {"target", "target_file"}
|
||||
}
|
||||
Reference in new issue
Block a user