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

Spread children not supported #420

Closed
yang opened this issue Feb 20, 2019 · 6 comments · Fixed by #431
Closed

Spread children not supported #420

yang opened this issue Feb 20, 2019 · 6 comments · Fixed by #431

Comments

@yang
Copy link
Contributor

yang commented Feb 20, 2019

const F = () => <a>{...[]}</a>;

crashes sucrase with:

Error transforming sample.tsx: Unterminated regular expression (1:29)
@alangpierce
Copy link
Owner

Interesting, I actually hadn't seen that syntax. FWIW, Babel (with the React transform) throws an exception, saying "Spread children are not supported in React" (repl). Are you using something other than React? I think React wants you to pass an array there so it knows to check that you're using keys properly.

Regardless, I think it's within Sucrase's scope to just compile it and let other tools do the error checking, so this certainly seems reasonable to implement.

@yang
Copy link
Contributor Author

yang commented Feb 27, 2019 via email

@arv
Copy link
Contributor

arv commented Feb 28, 2019

https://facebook.github.io/jsx/ includes it:

JSXChildExpression :
  AssignmentExpression
  ... AssignmentExpression

@alangpierce
Copy link
Owner

Some history here: facebook/jsx#57 . Looks like the original argument for adding it was for non-React use cases. My impression is Babel parses and recognizes it but intentionally disallows it for React, since they're worried that beginners will use it for dynamic arrays when really they should be specifying keys. TypeScript didn't implement that check, it seems. But I think that restriction is something a linter should do (and a team should be allowed to opt in and out of), so I think it's fine to just allow and transform the syntax as specified in Sucrase.

@alangpierce
Copy link
Owner

alangpierce commented Mar 4, 2019

@yang to be clear, you expect

<div>{...a}{...b}</div>

to be transformed to

React.createElement('div', null, ...a, ...b)

right? That's certainly what I'd expect. Looks like TypeScript is transforming it to React.createElement('div', null, a, b), which seems like it ruins the point of spread children. Sucrase playground link

I'll implement it with ... in the output since that makes the most sense (and is easier anyway), but I guess it's a bit surprising in that it seems like in TypeScript, it's the same as omitting the ....

alangpierce added a commit that referenced this issue Mar 4, 2019
Fixes #420

This was actually already handled by the parser carried over from Babel, but
there was a bug in the tokenization, since Sucrase always skips the last token
so we can get the proper token type for the next one. After fixing that bug,
everything seems to work; no transformer changes are needed because argument
spread syntax is the same as JSX child spread syntax.

As mentioned in #420, Babel disallows this syntax with React and TypeScript
seems to give the wrong result, but this change like a good idea regardless
since it's just a bug fix.
alangpierce added a commit that referenced this issue Mar 4, 2019
Fixes #420

This was actually already handled by the parser carried over from Babel, but
there was a bug in the tokenization, since Sucrase always skips the last token
so we can get the proper token type for the next one. After fixing that bug,
everything seems to work; no transformer changes are needed because argument
spread syntax is the same as JSX child spread syntax.

As mentioned in #420, Babel disallows this syntax with React and TypeScript
seems to give the wrong result, but this change like a good idea regardless
since it's just a bug fix.
@alangpierce
Copy link
Owner

I just released a fix for this in 3.10.0.

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 a pull request may close this issue.

3 participants