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

First pass at implementation of nullish coalescing #488

Merged
merged 1 commit into from Dec 27, 2019

Conversation

alangpierce
Copy link
Owner

Progress toward #461

Tech plan at https://github.com/alangpierce/sucrase/wiki/Sucrase-Optional-Chaining-and-Nullish-Coalescing-Technical-Plan

Some details:

  • In the parser, operator parsing now keeps track of the start token index so
    that when we observe a ?? operator, we can mark the first token as being the
    start of an optional chain.
  • The token format now has fields for the number of optional chain starts and
    ends for each token, which the transformer will use to inject code.
  • Optional chaining code needs to use the HelperManager system, so I refactored
    it to be on the RootTransformer instead of just on one of them.
  • The actual start/end code injection is done by the token system so that it
    works alongside all existing transforms.
  • Since TokenProcessor now needs a HelperManager, I had to break the dependency
    cycle by making NameManager no longer depend on TokenProcessor, since it was
    only barely using it anyway.

Some quick perf benchmarks show this as having potentially a 4% slowdown even
for code not using nullish coalescing, though I wasn't able to track down any
specific area of code responsible, and it may have been partially due to
variability or other factors. Except for more egregious cases, I'll leave
performance work as a follow-up.

Progress toward #461

Tech plan at https://github.com/alangpierce/sucrase/wiki/Sucrase-Optional-Chaining-and-Nullish-Coalescing-Technical-Plan

Some details:
* In the parser, operator parsing now keeps track of the start token index so
  that when we observe a `??` operator, we can mark the first token as being the
  start of an optional chain.
* The token format now has fields for the number of optional chain starts and
  ends for each token, which the transformer will use to inject code.
* Optional chaining code needs to use the HelperManager system, so I refactored
  it to be on the RootTransformer instead of just on one of them.
* The actual start/end code injection is done by the token system so that it
  works alongside all existing transforms.
* Since TokenProcessor now needs a HelperManager, I had to break the dependency
  cycle by making NameManager no longer depend on TokenProcessor, since it was
  only barely using it anyway.

Some quick perf benchmarks show this as having potentially a 4% slowdown even
for code not using nullish coalescing, though I wasn't able to track down any
specific area of code responsible, and it may have been partially due to
variability or other factors. Except for more egregious cases, I'll leave
performance work as a follow-up.
@codecov-io
Copy link

Codecov Report

Merging #488 into master will increase coverage by 0.2%.
The diff coverage is 98.27%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #488     +/-   ##
=========================================
+ Coverage   82.08%   82.29%   +0.2%     
=========================================
  Files          52       53      +1     
  Lines        5202     5466    +264     
  Branches     1210     1214      +4     
=========================================
+ Hits         4270     4498    +228     
- Misses        647      681     +34     
- Partials      285      287      +2
Impacted Files Coverage Δ
src/HelperManager.ts 100% <ø> (ø) ⬆️
src/index.ts 90% <100%> (+0.2%) ⬆️
src/transformers/CJSImportTransformer.ts 87.98% <100%> (ø) ⬆️
src/CJSImportProcessor.ts 92.15% <100%> (-0.08%) ⬇️
src/NameManager.ts 100% <100%> (ø) ⬆️
src/parser/tokenizer/index.ts 77.15% <100%> (+0.57%) ⬆️
src/TokenProcessor.ts 91.2% <100%> (+2.01%) ⬆️
src/util/getIdentifierNames.ts 100% <100%> (ø)
src/transformers/RootTransformer.ts 92.42% <100%> (+0.19%) ⬆️
src/parser/traverser/expression.ts 85.86% <88.88%> (+0.42%) ⬆️
... and 29 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 2498bcc...3e846d9. Read the comment docs.

@alangpierce alangpierce merged commit d290daa into master Dec 27, 2019
@alangpierce alangpierce deleted the implement-nullish-coalescing branch December 27, 2019 22:03
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.

None yet

2 participants