Report a SyntaxError for } and > in JSX text (#12451)

cherry-pick  #11046

Co-Authored-By: Kai Cataldo <kai@kaicataldo.com>
Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
This commit is contained in:
Nicolò Ribaudo 2020-12-28 19:04:54 +01:00 committed by GitHub
parent 0dbc3dd4a0
commit fbef603c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 357 additions and 106 deletions

View File

@ -125,6 +125,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
chunkStart = this.state.pos;
break;
case charCodes.greaterThan:
case charCodes.rightCurlyBrace:
if (process.env.BABEL_8_BREAKING) {
const htmlEntity =
ch === charCodes.rightCurlyBrace ? "&rbrace;" : "&gt;";
const char = this.input[this.state.pos];
this.raise(
this.state.pos,
`Unexpected token \`${char}\`. Did you mean \`${htmlEntity}\` or \`{'${char}'}\`?`,
);
}
/* falls through */
default:
if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.state.pos);

View File

@ -1 +0,0 @@
<LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />

View File

@ -1,105 +0,0 @@
{
"type": "File",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":57}},
"program": {
"type": "Program",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":57}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":57}},
"expression": {
"type": "JSXElement",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":57}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":57}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":10,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":10}},
"name": "LeftRight"
},
"attributes": [
{
"type": "JSXAttribute",
"start":11,"end":21,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":21}},
"name": {
"type": "JSXIdentifier",
"start":11,"end":15,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":15}},
"name": "left"
},
"value": {
"type": "JSXElement",
"start":16,"end":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}},
"openingElement": {
"type": "JSXOpeningElement",
"start":16,"end":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}},
"name": {
"type": "JSXIdentifier",
"start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18}},
"name": "a"
},
"attributes": [],
"selfClosing": true
},
"closingElement": null,
"children": []
}
},
{
"type": "JSXAttribute",
"start":22,"end":54,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":54}},
"name": {
"type": "JSXIdentifier",
"start":22,"end":27,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":27}},
"name": "right"
},
"value": {
"type": "JSXElement",
"start":28,"end":54,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":54}},
"openingElement": {
"type": "JSXOpeningElement",
"start":28,"end":31,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":31}},
"name": {
"type": "JSXIdentifier",
"start":29,"end":30,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":30}},
"name": "b"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":50,"end":54,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":54}},
"name": {
"type": "JSXIdentifier",
"start":52,"end":53,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":53}},
"name": "b"
}
},
"children": [
{
"type": "JSXText",
"start":31,"end":50,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":50}},
"extra": {
"rawValue": "monkeys /> gorillas",
"raw": "monkeys /> gorillas"
},
"value": "monkeys /> gorillas"
}
]
}
}
],
"selfClosing": true
},
"closingElement": null,
"children": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>></div>

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}

View File

@ -0,0 +1,52 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "JSXElement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}},
"name": {
"type": "JSXIdentifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"name": "div"
}
},
"children": [
{
"type": "JSXText",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6}},
"extra": {
"rawValue": ">",
"raw": ">"
},
"value": ">"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>{'>'}</div>

View File

@ -0,0 +1,56 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"expression": {
"type": "JSXElement",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":10,"end":16,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":16}},
"name": {
"type": "JSXIdentifier",
"start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15}},
"name": "div"
}
},
"children": [
{
"type": "JSXExpressionContainer",
"start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10}},
"expression": {
"type": "StringLiteral",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9}},
"extra": {
"rawValue": ">",
"raw": "'>'"
},
"value": ">"
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>}</div>

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}

View File

@ -0,0 +1,52 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "JSXElement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}},
"name": {
"type": "JSXIdentifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"name": "div"
}
},
"children": [
{
"type": "JSXText",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6}},
"extra": {
"rawValue": "}",
"raw": "}"
},
"value": "}"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>{'}'}</div>

View File

@ -0,0 +1,56 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"expression": {
"type": "JSXElement",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":10,"end":16,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":16}},
"name": {
"type": "JSXIdentifier",
"start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15}},
"name": "div"
}
},
"children": [
{
"type": "JSXExpressionContainer",
"start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10}},
"expression": {
"type": "StringLiteral",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9}},
"extra": {
"rawValue": "}",
"raw": "'}'"
},
"value": "}"
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>></div>

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}

View File

@ -0,0 +1,55 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"errors": [
"SyntaxError: Unexpected token `>`. Did you mean `&gt;` or `{'>'}`? (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "JSXElement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}},
"name": {
"type": "JSXIdentifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"name": "div"
}
},
"children": [
{
"type": "JSXText",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6}},
"extra": {
"rawValue": ">",
"raw": ">"
},
"value": ">"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
<div>}</div>

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}

View File

@ -0,0 +1,55 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"errors": [
"SyntaxError: Unexpected token `}`. Did you mean `&rbrace;` or `{'}'}`? (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "JSXElement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}},
"name": {
"type": "JSXIdentifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"name": "div"
}
},
"children": [
{
"type": "JSXText",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6}},
"extra": {
"rawValue": "}",
"raw": "}"
},
"value": "}"
}
]
}
}
],
"directives": []
}
}