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 parser changes for the JSX automatic runtime #739

Merged
merged 1 commit into from Aug 16, 2022

Conversation

alangpierce
Copy link
Owner

Progress toward #585

This change extends the parser to detect two JSX cases that are needed for the
automatic runtime. The new code is run unconditionally, even in the old
transform, since flagging it would have its own overhead and complexity.

The new JSX transform needs to distinguish children being static or non-static,
where static is equivalent to having at least two comma-separated children
emitted by the old transform or any child being a spread child. The main
challenge in getting this right is that JSX whitespace-trimming will sometimes
completely remove all content from a text range, in which case it shouldn't
count as a child for the purposes of counting the number of children.
Fortunately, there is a relatively simple algorithm to detect if a text range
is empty: it's empty if and only if it is entirely whitespace and has at least
one newline. To identify these "empty text" regions, I added a new token type
jsxEmptyText that is treated the same except for the purposes of counting
children. In the future, it likely is reasonable to not treat such a region as a
token in the first place.

The new JSX transform also needs to detect the pattern of a key appearing after
a prop spread. We don't do keyword detection on JSX prop names, so instead I
manually detect the name "key", but only if we have already seen a prop spread.

Progress toward #585

This change extends the parser to detect two JSX cases that are needed for the
automatic runtime. The new code is run unconditionally, even in the old
transform, since flagging it would have its own overhead and complexity.

The new JSX transform needs to distinguish children being static or non-static,
where static is equivalent to having at least two comma-separated children
emitted by the old transform or any child being a spread child. The main
challenge in getting this right is that JSX whitespace-trimming will sometimes
completely remove all content from a text range, in which case it shouldn't
count as a child for the purposes of counting the number of children.
Fortunately, there is a relatively simple algorithm to detect if a text range
is empty: it's empty if and only if it is entirely whitespace and has at least
one newline. To identify these "empty text" regions, I added a new token type
`jsxEmptyText` that is treated the same except for the purposes of counting
children. In the future, it likely is reasonable to not treat such a region as a
token in the first place.

The new JSX transform also needs to detect the pattern of a key appearing after
a prop spread. We don't do keyword detection on JSX prop names, so instead I
manually detect the name "key", but only if we have already seen a prop spread.
@codecov
Copy link

codecov bot commented Aug 16, 2022

Codecov Report

Merging #739 (4d46bda) into main (36b82ba) will increase coverage by 0.06%.
The diff coverage is 96.49%.

@@            Coverage Diff             @@
##             main     #739      +/-   ##
==========================================
+ Coverage   87.17%   87.23%   +0.06%     
==========================================
  Files          54       54              
  Lines        5722     5751      +29     
  Branches     1349     1357       +8     
==========================================
+ Hits         4988     5017      +29     
  Misses        466      466              
  Partials      268      268              
Impacted Files Coverage Δ
src/parser/traverser/expression.ts 88.41% <0.00%> (ø)
src/transformers/JSXTransformer.ts 97.16% <0.00%> (ø)
src/parser/plugins/jsx/index.ts 92.26% <100.00%> (+1.16%) ⬆️
src/parser/tokenizer/index.ts 86.28% <100.00%> (+0.17%) ⬆️
src/parser/util/charcodes.ts 98.16% <100.00%> (+0.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link

Benchmark results

Before this PR: 397.7 thousand lines per second
After this PR: 399.5 thousand lines per second

Measured change: 0.46% faster (0.77% slower to 0.7% faster)
Summary: Likely no significant difference

@alangpierce alangpierce merged commit e9a1281 into main Aug 16, 2022
@alangpierce alangpierce deleted the jsx-automatic-runtime-parser-changes branch August 16, 2022 21:02
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

1 participant