[babel 8] Disallow sequence expressions in JSX expression containers (#12447)

* jsx: fix sequence expression at JSXAttributeValue (#8787)

* jsx: fix sequence expression at JSXAttributeValue

* Change logic for detecting unparenthesized expressions

* use parseMaybeAssign instead of custom error handling

Co-authored-by: Daniel Tschinder <daniel@tschinder.de>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>

* I'm not good at booleans

* Format

* Throw a recoverable error

Co-authored-by: Bruno Macabeus <macabeus@users.noreply.github.com>
Co-authored-by: Daniel Tschinder <daniel@tschinder.de>
This commit is contained in:
Nicolò Ribaudo
2020-12-08 10:15:03 +01:00
committed by GitHub
parent 2ba9265198
commit b422c7f0ef
18 changed files with 707 additions and 2 deletions

View File

@@ -24,6 +24,8 @@ const JsxErrors = Object.freeze({
"JSX attributes must only be assigned a non-empty expression",
MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>",
MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>",
UnexpectedSequenceExpression:
"Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
UnsupportedJsxValue:
"JSX value should be either an expression or a quoted JSX text",
UnterminatedJsxContent: "Unterminated JSX contents",
@@ -344,9 +346,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.match(tt.braceR)) {
node.expression = this.jsxParseEmptyExpression();
} else {
node.expression = this.parseExpression();
const expression = this.parseExpression();
if (process.env.BABEL_8_BREAKING) {
if (
expression.type === "SequenceExpression" &&
!expression.extra?.parenthesized
) {
this.raise(
expression.expressions[1].start,
JsxErrors.UnexpectedSequenceExpression,
);
}
}
node.expression = expression;
}
this.expect(tt.braceR);
return this.finishNode(node, "JSXExpressionContainer");
}