-
Notifications
You must be signed in to change notification settings - Fork 142
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
Implement Flow enums #709
Merged
Merged
Implement Flow enums #709
Conversation
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
Fixes #701 There were a few cases to handle around different types of enums and whethery they are exported or default export and targeting ESM vs CJS, but overall the code transform behavior is pretty straightforward. One point of complexity was that the imports transform needed to opt out of handling for the default export case so that the enum transform can handle it. Some caveats in the implementation: * The snippet `require("flow-enums-runtime")` is not currently configurable like it is for the babel plugin. It wouldn't be hard to add this as a new option, but I think it's best to leave out config options unless they're needed. * Flow enums have quite a few constraints which are validated by the babel parser and which Sucrase does not validate, such as initializers being all-or-nothing, initializers always being literals, and initializers agreeing with the enum type. Currently, Sucrase doesn't give a very helpful error when these assumptions aren't true, e.g. the transform step can overrun the enum body and crash unexpectedly later. This hopefully isn't such a big deal since Flow will give a helpful error, but there are probably ways to make the Sucrase errors more helpful without sacrificing performance.
Codecov Report
@@ Coverage Diff @@
## main #709 +/- ##
==========================================
+ Coverage 84.83% 84.97% +0.14%
==========================================
Files 54 54
Lines 5801 5858 +57
Branches 1323 1338 +15
==========================================
+ Hits 4921 4978 +57
Misses 597 597
Partials 283 283
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
Benchmark resultsBefore this PR: 292.9 thousand lines per second Measured change: 1.74% slower (2.62% slower to 0.18% slower) |
1Lighty
pushed a commit
to Astra-mod/sucrase
that referenced
this pull request
Aug 14, 2022
Fixes alangpierce#701 There were a few cases to handle around different types of enums and whethery they are exported or default export and targeting ESM vs CJS, but overall the code transform behavior is pretty straightforward. One point of complexity was that the imports transform needed to opt out of handling for the default export case so that the enum transform can handle it. Some caveats in the implementation: * The snippet `require("flow-enums-runtime")` is not currently configurable like it is for the babel plugin. It wouldn't be hard to add this as a new option, but I think it's best to leave out config options unless they're needed. * Flow enums have quite a few constraints which are validated by the babel parser and which Sucrase does not validate, such as initializers being all-or-nothing, initializers always being literals, and initializers agreeing with the enum type. Currently, Sucrase doesn't give a very helpful error when these assumptions aren't true, e.g. the transform step can overrun the enum body and crash unexpectedly later. This hopefully isn't such a big deal since Flow will give a helpful error, but there are probably ways to make the Sucrase errors more helpful without sacrificing performance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #701
There were a few cases to handle around different types of enums and whethery
they are exported or default export and targeting ESM vs CJS, but overall the
code transform behavior is pretty straightforward. One point of complexity was
that the imports transform needed to opt out of handling for the default export
case so that the enum transform can handle it.
Some caveats in the implementation:
require("flow-enums-runtime")
is not currently configurable likeit is for the babel plugin. It wouldn't be hard to add this as a new option,
but I think it's best to leave out config options unless they're needed.
parser and which Sucrase does not validate, such as initializers being
all-or-nothing, initializers always being literals, and initializers agreeing
with the enum type. Currently, Sucrase doesn't give a very helpful error when
these assumptions aren't true, e.g. the transform step can overrun the enum
body and crash unexpectedly later. This hopefully isn't such a big deal since
Flow will give a helpful error, but there are probably ways to make the
Sucrase errors more helpful without sacrificing performance.