Skip to content

Commit 3bc347c

Browse files
Alanhansl
Alan
authored andcommitted
refactor: use getExportSpecifierLocalTargetSymbol and getShorthandAssignmentValueSymbol to handle symbols lookups
Use typechecker methods to handle special cases for `ExportSpecifier` and `ShorthandPropertyAssignment`
1 parent 5e294ec commit 3bc347c

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

packages/ngtools/webpack/src/transformers/elide_imports.ts

+16-18
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export function elideImports(
2929
const typeChecker = getTypeChecker();
3030

3131
// Collect all imports and used identifiers
32-
const specialCaseNames = new Set<string>();
3332
const usedSymbols = new Set<ts.Symbol>();
3433
const imports: ts.ImportDeclaration[] = [];
34+
3535
ts.forEachChild(sourceFile, function visit(node) {
3636
// Skip removed nodes
3737
if (removedNodes.includes(node)) {
@@ -45,20 +45,22 @@ export function elideImports(
4545
return;
4646
}
4747

48-
if (ts.isIdentifier(node)) {
49-
const symbol = typeChecker.getSymbolAtLocation(node);
50-
if (symbol) {
51-
usedSymbols.add(symbol);
52-
}
53-
} else if (ts.isExportSpecifier(node)) {
54-
// Export specifiers return the non-local symbol from the above
55-
// so check the name string instead
56-
specialCaseNames.add((node.propertyName || node.name).text);
48+
let symbol: ts.Symbol | undefined;
49+
50+
switch (node.kind) {
51+
case ts.SyntaxKind.Identifier:
52+
symbol = typeChecker.getSymbolAtLocation(node);
53+
break;
54+
case ts.SyntaxKind.ExportSpecifier:
55+
symbol = typeChecker.getExportSpecifierLocalTargetSymbol(node as ts.ExportSpecifier);
56+
break;
57+
case ts.SyntaxKind.ShorthandPropertyAssignment:
58+
symbol = typeChecker.getShorthandAssignmentValueSymbol(node);
59+
break;
60+
}
5761

58-
return;
59-
} else if (ts.isShorthandPropertyAssignment(node)) {
60-
// Shorthand property assignments return the object property's symbol not the import's
61-
specialCaseNames.add(node.name.text);
62+
if (symbol) {
63+
usedSymbols.add(symbol);
6264
}
6365

6466
ts.forEachChild(node, visit);
@@ -69,10 +71,6 @@ export function elideImports(
6971
}
7072

7173
const isUnused = (node: ts.Identifier) => {
72-
if (specialCaseNames.has(node.text)) {
73-
return false;
74-
}
75-
7674
const symbol = typeChecker.getSymbolAtLocation(node);
7775

7876
return symbol && !usedSymbols.has(symbol);

0 commit comments

Comments
 (0)