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

Properly transform enum values, rewrite enum implementation #621

Merged
merged 1 commit into from
Jun 7, 2021

Conversation

alangpierce
Copy link
Owner

Fixes #620

The previous enum implementation was a bit tangled, handling several different
cases in one big while loop. This PR refactors the code into three distinct
cases: string enum members, calculated/constant enum members, and
autoincrementing enum members, and the transform details are handled separately
for each. Each case still needs to think about the case where we save a variable
and the case where we don't, but hopefully the whole thing is more
understandable now, and certainly better-documented.

This refactor makes it much easier to fix #620, where we weren't actually
passing the RHS expressions to the root transformer for their own transforms. In
particular, that meant that imported values weren't transformed correctly. To
fix, we now call out to the root transformer and apply the enum transform in a
purely left-to-right way rather than needing to save and re-emit a region of code.

As part of the refactor, I also changed the emitted code so that when
autoincrementing a previous value, we always reference from the enum it rather
than copying the entire expression. This should reduce cases where we need to
copy a large range of code, which is generally questionable in Sucrase.

Fixes #620

The previous enum implementation was a bit tangled, handling several different
cases in one big `while` loop. This PR refactors the code into three distinct
cases: string enum members, calculated/constant enum members, and
autoincrementing enum members, and the transform details are handled separately
for each. Each case still needs to think about the case where we save a variable
and the case where we don't, but hopefully the whole thing is more
understandable now, and certainly better-documented.

This refactor makes it much easier to fix #620, where we weren't actually
passing the RHS expressions to the root transformer for their own transforms. In
particular, that meant that imported values weren't transformed correctly. To
fix, we now apply the enum transform in a purely left-to-right way rather than
needing to save and re-emit a region of code.

As part of the refactor, I also changed the emitted code so that when
autoincrementing a previous value, we always reference it rather than copying
the entire expression. This should reduce cases where we need to copy a large
range of code, which is generally questionable in Sucrase.
@codecov
Copy link

codecov bot commented Jun 7, 2021

Codecov Report

Merging #621 (359800f) into main (2b50eef) will increase coverage by 0.11%.
The diff coverage is 91.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #621      +/-   ##
==========================================
+ Coverage   82.05%   82.16%   +0.11%     
==========================================
  Files          56       56              
  Lines        5622     5870     +248     
  Branches     1321     1324       +3     
==========================================
+ Hits         4613     4823     +210     
- Misses        725      763      +38     
  Partials      284      284              
Impacted Files Coverage Δ
src/transformers/TypeScriptTransformer.ts 95.55% <91.83%> (+0.61%) ⬆️
src/util/formatTokens.ts 70.58% <0.00%> (-10.06%) ⬇️
src/parser/util/identifier.ts 90.90% <0.00%> (-4.10%) ⬇️
src/util/shouldElideDefaultExport.ts 77.77% <0.00%> (-3.48%) ⬇️
src/util/getClassInfo.ts 87.21% <0.00%> (-1.93%) ⬇️
src/parser/plugins/jsx/index.ts 91.33% <0.00%> (-1.48%) ⬇️
src/parser/plugins/flow.ts 63.72% <0.00%> (-1.28%) ⬇️
src/identifyShadowedGlobals.ts 87.80% <0.00%> (-1.09%) ⬇️
src/parser/traverser/expression.ts 88.10% <0.00%> (-0.11%) ⬇️
src/Options.ts 100.00% <0.00%> (ø)
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2b50eef...359800f. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Jun 7, 2021

Benchmark results

Before this PR: 276.8 thousand lines per second
After this PR: 274.6 thousand lines per second

Measured change: 0.81% slower (2.51% slower to 0.44% faster)
Summary: Likely no significant difference

@alangpierce alangpierce merged commit aee2a87 into main Jun 7, 2021
@alangpierce alangpierce deleted the rewrite-enums-fix-evaluation branch June 7, 2021 15:38
@jeresig jeresig mentioned this pull request Jun 9, 2022
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.

Invalid import of a compiled enum from JS file into TS file
1 participant