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

Ensure that template literals generated by JSX-to-HTM transform has a balanced amount of quasis and expressions #199

Merged
merged 4 commits into from Feb 19, 2021

Conversation

@jviide
Copy link
Collaborator

@jviide jviide commented Feb 19, 2021

This pull request fixes the babel-plugin-transform-jsx-to-htm plugin output for JSX inputs like this:

<>
  <Foo />
  {Bar}
</>

Before the fix the plugin crashes with the following error message:

Number of TemplateLiteral quasis should be exactly one more than the number of expressions.
Expected 3 quasis but got 2

The fix is to always ensure that a template literal expression ends with a quasi, even when the quasi is an empty string. New static string content is then always applied to the (currently) last quasi in the template. When an expression is added a new empty quasi is immediately appended after that.

Kudos to @cristianbote for noticing a problem on wmr output that was caused by this issue. It appears that this fix also fixes the wmr problem.

This pull request also adds a test illustrating the problem.

jviide added 2 commits Feb 19, 2021
JSX expressions such as <><Foo />{Bar}</> get parsed into unbalanced TemplateLiteral expressions where there are less quasis than there should be when compared to the number of expressions.
Always keep a quasi - even one with no text - after an expression in generated template literals.
@jviide jviide requested a review from developit Feb 19, 2021
@jviide jviide force-pushed the fix-fragments branch from 7aa151e to e98149c Feb 19, 2021
@jviide jviide force-pushed the fix-fragments branch from e98149c to 283de56 Feb 19, 2021
Copy link
Owner

@developit developit left a comment

yay!

@jviide jviide merged commit 74fd8a7 into master Feb 19, 2021
6 checks passed
6 checks passed
build (10.x) build (10.x)
Details
build (10.x) build (10.x)
Details
build (12.x) build (12.x)
Details
build (12.x) build (12.x)
Details
build (14.x) build (14.x)
Details
build (14.x) build (14.x)
Details
@jviide jviide deleted the fix-fragments branch Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants