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

Implement Flow enums #709

Merged
merged 2 commits into from
Jun 27, 2022
Merged

Implement Flow enums #709

merged 2 commits into from
Jun 27, 2022

Conversation

alangpierce
Copy link
Owner

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.

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
Copy link

codecov bot commented Jun 25, 2022

Codecov Report

Merging #709 (68cbf4b) into main (00e7ed6) will increase coverage by 0.14%.
The diff coverage is 100.00%.

@@            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              
Impacted Files Coverage Δ
src/TokenProcessor.ts 93.80% <100.00%> (+0.05%) ⬆️
src/transformers/CJSImportTransformer.ts 88.35% <100.00%> (+0.05%) ⬆️
src/transformers/FlowTransformer.ts 100.00% <100.00%> (ø)
src/transformers/RootTransformer.ts 93.65% <100.00%> (ø)

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

@github-actions
Copy link

github-actions bot commented Jun 25, 2022

Benchmark results

Before this PR: 292.9 thousand lines per second
After this PR: 287.8 thousand lines per second

Measured change: 1.74% slower (2.62% slower to 0.18% slower)
Summary: Probably slower

@alangpierce alangpierce merged commit 2da78b9 into main Jun 27, 2022
@alangpierce alangpierce deleted the implement-flow-enums branch June 27, 2022 20:11
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Flow Enums
1 participant