From 2603c2e227521e1fbccbaf5f357426e384d0cbb5 Mon Sep 17 00:00:00 2001 From: vages Date: Tue, 3 Mar 2020 17:27:41 +0000 Subject: [PATCH] =?UTF-8?q?fix(babel-parser):=20chain=20off=20optionally?= =?UTF-8?q?=20chained=20keys=20named=20cla=E2=80=A6=20(#11198)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(babel-parser): chain off optionally chained keys named class and function Fixes #11197 * Extract override as variable to satisfy typechecker Flow did not trust that curContext.override would be defined --- packages/babel-parser/src/tokenizer/index.js | 5 +- .../input.js | 4 + .../output.json | 469 ++++++++++++++++++ 3 files changed, 476 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/input.js create mode 100644 packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/output.json diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index ba544a9fb7..40b5e661c1 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -233,8 +233,9 @@ export default class Tokenizer extends LocationParser { return; } - if (curContext.override) { - curContext.override(this); + const override = curContext?.override; + if (override) { + override(this); } else { this.getTokenFromCode(this.input.codePointAt(this.state.pos)); } diff --git a/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/input.js b/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/input.js new file mode 100644 index 0000000000..5428e236f7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/input.js @@ -0,0 +1,4 @@ +foo?.class.bar +foo?.function.bar +foo?.bar?.class.bar +foo?.function?.bar diff --git a/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/output.json b/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/output.json new file mode 100644 index 0000000000..9f59d5d3c2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/optional-chaining/chaining-off-optionally-chained-keys-named-class-or-function/output.json @@ -0,0 +1,469 @@ +{ + "type": "File", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "OptionalMemberExpression", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "object": { + "type": "OptionalMemberExpression", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "class" + }, + "name": "class" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "computed": false, + "optional": false + } + }, + { + "type": "ExpressionStatement", + "start": 15, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "expression": { + "type": "OptionalMemberExpression", + "start": 15, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "object": { + "type": "OptionalMemberExpression", + "start": 15, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "object": { + "type": "Identifier", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "property": { + "type": "Identifier", + "start": 20, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "function" + }, + "name": "function" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "computed": false, + "optional": false + } + }, + { + "type": "ExpressionStatement", + "start": 33, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "expression": { + "type": "OptionalMemberExpression", + "start": 33, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "object": { + "type": "OptionalMemberExpression", + "start": 33, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "object": { + "type": "OptionalMemberExpression", + "start": 33, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "object": { + "type": "Identifier", + "start": 33, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "property": { + "type": "Identifier", + "start": 38, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 43, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "class" + }, + "name": "class" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 49, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 19 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "computed": false, + "optional": false + } + }, + { + "type": "ExpressionStatement", + "start": 53, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "expression": { + "type": "OptionalMemberExpression", + "start": 53, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "object": { + "type": "OptionalMemberExpression", + "start": 53, + "end": 66, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "object": { + "type": "Identifier", + "start": 53, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "property": { + "type": "Identifier", + "start": 58, + "end": 66, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "function" + }, + "name": "function" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 68, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 18 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "computed": false, + "optional": true + } + } + ], + "directives": [] + } +} \ No newline at end of file