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

Disallow < after a JSX element #141

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,41 @@ render(dropdown);

<h2>Syntax</h2>
<emu-grammar>
PrimaryExpression :
<ins>JSXElement</ins>
<ins>JSXFragment</ins>
PrimaryExpression ::
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
<ins>JSXElementOrFragment `<`</ins>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this production to be able to define an early error on PrimaryExpression :: JSXElementOrFragment <?

Copy link
Author

Choose a reason for hiding this comment

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

We could do something like

PrimaryExpression :: JSXElementOrFragment

- It is a Syntax Error is this production is followed by the `<` token

but I used that new production to avoid introducing new language/checks that are not already used in the ecma262 spec

Copy link
Contributor

@Huxpro Huxpro Feb 28, 2022

Choose a reason for hiding this comment

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

I understand that without PrimaryExpression :: JSXElementOrFragment < we don't have a production to match with. But it's also not ideal to me to only add this production specifically for the early error 🤔

Copy link
Author

Choose a reason for hiding this comment

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

Note that the ecma262 spec already does this in a few cases, for example to disallow a?.b`c`: https://tc39.es/ecma262/#sec-left-hand-side-expressions-static-semantics-early-errors

<ins>JSXElementOrFragment</ins>
</emu-grammar>

<emu-clause type="sdo" id="sec-jsx-PrimaryExpression-early-errors">
<h1>Static Semantics: Early Errors</h1>
<emu-grammar>
PrimaryExpression :: JSXElementOrFragment `<`
</emu-grammar>
<ul>
<li>It is a SyntaxError if any source text is matched by this production.</li>
</ul>
<emu-note>
This Early Error prevents users from accidentally writing two adjacent JSX elements without wrapping them in a fragment:
<pre><code class="language-jsx">
// Invalid
var elts = &lt;a&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;

// Valid
var elts = &lt;&gt;&lt;a&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;&lt;/&gt;
</code></pre>
</emu-note>
</emu-clause>
</emu-clause>

<emu-clause id="sec-jsx-elements">
<h1>JSX Elements</h1>
<h2>Syntax</h2>

<emu-grammar type="definition">
JSXElementOrFragment ::
JSXElement
JSXFragment

JSXElement :
JSXSelfClosingElement
JSXOpeningElement JSXChildren? JSXClosingElement
Expand Down