Skip to content

Commit 4f9116f

Browse files
magic-akarikdy1
andauthoredAug 2, 2024··
fix(es/typescript): Analyze import chain (#9369)
- Closes #9368 --------- Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
1 parent 4190657 commit 4f9116f

11 files changed

+70
-14
lines changed
 

‎.changeset/wicked-lizards-smoke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_ecma_transforms_typescript: patch
3+
---
4+
5+
fix(es/typescript): Analyze import chain

‎crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.1.normal.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ define([
3838
//// [foo_1.ts]
3939
define([
4040
"require",
41-
"exports",
42-
"./foo_0"
43-
], function(require, exports, _foo_0) {
41+
"exports"
42+
], function(require, exports) {
4443
"use strict";
4544
Object.defineProperty(exports, "__esModule", {
4645
value: true

‎crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.2.minified.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ define([
2727
//// [foo_1.ts]
2828
define([
2929
"require",
30-
"exports",
31-
"./foo_0"
32-
], function(require, exports, _foo_0) {
30+
"exports"
31+
], function(require, exports) {
3332
Object.defineProperty(exports, "__esModule", {
3433
value: !0
3534
});

‎crates/swc/tests/tsc-references/commonJSImportNotAsPrimaryExpression.1.normal.js

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var E1;
3535
Object.defineProperty(exports, "__esModule", {
3636
value: true
3737
});
38-
var foo = require("./foo_0");
3938
var i;
4039
var x = {};
4140
var y = false;

‎crates/swc/tests/tsc-references/commonJSImportNotAsPrimaryExpression.2.minified.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ C1.s1 = !0, (E1 = E11 || (E11 = {}))[E1.A = 0] = "A", E1[E1.B = 1] = "B", E1[E1.
2121
//// [foo_1.ts]
2222
Object.defineProperty(exports, "__esModule", {
2323
value: !0
24-
}), require("./foo_0");
24+
});

‎crates/swc/tests/tsc-references/importStatementsInterfaces.1.normal.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
var C;
33
// no code gen expected
44
(function(C) {
5-
var a = A;
65
var m;
76
var p;
87
var p = {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [importStatementsInterfaces.ts]
22
var C, D, E;
3-
C || (C = {}), A, D || (D = {}), (E || (E = {})).xDist = function(x) {
3+
C || (C = {}), D || (D = {}), (E || (E = {})).xDist = function(x) {
44
return 0 - x.x;
55
};

‎crates/swc_ecma_transforms_typescript/src/strip_import_export.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::mem;
22

3-
use swc_common::collections::AHashSet;
3+
use swc_common::collections::{AHashMap, AHashSet};
44
use swc_ecma_ast::*;
55
use swc_ecma_utils::stack_size::maybe_grow_default;
66
use swc_ecma_visit::{noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith};
@@ -10,11 +10,15 @@ use crate::{strip_type::IsConcrete, ImportsNotUsedAsValues};
1010
#[derive(Debug, Default)]
1111
struct UsageCollect {
1212
id_usage: AHashSet<Id>,
13+
import_chain: AHashMap<Id, Id>,
1314
}
1415

1516
impl From<AHashSet<Id>> for UsageCollect {
1617
fn from(id_usage: AHashSet<Id>) -> Self {
17-
Self { id_usage }
18+
Self {
19+
id_usage,
20+
import_chain: Default::default(),
21+
}
1822
}
1923
}
2024

@@ -68,7 +72,15 @@ impl Visit for UsageCollect {
6872
return;
6973
};
7074

71-
get_module_ident(ts_entity_name).visit_with(self);
75+
let id = get_module_ident(ts_entity_name);
76+
77+
if n.is_export {
78+
id.visit_with(self);
79+
n.id.visit_with(self);
80+
return;
81+
}
82+
83+
self.import_chain.insert(n.id.to_id(), id.to_id());
7284
}
7385

7486
fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) {
@@ -101,6 +113,26 @@ impl UsageCollect {
101113
fn has_usage(&self, id: &Id) -> bool {
102114
self.id_usage.contains(id)
103115
}
116+
117+
fn analyze_import_chain(&mut self) {
118+
if self.import_chain.is_empty() {
119+
return;
120+
}
121+
122+
let mut new_usage = AHashSet::default();
123+
for id in &self.id_usage {
124+
let mut entry = self.import_chain.remove_entry(id);
125+
while let Some((id, next)) = entry {
126+
new_usage.insert(next);
127+
entry = self.import_chain.remove_entry(&id);
128+
}
129+
if self.import_chain.is_empty() {
130+
break;
131+
}
132+
}
133+
self.import_chain.clear();
134+
self.id_usage.extend(new_usage);
135+
}
104136
}
105137

106138
fn get_module_ident(ts_entity_name: &TsEntityName) -> &Ident {
@@ -264,6 +296,8 @@ impl VisitMut for StripImportExport {
264296
n.visit_with(&mut usage_info);
265297
n.visit_with(&mut declare_info);
266298

299+
usage_info.analyze_import_chain();
300+
267301
let mut strip_ts_import_equals = StripTsImportEquals;
268302

269303
n.retain_mut(|module_item| match module_item {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import * as mongo from 'https://deno.land/x/mongo@v0.27.0/mod.ts';
21
const mongoClient = {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Schemas } from './types.d';
2+
import AddressResource = Schemas.AddressResource;
3+
4+
// type usage
5+
const x: AddressResource = {};
6+
7+
8+
import { foo, bar } from "mod";
9+
10+
// value usage
11+
import y = foo.y;
12+
13+
// type usage
14+
import z = bar.z;
15+
16+
console.log(y as z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// type usage
2+
const x = {};
3+
import { foo } from "mod";
4+
// value usage
5+
const y = foo.y;
6+
console.log(y);

0 commit comments

Comments
 (0)
Please sign in to comment.