-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly elide TS imports even when referenced by shadowing variables (…
…#342) Fixes #298 TypeScript is required to remove imports where all bindings are never referenced in a value position, but previously it could have false positives when there are shadowed variables with the same name. Now, we do the intelligent scope-based shadowed global detection like we do with import replacement and properly elide when necessary.
- Loading branch information
1 parent
41476b0
commit 57323b6
Showing
4 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {ContextualKeyword} from "../parser/tokenizer"; | ||
import {TokenType as tt} from "../parser/tokenizer/types"; | ||
import TokenProcessor from "../TokenProcessor"; | ||
|
||
/** | ||
* Special case code to scan for imported names in ESM TypeScript. We need to do this so we can | ||
* properly get globals so we can compute shadowed globals. | ||
* | ||
* This is similar to logic in CJSImportProcessor, but trimmed down to avoid logic with CJS | ||
* replacement and flow type imports. | ||
*/ | ||
export default function getTSImportedNames(tokens: TokenProcessor): Set<string> { | ||
const importedNames = new Set(); | ||
for (let i = 0; i < tokens.tokens.length; i++) { | ||
if ( | ||
tokens.matchesAtIndex(i, [tt._import]) && | ||
!tokens.matchesAtIndex(i, [tt._import, tt.name, tt.eq]) | ||
) { | ||
collectNamesForImport(tokens, i, importedNames); | ||
} | ||
} | ||
return importedNames; | ||
} | ||
|
||
function collectNamesForImport( | ||
tokens: TokenProcessor, | ||
index: number, | ||
importedNames: Set<string>, | ||
): void { | ||
index++; | ||
|
||
if (tokens.matchesAtIndex(index, [tt.parenL])) { | ||
// Dynamic import, so nothing to do | ||
return; | ||
} | ||
|
||
if (tokens.matchesAtIndex(index, [tt.name])) { | ||
importedNames.add(tokens.identifierNameAtIndex(index)); | ||
index++; | ||
if (tokens.matchesAtIndex(index, [tt.comma])) { | ||
index++; | ||
} | ||
} | ||
|
||
if (tokens.matchesAtIndex(index, [tt.star])) { | ||
// * as | ||
index += 2; | ||
importedNames.add(tokens.identifierNameAtIndex(index)); | ||
index++; | ||
} | ||
|
||
if (tokens.matchesAtIndex(index, [tt.braceL])) { | ||
index++; | ||
collectNamesForNamedImport(tokens, index, importedNames); | ||
} | ||
} | ||
|
||
function collectNamesForNamedImport( | ||
tokens: TokenProcessor, | ||
index: number, | ||
importedNames: Set<string>, | ||
): void { | ||
while (true) { | ||
if (tokens.matchesAtIndex(index, [tt.braceR])) { | ||
return; | ||
} | ||
|
||
// We care about the local name, which might be the first token, or if there's an "as", is the | ||
// one after that. | ||
let name = tokens.identifierNameAtIndex(index); | ||
index++; | ||
if (tokens.matchesContextualAtIndex(index, ContextualKeyword._as)) { | ||
index++; | ||
name = tokens.identifierNameAtIndex(index); | ||
index++; | ||
} | ||
importedNames.add(name); | ||
if (tokens.matchesAtIndex(index, [tt.comma, tt.braceR])) { | ||
return; | ||
} else if (tokens.matchesAtIndex(index, [tt.braceR])) { | ||
return; | ||
} else if (tokens.matchesAtIndex(index, [tt.comma])) { | ||
index++; | ||
} else { | ||
throw new Error(`Unexpected token: ${JSON.stringify(tokens.tokens[index])}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters