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

fix: preserve tokensLength in tryParse #13326

Merged
merged 2 commits into from May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -216,6 +216,10 @@ export default class UtilParser extends Tokenizer {
if (this.state.errors.length > oldState.errors.length) {
const failState = this.state;
this.state = oldState;
// The tokensLength should be preserved on error recovery mode
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
// since parser does not halt and instead it will parse the
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
// remaining tokens
this.state.tokensLength = failState.tokensLength;
return {
node,
error: (failState.errors[oldState.errors.length]: SyntaxError),
Expand Down
@@ -0,0 +1 @@
<div propA={[key: value]} />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently the tokens of parsed AST is a length-14 array

[ tt.jsxTagStart, tt.name, undefined, undefined, ... , tt.eof]

babel7Compat thus throws because the tokens unexpectedly includes undefined.

@@ -0,0 +1,4 @@
{
"tokens": true,
"plugins": ["jsx", "flow"]
}
@@ -0,0 +1,292 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"errors": [
"SyntaxError: The type cast expression is expected to be wrapped with parenthesis. (1:16)"
],
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"expression": {
"type": "JSXElement",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
},
"attributes": [
{
"type": "JSXAttribute",
"start":5,"end":25,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":25}},
"name": {
"type": "JSXIdentifier",
"start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10}},
"name": "propA"
},
"value": {
"type": "JSXExpressionContainer",
"start":11,"end":25,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":25}},
"expression": {
"type": "ArrayExpression",
"start":12,"end":24,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":24}},
"elements": [
{
"type": "TypeCastExpression",
"start":13,"end":23,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":23}},
"expression": {
"type": "Identifier",
"start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16},"identifierName":"key"},
"name": "key"
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start":16,"end":23,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":23}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23},"identifierName":"value"},
"name": "value"
}
}
}
}
]
}
}
}
],
"selfClosing": true
},
"closingElement": null,
"children": []
}
}
],
"directives": []
},
"tokens": [
{
"type": {
"label": "jsxTagStart",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}}
},
{
"type": {
"label": "jsxName",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": "div",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}}
},
{
"type": {
"label": "jsxName",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": "propA",
"start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10}}
},
{
"type": {
"label": "=",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": true,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": "=",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11}}
},
{
"type": {
"label": "{",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":11,"end":12,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":12}}
},
{
"type": {
"label": "[",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13}}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "key",
"start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16}}
},
{
"type": {
"label": ":",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "value",
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}}
},
{
"type": {
"label": "]",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":23,"end":24,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":24}}
},
{
"type": {
"label": "}",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":24,"end":25,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":25}}
},
{
"type": {
"label": "/",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": 10,
"updateContext": null
},
"value": "/",
"start":26,"end":27,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":27}}
},
{
"type": {
"label": "jsxTagEnd",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":27,"end":28,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":28}}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":28,"end":28,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":28}}
}
]
}