@@ -29,9 +29,9 @@ export function elideImports(
29
29
const typeChecker = getTypeChecker ( ) ;
30
30
31
31
// Collect all imports and used identifiers
32
- const specialCaseNames = new Set < string > ( ) ;
33
32
const usedSymbols = new Set < ts . Symbol > ( ) ;
34
33
const imports : ts . ImportDeclaration [ ] = [ ] ;
34
+
35
35
ts . forEachChild ( sourceFile , function visit ( node ) {
36
36
// Skip removed nodes
37
37
if ( removedNodes . includes ( node ) ) {
@@ -45,20 +45,22 @@ export function elideImports(
45
45
return ;
46
46
}
47
47
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
+ }
57
61
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 ) ;
62
64
}
63
65
64
66
ts . forEachChild ( node , visit ) ;
@@ -69,10 +71,6 @@ export function elideImports(
69
71
}
70
72
71
73
const isUnused = ( node : ts . Identifier ) => {
72
- if ( specialCaseNames . has ( node . text ) ) {
73
- return false ;
74
- }
75
-
76
74
const symbol = typeChecker . getSymbolAtLocation ( node ) ;
77
75
78
76
return symbol && ! usedSymbols . has ( symbol ) ;
0 commit comments