From 0d0950f3419a59fb452ed205ef8505fb7d6d2535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 17 May 2021 15:32:12 -0400 Subject: [PATCH] fix: preserve tokensLength in tryParse (#13326) * fix: preserve tokensLength in tryParse * Apply suggestions from code review Co-authored-by: Brian Ng Co-authored-by: Brian Ng --- packages/babel-parser/src/parser/util.js | 4 + .../input.js | 1 + .../options.json | 4 + .../output.json | 292 ++++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/options.json create mode 100644 packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/output.json diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index 6b768d6837eb..f1106f45864a 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -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; + // tokensLength should be preserved during error recovery mode + // since the parser does not halt and will instead parse the + // remaining tokens + this.state.tokensLength = failState.tokensLength; return { node, error: (failState.errors[oldState.errors.length]: SyntaxError), diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/input.js b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/input.js new file mode 100644 index 000000000000..37f70db59856 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/input.js @@ -0,0 +1 @@ +
diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/options.json b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/options.json new file mode 100644 index 000000000000..16d477920e64 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/options.json @@ -0,0 +1,4 @@ +{ + "tokens": true, + "plugins": ["jsx", "flow"] +} diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/output.json b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/output.json new file mode 100644 index 000000000000..286e473eb3b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens-jsx-tokens-true/output.json @@ -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}} + } + ] +} \ No newline at end of file