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

Add better parser error when using jsx #11722

Merged
merged 6 commits into from Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1170,6 +1170,12 @@ export default class ExpressionParser extends LValParser {
}
}
// fall through
case tt.relational: {
if (this.state.value === "<") {
throw this.expectOnePlugin(["jsx", "flow", "typescript"]);
}
}
// fall through
default:
throw this.unexpected();
}
Expand Down
@@ -0,0 +1 @@
<div>{name}</div>
@@ -0,0 +1,4 @@
{
"plugins": [],
"throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (1:0)"
}
@@ -0,0 +1,2 @@
"use strict"
<div
@@ -0,0 +1,36 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":4}},
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":4}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":4}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":4}},
"left": {
"type": "StringLiteral",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"extra": {
"rawValue": "use strict",
"raw": "\"use strict\""
},
"value": "use strict"
},
"operator": "<",
"right": {
"type": "Identifier",
"start":14,"end":17,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":4},"identifierName":"div"},
"name": "div"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
<div>() => {}
penguingovernor marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,43 @@
{
"type": "File",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"program": {
"type": "Program",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"expression": {
"type": "ArrowFunctionExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"params": [
{
"type": "TypeParameter",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div",
"variance": null
}
]
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
<div></div>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add the following test case?

<div>{name}</div>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and done (:

@@ -0,0 +1,4 @@
{
"throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (1:0)",
"plugins": []
}