Skip to content

Commit e6d4da2

Browse files
authored
fix(ts/isolated-dts): Add edges SymbolFlags::Value and SymbolFlags::Type in exports (#10577)
**Description:** We should add edges for all three possible namespaces because there's no mechanism to merge SymbolFlags with the same IDs. For example: ```ts const a = 1 // SymbolFlags::Value type a = number // SymbolFlags::Type class A {} // SymbolFlags::all() export { a, A } ``` Ideally, we should merge two `a`s with `SymbolFlags::all`. But it's not efficient to merge nodes in the real time. So when exporting symbols, we add edges for all possible cases. **Related issue:** - Closes #10576
1 parent 49d15df commit e6d4da2

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

.changeset/weak-shirts-count.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_typescript: patch
4+
---
5+
6+
fix(isolated_declarations): add edges `SymbolFlags::Value` and `SymbolFlags::Type` in exports

crates/swc_typescript/src/fast_dts/visitors/type_usage.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,13 @@ impl Visit for TypeUsageAnalyzer<'_> {
326326
for specifier in &node.specifiers {
327327
if let Some(name) = specifier.as_named() {
328328
if let ModuleExportName::Ident(ident) = &name.orig {
329-
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::all()), true);
329+
// We should add egdes for all three possible namespaces because there's no
330+
// mechanism to merge SymbolFlags with same Ids
331+
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::Type), true);
332+
if !node.type_only {
333+
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::Value), true);
334+
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::all()), true);
335+
}
330336
}
331337
}
332338
}
@@ -342,6 +348,10 @@ impl Visit for TypeUsageAnalyzer<'_> {
342348
fn visit_export_default_expr(&mut self, node: &ExportDefaultExpr) {
343349
self.with_source(self.source, |this| {
344350
if let Some(ident) = node.expr.as_ident() {
351+
// We should add egdes for all three possible namespaces because there's no
352+
// mechanism to merge SymbolFlags with same Ids
353+
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::Type), true);
354+
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::Value), true);
345355
this.add_edge(Symbol::new(ident.to_id(), SymbolFlags::all()), true);
346356
}
347357
node.visit_children_with(this);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```==================== .D.TS ====================
2+
3+
import type Animal from "the/jungle/book";
4+
interface King extends Animal {
5+
isLouie(): boolean;
6+
}
7+
export type { King as default };
8+
9+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type Animal from "the/jungle/book";
2+
3+
interface King extends Animal {
4+
isLouie(): boolean;
5+
}
6+
7+
export type { King as default };

0 commit comments

Comments
 (0)