Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for TS 4.5 import type modifiers #713

Merged
merged 1 commit into from Jul 1, 2022

Conversation

alangpierce
Copy link
Owner

Fixes #668

Details on the syntax are described here:
https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names

This was implemented in Babel here: babel/babel#13802

In terms of behavior, the type syntax is almost a no-op in valid code; we
already scan the file to see which identifiers have been used in a value or type
context and remove type imports automatically, and this PR doesn't change that.
However, there are a few subtle differences:

  • When targeting commonjs, if an imported name has a type modifier and is used
    in a value context, we do not transform the syntax to access the module like
    we did before. For example, this comes up if accessing a global value with the
    same name as an imported type.
  • In both CJS and ESM targets, code that imports and then re-exports a type
    needs to have that export elided. Previously, this was a situation where TS
    doesn't know whether the name is a type or not, so it included the export as a
    value.

Right now, there is no equivalent to --preserveValueImports, but this could be
added in the future. It almost would turn the TS transform into a purely
syntax-level transform without the need for scope analysis and variable name
tracking (thus simplifying and speeding up Sucrase if it became the standard
approach), except that even with that flag set, type-only exports are still
elided.

In terms of implementation details, the Babel parser details were a bit
AST-focused and didn't need to deal with IdentifierRoles, so I re-implemented
parsing using a simpler approach, which was also useful in the transform step.

It turns out that in both Flow and TS, you can parse an import or export
specifier by accepting anywhere from 1 to 4 identifiers, and the number of
identifiers is enough to exactly determine the syntax. This is more reliable
than matching on the names type (or typeof) and as because those are
contextual keywords that might be valid identifiers.

In the transform step, there were 5 places where we were manually walking
import/export specifiers, so it seemed best to unify that parsing into a shared
utility. This potentially has some performance cost due to the object
allocations, but it's likely to be very minor and could potentially be improved
later.

@codecov
Copy link

codecov bot commented Jul 1, 2022

Codecov Report

Merging #713 (4a9ecb2) into main (ee5879e) will increase coverage by 0.16%.
The diff coverage is 98.01%.

@@            Coverage Diff             @@
##             main     #713      +/-   ##
==========================================
+ Coverage   85.36%   85.53%   +0.16%     
==========================================
  Files          54       55       +1     
  Lines        5855     5882      +27     
  Branches     1337     1331       -6     
==========================================
+ Hits         4998     5031      +33     
+ Misses        576      574       -2     
+ Partials      281      277       -4     
Impacted Files Coverage Δ
src/util/getImportExportSpecifierInfo.ts 89.47% <89.47%> (ø)
src/CJSImportProcessor.ts 91.91% <100.00%> (-0.36%) ⬇️
src/parser/plugins/typescript.ts 86.11% <100.00%> (+0.82%) ⬆️
src/parser/traverser/statement.ts 82.85% <100.00%> (+0.20%) ⬆️
src/transformers/CJSImportTransformer.ts 88.32% <100.00%> (-0.03%) ⬇️
src/transformers/ESMImportTransformer.ts 90.07% <100.00%> (+2.93%) ⬆️
src/util/getTSImportedNames.ts 84.21% <100.00%> (-1.16%) ⬇️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@github-actions
Copy link

github-actions bot commented Jul 1, 2022

Benchmark results

Before this PR: 330.2 thousand lines per second
After this PR: 329.7 thousand lines per second

Measured change: 0.16% slower (0.79% slower to 0.82% faster)
Summary: Likely no significant difference

Fixes #668

Details on the syntax are described here:
https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names

This was implemented in Babel here: babel/babel#13802

In terms of behavior, the type syntax is *almost* a no-op in valid code; we
already scan the file to see which identifiers have been used in a value or type
context and remove type imports automatically, and this PR doesn't change that.
However, there are a few subtle differences:
* When targeting commonjs, if an imported name has a `type` modifier and is used
  in a value context, we do *not* transform the syntax to access the module like
  we did before. For example, this comes up if accessing a global value with the
  same name as an imported type.
* In both CJS and ESM targets, code that imports and then re-exports a type
  needs to have that export elided. Previously, this was a situation where TS
  doesn't know whether the name is a type or not, so it included the export as a
  value.

Right now, there is no equivalent to `--preserveValueImports`, but this could be
added in the future. It *almost* would turn the TS transform into a purely
syntax-level transform without the need for scope analysis and variable name
tracking (thus simplifying and speeding up Sucrase if it became the standard
approach), except that even with that flag set, type-only exports are still
elided.

In terms of implementation details, the Babel parser details were a bit
AST-focused and didn't need to deal with `IdentifierRole`s, so I re-implemented
parsing using a simpler approach, which was also useful in the transform step.

It turns out that in both Flow and TS, you can parse an import or export
specifier by accepting anywhere from 1 to 4 identifiers, and the number of
identifiers is enough to exactly determine the syntax. This is more reliable
than matching on the names `type` (or `typeof`) and `as` because those are
contextual keywords that might be valid identifiers.

In the transform step, there were 5 places where we were manually walking
import/export specifiers, so it seemed best to unify that parsing into a shared
utility. This potentially has some performance cost due to the object
allocations, but it's likely to be very minor and could potentially be improved
later.
@alangpierce alangpierce force-pushed the support-ts-import-type-modifiers branch from d494e93 to 4a9ecb2 Compare July 1, 2022 18:18
@alangpierce alangpierce merged commit b9bce13 into main Jul 1, 2022
@alangpierce alangpierce deleted the support-ts-import-type-modifiers branch July 1, 2022 19:08
alangpierce added a commit that referenced this pull request Jul 1, 2022
Instructions: https://github.com/alangpierce/sucrase/wiki/Porting-changes-from-Babel's-parser

976bfbbf69 fix: assure left bracket is not consumed after dot (#13695)
🚫 Bug doesn't seem to apply to Sucrase, and fix is validation only.

1d4bd31950 add missing ExportNamespaceSpecifier in spec.md (#13691)
🚫 Docs only.

b141c85b17 fix(babel-parser): delete `static` property from class static block for TS (#13680)
🚫 Only affects AST.

64d116bd6a fix(parser): [Babel8] Align error codes between Flow and TypeScript (#13294)
🚫 Only affects error reporting.

a54f041440 test(parser): add no_plugin tests for module blocks (#13714)
🚫 Test only.

b2376757d7 v7.15.4
🚫 Release only.

44388e6e81 Disallow `#a in #b in c` and similar expressions (#13727)
🚫 Only affects validation.

e82e43ca33 v7.15.5
🚫 Release only.

62e42a3f60 fix(babel-parser): Fix end of `range` of `SequenceExpression` (#13731)
🚫 AST only.

c25ec3e069 [babel-parser] add tests for static blocks with line breaks (#13734)
✅ Added similar test for Sucrase (though nothing was broken).

3c3f5205c9 Fix right precedence of Hack pipes (#13668)
✅ Given the active spec work, I filed #674 to revisit in the future.

34a27e46f8 v7.15.6
🚫 Release only.

710b3919fa [estree] Fix conversion of `PrivateName` in `MemberExpression` (#13755)
🚫 Only affects estree.

da79c90f2a fix(babel-parser): Allow line break before `assert` return type (#13771)
✅ Ported fix.

d2076a531f Store token type as number (#13768)
🚫 Optimization that's pretty much already in Sucrase.

8d0294edf8 v7.15.7
🚫 Release only.

2e2d202694 [babel 8] Materialize ESTree's `classFeatures` option (#13752)
🚫 Not relevant for Sucrase.

178d43ff17 Tokenize keywords-like identifier as new tokens (#13769)
🚫 Interesting alternative approach to Sucrase's contextual keyword enums. Doesn't seem worth it to switch to Babel's approach for now.

64f14b05fa Collect comments around parentheses in expressions (#13803)
🚫 AST only.

c4b13725aa v7.15.8
🚫 Release only.

b5907ef967 remove executable permission from files that shouldn't have it (#13873)
🚫 Babel-internal change.

62b2c5ebb9 Regenerate parser fixtures (#13882)
🚫 Babel-internal change.

c7ddb1ae93 Use `workspace:^` to specify `@babel/` dependencies (#13772)
🚫 Babel-internal change.

fb7ddf4d38 [ts] Support private methods overloads (#13876)
🚫 Issue doesn't affect Sucrase.

ddc45a5a50 Enable class static blocks by default (#13713)
🚫 Already enabled by default for Sucrase.

ad59a2c618 Caret topic (pipe operator) (#13749)
🚫 Not implementing topic token changes for now until it's finalized, see #674

718c6cb7de Handle `.mts` and `.cts` files in `@babel/preset-typescript` (#13838)
🚫 Only adds error handling to the parser. Sucrase integrations should better handle .mts and .cts, but that can be done in follow-up work.

872086a9a0 feat: support `startColumn` option (#13887)
🚫 Not relevant to Sucrase.

d5ba355867 Support TypeScript 4.5 type-only import/export specifiers (#13802)
✅ Implemented separately as #713

45308f3cba v7.16.0
🚫 Release only.

cba7f9e503 Add missing assertions type (#13905)
🚫 AST only.

68fad796b1 v7.16.2
🚫 Release only.

1fa759f989 refactor: extract tt.lt and tt.gt from tt.relation (#13892)
🚫 Already done in Sucrase. Also includes a refactor that could be reasonable to incorporate, but I'll skip that for now.

7250d2562b Simplifiy tracking of valid JSX positions (#13891)
🚫 The relevant code has changed enough that this probably isn't worth porting.

a6a526968d fix: incorrect await rejection following arrow function in parameters (#13928)
🚫 Bug fix is in code not relevant to Sucrase.

3b6f061e18 v7.16.3
🚫 Release only.

135ab837bc Throw on duplicate `__proto__` props followed by assignment (#13951)
🚫 Only affects error handling.

54c539ecc1 Refactor bindingProperty parsing (#13929)
🚫 Nothing stands out as relevant to Sucrase.

966387d263 Always expose `expressionValue` in `DirectiveLiteral` nodes (#13960)
🚫 AST only.

87fc2e76d7 Add `assertions` to `ExportNamedDeclaration` without `from` (#13957)
🚫 AST only.

99774ee9fa v7.16.4
🚫 Release only.

d30308fe8e fix: update UnexpectedPrivateField error message (#13975)
🚫 Validation only.

a470f7b479 Recover from shorthand assign exprs (#13968)
✅ We already handled this case, but I added a test to confirm.

75996cb62b Use full import specifier path in tests (#13938)
🚫 Babel-internal change.

ad1798ed48 Only bundle the release build, and don't import `src` in tests (#13978)
🚫 Babel-internal change.

f4236f43a1 Extend `hasPlugin` to accept plugin-configuration array pairs (#13982)
🚫 Sucrase uses a different config system.

2d989a983d Run tests in a native Node.js ESM environment (#13966)
🚫 Babel-internal change. (Though very cool to see!)

94af0e5c62 Improve template tokenizing (#13919)
🚫 Sucrase got rid of context, so it looks like this change isn't as relevant.

39080492f4 maintain estree string literal shape when cloned (#14039)
🚫 AST only.

7b7ab94066 v7.16.5
🚫 Release only.

7794201940 fix: handle tokens for invalid template element (#14055)
🚫 Bug not relevant to Sucrase.

5687ade5e7 v7.16.6
🚫 Release only.

ad17fe1cce fix: check preceding line break before exclamation (#14049)
✅ Fixed bug in the same way and added a test.

d1cabf6bc8 [babel 8] Add `"exports"` to every package (#14013)
🚫 Babel-internal change.

23e884048a v7.16.7
🚫 Release only.
alangpierce added a commit that referenced this pull request Jul 1, 2022
Instructions: https://github.com/alangpierce/sucrase/wiki/Porting-changes-from-Babel's-parser

976bfbbf69 fix: assure left bracket is not consumed after dot (#13695)
🚫 Bug doesn't seem to apply to Sucrase, and fix is validation only.

1d4bd31950 add missing ExportNamespaceSpecifier in spec.md (#13691)
🚫 Docs only.

b141c85b17 fix(babel-parser): delete `static` property from class static block for TS (#13680)
🚫 Only affects AST.

64d116bd6a fix(parser): [Babel8] Align error codes between Flow and TypeScript (#13294)
🚫 Only affects error reporting.

a54f041440 test(parser): add no_plugin tests for module blocks (#13714)
🚫 Test only.

b2376757d7 v7.15.4
🚫 Release only.

44388e6e81 Disallow `#a in #b in c` and similar expressions (#13727)
🚫 Only affects validation.

e82e43ca33 v7.15.5
🚫 Release only.

62e42a3f60 fix(babel-parser): Fix end of `range` of `SequenceExpression` (#13731)
🚫 AST only.

c25ec3e069 [babel-parser] add tests for static blocks with line breaks (#13734)
✅ Added similar test for Sucrase (though nothing was broken).

3c3f5205c9 Fix right precedence of Hack pipes (#13668)
✅ Given the active spec work, I filed #674 to revisit in the future.

34a27e46f8 v7.15.6
🚫 Release only.

710b3919fa [estree] Fix conversion of `PrivateName` in `MemberExpression` (#13755)
🚫 Only affects estree.

da79c90f2a fix(babel-parser): Allow line break before `assert` return type (#13771)
✅ Ported fix.

d2076a531f Store token type as number (#13768)
🚫 Optimization that's pretty much already in Sucrase.

8d0294edf8 v7.15.7
🚫 Release only.

2e2d202694 [babel 8] Materialize ESTree's `classFeatures` option (#13752)
🚫 Not relevant for Sucrase.

178d43ff17 Tokenize keywords-like identifier as new tokens (#13769)
🚫 Interesting alternative approach to Sucrase's contextual keyword enums. Doesn't seem worth it to switch to Babel's approach for now.

64f14b05fa Collect comments around parentheses in expressions (#13803)
🚫 AST only.

c4b13725aa v7.15.8
🚫 Release only.

b5907ef967 remove executable permission from files that shouldn't have it (#13873)
🚫 Babel-internal change.

62b2c5ebb9 Regenerate parser fixtures (#13882)
🚫 Babel-internal change.

c7ddb1ae93 Use `workspace:^` to specify `@babel/` dependencies (#13772)
🚫 Babel-internal change.

fb7ddf4d38 [ts] Support private methods overloads (#13876)
🚫 Issue doesn't affect Sucrase.

ddc45a5a50 Enable class static blocks by default (#13713)
🚫 Already enabled by default for Sucrase.

ad59a2c618 Caret topic (pipe operator) (#13749)
🚫 Not implementing topic token changes for now until it's finalized, see #674

718c6cb7de Handle `.mts` and `.cts` files in `@babel/preset-typescript` (#13838)
🚫 Only adds error handling to the parser. Sucrase integrations should better handle .mts and .cts, but that can be done in follow-up work.

872086a9a0 feat: support `startColumn` option (#13887)
🚫 Not relevant to Sucrase.

d5ba355867 Support TypeScript 4.5 type-only import/export specifiers (#13802)
✅ Implemented separately as #713

45308f3cba v7.16.0
🚫 Release only.

cba7f9e503 Add missing assertions type (#13905)
🚫 AST only.

68fad796b1 v7.16.2
🚫 Release only.

1fa759f989 refactor: extract tt.lt and tt.gt from tt.relation (#13892)
🚫 Already done in Sucrase. Also includes a refactor that could be reasonable to incorporate, but I'll skip that for now.

7250d2562b Simplifiy tracking of valid JSX positions (#13891)
🚫 The relevant code has changed enough that this probably isn't worth porting.

a6a526968d fix: incorrect await rejection following arrow function in parameters (#13928)
🚫 Bug fix is in code not relevant to Sucrase.

3b6f061e18 v7.16.3
🚫 Release only.

135ab837bc Throw on duplicate `__proto__` props followed by assignment (#13951)
🚫 Only affects error handling.

54c539ecc1 Refactor bindingProperty parsing (#13929)
🚫 Nothing stands out as relevant to Sucrase.

966387d263 Always expose `expressionValue` in `DirectiveLiteral` nodes (#13960)
🚫 AST only.

87fc2e76d7 Add `assertions` to `ExportNamedDeclaration` without `from` (#13957)
🚫 AST only.

99774ee9fa v7.16.4
🚫 Release only.

d30308fe8e fix: update UnexpectedPrivateField error message (#13975)
🚫 Validation only.

a470f7b479 Recover from shorthand assign exprs (#13968)
✅ We already handled this case, but I added a test to confirm.

75996cb62b Use full import specifier path in tests (#13938)
🚫 Babel-internal change.

ad1798ed48 Only bundle the release build, and don't import `src` in tests (#13978)
🚫 Babel-internal change.

f4236f43a1 Extend `hasPlugin` to accept plugin-configuration array pairs (#13982)
🚫 Sucrase uses a different config system.

2d989a983d Run tests in a native Node.js ESM environment (#13966)
🚫 Babel-internal change. (Though very cool to see!)

94af0e5c62 Improve template tokenizing (#13919)
🚫 Sucrase got rid of context, so it looks like this change isn't as relevant.

39080492f4 maintain estree string literal shape when cloned (#14039)
🚫 AST only.

7b7ab94066 v7.16.5
🚫 Release only.

7794201940 fix: handle tokens for invalid template element (#14055)
🚫 Bug not relevant to Sucrase.

5687ade5e7 v7.16.6
🚫 Release only.

ad17fe1cce fix: check preceding line break before exclamation (#14049)
✅ Fixed bug in the same way and added a test.

d1cabf6bc8 [babel 8] Add `"exports"` to every package (#14013)
🚫 Babel-internal change.

23e884048a v7.16.7
🚫 Release only.
alangpierce added a commit that referenced this pull request Jul 1, 2022
Instructions: https://github.com/alangpierce/sucrase/wiki/Porting-changes-from-Babel's-parser

976bfbbf69 fix: assure left bracket is not consumed after dot (#13695)
🚫 Bug doesn't seem to apply to Sucrase, and fix is validation only.

1d4bd31950 add missing ExportNamespaceSpecifier in spec.md (#13691)
🚫 Docs only.

b141c85b17 fix(babel-parser): delete `static` property from class static block for TS (#13680)
🚫 Only affects AST.

64d116bd6a fix(parser): [Babel8] Align error codes between Flow and TypeScript (#13294)
🚫 Only affects error reporting.

a54f041440 test(parser): add no_plugin tests for module blocks (#13714)
🚫 Test only.

b2376757d7 v7.15.4
🚫 Release only.

44388e6e81 Disallow `#a in #b in c` and similar expressions (#13727)
🚫 Only affects validation.

e82e43ca33 v7.15.5
🚫 Release only.

62e42a3f60 fix(babel-parser): Fix end of `range` of `SequenceExpression` (#13731)
🚫 AST only.

c25ec3e069 [babel-parser] add tests for static blocks with line breaks (#13734)
✅ Added similar test for Sucrase (though nothing was broken).

3c3f5205c9 Fix right precedence of Hack pipes (#13668)
✅ Given the active spec work, I filed #674 to revisit in the future.

34a27e46f8 v7.15.6
🚫 Release only.

710b3919fa [estree] Fix conversion of `PrivateName` in `MemberExpression` (#13755)
🚫 Only affects estree.

da79c90f2a fix(babel-parser): Allow line break before `assert` return type (#13771)
✅ Ported fix.

d2076a531f Store token type as number (#13768)
🚫 Optimization that's pretty much already in Sucrase.

8d0294edf8 v7.15.7
🚫 Release only.

2e2d202694 [babel 8] Materialize ESTree's `classFeatures` option (#13752)
🚫 Not relevant for Sucrase.

178d43ff17 Tokenize keywords-like identifier as new tokens (#13769)
🚫 Interesting alternative approach to Sucrase's contextual keyword enums. Doesn't seem worth it to switch to Babel's approach for now.

64f14b05fa Collect comments around parentheses in expressions (#13803)
🚫 AST only.

c4b13725aa v7.15.8
🚫 Release only.

b5907ef967 remove executable permission from files that shouldn't have it (#13873)
🚫 Babel-internal change.

62b2c5ebb9 Regenerate parser fixtures (#13882)
🚫 Babel-internal change.

c7ddb1ae93 Use `workspace:^` to specify `@babel/` dependencies (#13772)
🚫 Babel-internal change.

fb7ddf4d38 [ts] Support private methods overloads (#13876)
🚫 Issue doesn't affect Sucrase.

ddc45a5a50 Enable class static blocks by default (#13713)
🚫 Already enabled by default for Sucrase.

ad59a2c618 Caret topic (pipe operator) (#13749)
🚫 Not implementing topic token changes for now until it's finalized, see #674

718c6cb7de Handle `.mts` and `.cts` files in `@babel/preset-typescript` (#13838)
🚫 Only adds error handling to the parser. Sucrase integrations should better handle .mts and .cts, but that can be done in follow-up work.

872086a9a0 feat: support `startColumn` option (#13887)
🚫 Not relevant to Sucrase.

d5ba355867 Support TypeScript 4.5 type-only import/export specifiers (#13802)
✅ Implemented separately as #713

45308f3cba v7.16.0
🚫 Release only.

cba7f9e503 Add missing assertions type (#13905)
🚫 AST only.

68fad796b1 v7.16.2
🚫 Release only.

1fa759f989 refactor: extract tt.lt and tt.gt from tt.relation (#13892)
🚫 Already done in Sucrase. Also includes a refactor that could be reasonable to incorporate, but I'll skip that for now.

7250d2562b Simplifiy tracking of valid JSX positions (#13891)
🚫 The relevant code has changed enough that this probably isn't worth porting.

a6a526968d fix: incorrect await rejection following arrow function in parameters (#13928)
🚫 Bug fix is in code not relevant to Sucrase.

3b6f061e18 v7.16.3
🚫 Release only.

135ab837bc Throw on duplicate `__proto__` props followed by assignment (#13951)
🚫 Only affects error handling.

54c539ecc1 Refactor bindingProperty parsing (#13929)
🚫 Nothing stands out as relevant to Sucrase.

966387d263 Always expose `expressionValue` in `DirectiveLiteral` nodes (#13960)
🚫 AST only.

87fc2e76d7 Add `assertions` to `ExportNamedDeclaration` without `from` (#13957)
🚫 AST only.

99774ee9fa v7.16.4
🚫 Release only.

d30308fe8e fix: update UnexpectedPrivateField error message (#13975)
🚫 Validation only.

a470f7b479 Recover from shorthand assign exprs (#13968)
✅ We already handled this case, but I added a test to confirm.

75996cb62b Use full import specifier path in tests (#13938)
🚫 Babel-internal change.

ad1798ed48 Only bundle the release build, and don't import `src` in tests (#13978)
🚫 Babel-internal change.

f4236f43a1 Extend `hasPlugin` to accept plugin-configuration array pairs (#13982)
🚫 Sucrase uses a different config system.

2d989a983d Run tests in a native Node.js ESM environment (#13966)
🚫 Babel-internal change. (Though very cool to see!)

94af0e5c62 Improve template tokenizing (#13919)
🚫 Sucrase got rid of context, so it looks like this change isn't as relevant.

39080492f4 maintain estree string literal shape when cloned (#14039)
🚫 AST only.

7b7ab94066 v7.16.5
🚫 Release only.

7794201940 fix: handle tokens for invalid template element (#14055)
🚫 Bug not relevant to Sucrase.

5687ade5e7 v7.16.6
🚫 Release only.

ad17fe1cce fix: check preceding line break before exclamation (#14049)
✅ Fixed bug in the same way and added a test.

d1cabf6bc8 [babel 8] Add `"exports"` to every package (#14013)
🚫 Babel-internal change.

23e884048a v7.16.7
🚫 Release only.
1Lighty pushed a commit to Astra-mod/sucrase that referenced this pull request Aug 14, 2022
Fixes alangpierce#668

Details on the syntax are described here:
https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names

This was implemented in Babel here: babel/babel#13802

In terms of behavior, the type syntax is *almost* a no-op in valid code; we
already scan the file to see which identifiers have been used in a value or type
context and remove type imports automatically, and this PR doesn't change that.
However, there are a few subtle differences:
* When targeting commonjs, if an imported name has a `type` modifier and is used
  in a value context, we do *not* transform the syntax to access the module like
  we did before. For example, this comes up if accessing a global value with the
  same name as an imported type.
* In both CJS and ESM targets, code that imports and then re-exports a type
  needs to have that export elided. Previously, this was a situation where TS
  doesn't know whether the name is a type or not, so it included the export as a
  value.

Right now, there is no equivalent to `--preserveValueImports`, but this could be
added in the future. It *almost* would turn the TS transform into a purely
syntax-level transform without the need for scope analysis and variable name
tracking (thus simplifying and speeding up Sucrase if it became the standard
approach), except that even with that flag set, type-only exports are still
elided.

In terms of implementation details, the Babel parser details were a bit
AST-focused and didn't need to deal with `IdentifierRole`s, so I re-implemented
parsing using a simpler approach, which was also useful in the transform step.

It turns out that in both Flow and TS, you can parse an import or export
specifier by accepting anywhere from 1 to 4 identifiers, and the number of
identifiers is enough to exactly determine the syntax. This is more reliable
than matching on the names `type` (or `typeof`) and `as` because those are
contextual keywords that might be valid identifiers.

In the transform step, there were 5 places where we were manually walking
import/export specifiers, so it seemed best to unify that parsing into a shared
utility. This potentially has some performance cost due to the object
allocations, but it's likely to be very minor and could potentially be improved
later.
1Lighty pushed a commit to Astra-mod/sucrase that referenced this pull request Aug 14, 2022
)

Instructions: https://github.com/alangpierce/sucrase/wiki/Porting-changes-from-Babel's-parser

976bfbbf69 fix: assure left bracket is not consumed after dot (#13695)
🚫 Bug doesn't seem to apply to Sucrase, and fix is validation only.

1d4bd31950 add missing ExportNamespaceSpecifier in spec.md (#13691)
🚫 Docs only.

b141c85b17 fix(babel-parser): delete `static` property from class static block for TS (#13680)
🚫 Only affects AST.

64d116bd6a fix(parser): [Babel8] Align error codes between Flow and TypeScript (#13294)
🚫 Only affects error reporting.

a54f041440 test(parser): add no_plugin tests for module blocks (#13714)
🚫 Test only.

b2376757d7 v7.15.4
🚫 Release only.

44388e6e81 Disallow `#a in #b in c` and similar expressions (#13727)
🚫 Only affects validation.

e82e43ca33 v7.15.5
🚫 Release only.

62e42a3f60 fix(babel-parser): Fix end of `range` of `SequenceExpression` (#13731)
🚫 AST only.

c25ec3e069 [babel-parser] add tests for static blocks with line breaks (#13734)
✅ Added similar test for Sucrase (though nothing was broken).

3c3f5205c9 Fix right precedence of Hack pipes (#13668)
✅ Given the active spec work, I filed alangpierce#674 to revisit in the future.

34a27e46f8 v7.15.6
🚫 Release only.

710b3919fa [estree] Fix conversion of `PrivateName` in `MemberExpression` (#13755)
🚫 Only affects estree.

da79c90f2a fix(babel-parser): Allow line break before `assert` return type (#13771)
✅ Ported fix.

d2076a531f Store token type as number (#13768)
🚫 Optimization that's pretty much already in Sucrase.

8d0294edf8 v7.15.7
🚫 Release only.

2e2d202694 [babel 8] Materialize ESTree's `classFeatures` option (#13752)
🚫 Not relevant for Sucrase.

178d43ff17 Tokenize keywords-like identifier as new tokens (#13769)
🚫 Interesting alternative approach to Sucrase's contextual keyword enums. Doesn't seem worth it to switch to Babel's approach for now.

64f14b05fa Collect comments around parentheses in expressions (#13803)
🚫 AST only.

c4b13725aa v7.15.8
🚫 Release only.

b5907ef967 remove executable permission from files that shouldn't have it (#13873)
🚫 Babel-internal change.

62b2c5ebb9 Regenerate parser fixtures (#13882)
🚫 Babel-internal change.

c7ddb1ae93 Use `workspace:^` to specify `@babel/` dependencies (#13772)
🚫 Babel-internal change.

fb7ddf4d38 [ts] Support private methods overloads (#13876)
🚫 Issue doesn't affect Sucrase.

ddc45a5a50 Enable class static blocks by default (#13713)
🚫 Already enabled by default for Sucrase.

ad59a2c618 Caret topic (pipe operator) (#13749)
🚫 Not implementing topic token changes for now until it's finalized, see alangpierce#674

718c6cb7de Handle `.mts` and `.cts` files in `@babel/preset-typescript` (#13838)
🚫 Only adds error handling to the parser. Sucrase integrations should better handle .mts and .cts, but that can be done in follow-up work.

872086a9a0 feat: support `startColumn` option (#13887)
🚫 Not relevant to Sucrase.

d5ba355867 Support TypeScript 4.5 type-only import/export specifiers (#13802)
✅ Implemented separately as alangpierce#713

45308f3cba v7.16.0
🚫 Release only.

cba7f9e503 Add missing assertions type (#13905)
🚫 AST only.

68fad796b1 v7.16.2
🚫 Release only.

1fa759f989 refactor: extract tt.lt and tt.gt from tt.relation (#13892)
🚫 Already done in Sucrase. Also includes a refactor that could be reasonable to incorporate, but I'll skip that for now.

7250d2562b Simplifiy tracking of valid JSX positions (#13891)
🚫 The relevant code has changed enough that this probably isn't worth porting.

a6a526968d fix: incorrect await rejection following arrow function in parameters (#13928)
🚫 Bug fix is in code not relevant to Sucrase.

3b6f061e18 v7.16.3
🚫 Release only.

135ab837bc Throw on duplicate `__proto__` props followed by assignment (#13951)
🚫 Only affects error handling.

54c539ecc1 Refactor bindingProperty parsing (#13929)
🚫 Nothing stands out as relevant to Sucrase.

966387d263 Always expose `expressionValue` in `DirectiveLiteral` nodes (#13960)
🚫 AST only.

87fc2e76d7 Add `assertions` to `ExportNamedDeclaration` without `from` (#13957)
🚫 AST only.

99774ee9fa v7.16.4
🚫 Release only.

d30308fe8e fix: update UnexpectedPrivateField error message (#13975)
🚫 Validation only.

a470f7b479 Recover from shorthand assign exprs (#13968)
✅ We already handled this case, but I added a test to confirm.

75996cb62b Use full import specifier path in tests (#13938)
🚫 Babel-internal change.

ad1798ed48 Only bundle the release build, and don't import `src` in tests (#13978)
🚫 Babel-internal change.

f4236f43a1 Extend `hasPlugin` to accept plugin-configuration array pairs (#13982)
🚫 Sucrase uses a different config system.

2d989a983d Run tests in a native Node.js ESM environment (#13966)
🚫 Babel-internal change. (Though very cool to see!)

94af0e5c62 Improve template tokenizing (#13919)
🚫 Sucrase got rid of context, so it looks like this change isn't as relevant.

39080492f4 maintain estree string literal shape when cloned (#14039)
🚫 AST only.

7b7ab94066 v7.16.5
🚫 Release only.

7794201940 fix: handle tokens for invalid template element (#14055)
🚫 Bug not relevant to Sucrase.

5687ade5e7 v7.16.6
🚫 Release only.

ad17fe1cce fix: check preceding line break before exclamation (#14049)
✅ Fixed bug in the same way and added a test.

d1cabf6bc8 [babel 8] Add `"exports"` to every package (#14013)
🚫 Babel-internal change.

23e884048a v7.16.7
🚫 Release only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support type modifers on import names in Typescript
1 participant