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

Added transform for logical assignment operators #557

Closed
wants to merge 1 commit into from

Conversation

Rugvip
Copy link
Contributor

@Rugvip Rugvip commented Oct 7, 2020

Implements transforms of &&=, ||=, and ??=, as discussed in #550.

This is an early draft to see what you think about the direction. It does end up adding more fields to the tokens, but it's also real tricky to get around that. I did end up with some thoughts about how to refactor into a more generic helper prefixing field, but that'd be a separate thing.

Only tested with transform tests so far, haven't actually executed any transformed code, so needs some more testing there x)

@codecov-io
Copy link

codecov-io commented Oct 7, 2020

Codecov Report

Merging #557 into master will increase coverage by 0.12%.
The diff coverage is 89.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #557      +/-   ##
==========================================
+ Coverage   81.47%   81.59%   +0.12%     
==========================================
  Files          55       56       +1     
  Lines        5538     5624      +86     
  Branches     1308     1325      +17     
==========================================
+ Hits         4512     4589      +77     
- Misses        735      739       +4     
- Partials      291      296       +5     
Impacted Files Coverage Δ
src/HelperManager.ts 100.00% <ø> (ø)
src/transformers/LogicalAssignmentTransformer.ts 87.32% <87.32%> (ø)
src/TokenProcessor.ts 93.80% <100.00%> (+1.07%) ⬆️
src/parser/tokenizer/index.ts 78.85% <100.00%> (+0.04%) ⬆️
src/parser/traverser/expression.ts 88.56% <100.00%> (+0.46%) ⬆️
src/transformers/RootTransformer.ts 92.46% <100.00%> (+0.07%) ⬆️
src/transformers/CJSImportTransformer.ts 87.81% <0.00%> (-0.51%) ⬇️

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 080fce6...c2b8dae. Read the comment docs.


export default class LogicalAssignmentTransformer extends Transformer {
// This stack stores the state needed to transform computed property access
private readonly computedAccessStack: Array<ComputedAccess> = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to a manual stack instead of relying on the JS execution stack. This seemed to avoid some of the competition going on with other processors, but could switch back if you think the old approach is better.

@Rugvip Rugvip marked this pull request as ready for review October 10, 2020 13:59
Base automatically changed from master to main January 31, 2021 22:58
@Qix-
Copy link

Qix- commented Jun 20, 2021

@alangpierce Sorry for the ping, but any chance of getting this published? :)


Upon testing, it works (yay!) but outputs some questionable stuff.

_logicalAssign(type, 'properties', '??=', () =>  {});

Is this really necessary?

foo ??= bar;
// can be transformed as
if (foo == null) foo = bar;

I don't see why it requires a function call, string comparison, etc. just to do a simple assignment.

@Rugvip
Copy link
Contributor Author

Rugvip commented Jun 22, 2021

@Qix- #550 (comment) has more context on why the transform is the way it is. Also note that the helper transform is avoided when the left side is a plain variable.

Could be discussed if it's worth merging this btw, as we're approaching support for the syntax in all major runtimes.

@Rugvip
Copy link
Contributor Author

Rugvip commented Aug 19, 2021

Gonna close this since Node.js v16 is the first LTS release with support for this syntax and it is soon the active LTS. At this point it doesn't make sense to add the complexity of this transform to sucrase, with it being better to rather look at which transforms we can remove.

@Rugvip Rugvip closed this Aug 19, 2021
@Qix-
Copy link

Qix- commented Aug 21, 2021

I don't use Sucrase for Node. I use it for the browser. There are recent browsers that still don't support this syntax.

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

4 participants