-
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.
Elide import = statements that are only used as a type (#441)
Fixes #422 In both CJS and ESM, we now look at the identifier in `import A =` and decide whether to elide the entire statement in a similar way to normal TS import elision. Since there are only two forms that these statements can have, I just special cased the two when deleting statements. Technically to implement this totally correctly, I'd need something like a graph traversal: in an `import A = B.C;` statement, `B` is referenced as a value if `A` is ever referenced as a value, so deciding if an import should be elided requires following an arbitrary number of import statements. In practice, I think this syntax is only ever used to pull in types (for values, you can just use `const`, and the syntax is obscure anyway), so I treat it as a type statement that gets elided.
- Loading branch information
1 parent
fd0f689
commit bd1ee84
Showing
7 changed files
with
127 additions
and
9 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
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,29 @@ | ||
import {TokenType as tt} from "../parser/tokenizer/types"; | ||
import TokenProcessor from "../TokenProcessor"; | ||
|
||
export default function elideImportEquals(tokens: TokenProcessor): void { | ||
// import | ||
tokens.removeInitialToken(); | ||
// name | ||
tokens.removeToken(); | ||
// = | ||
tokens.removeToken(); | ||
// name or require | ||
tokens.removeToken(); | ||
// Handle either `import A = require('A')` or `import A = B.C.D`. | ||
if (tokens.matches1(tt.parenL)) { | ||
// ( | ||
tokens.removeToken(); | ||
// path string | ||
tokens.removeToken(); | ||
// ) | ||
tokens.removeToken(); | ||
} else { | ||
while (tokens.matches1(tt.dot)) { | ||
// . | ||
tokens.removeToken(); | ||
// name | ||
tokens.removeToken(); | ||
} | ||
} | ||
} |
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