From a1063d2583e9fcbd782be369e46cdf44b236fc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 16 Jan 2020 09:34:47 -0500 Subject: [PATCH] fix: triple __proto__ in object patterns should be allowed (#11009) --- .../babel-parser/src/parser/expression.js | 9 +- .../destructuring/duplicate-proto-4/input.js | 1 + .../duplicate-proto-4/output.json | 260 ++++++++++++++++++ 3 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 86c4f0808d78..2598eefc1d47 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -88,10 +88,13 @@ export default class ExpressionParser extends LValParser { const name = key.type === "Identifier" ? key.name : String(key.value); if (name === "__proto__") { - // Store the first redefinition's position if (protoRef.used) { - if (refExpressionErrors && refExpressionErrors.doubleProto === -1) { - refExpressionErrors.doubleProto = key.start; + if (refExpressionErrors) { + // Store the first redefinition's position, otherwise ignore because + // we are parsing ambiguous pattern + if (refExpressionErrors.doubleProto === -1) { + refExpressionErrors.doubleProto = key.start; + } } else { this.raise(key.start, "Redefinition of __proto__ property"); } diff --git a/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/input.js b/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/input.js new file mode 100644 index 000000000000..4a4a220727ab --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/input.js @@ -0,0 +1 @@ +({ __proto__: x, __proto__: y, __proto__: z }) => {}; diff --git a/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/output.json b/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/output.json new file mode 100644 index 000000000000..7b98cc79a762 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/destructuring/duplicate-proto-4/output.json @@ -0,0 +1,260 @@ +{ + "type": "File", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "start": 1, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 3, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 3, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "__proto__" + }, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + { + "type": "ObjectProperty", + "start": 17, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 17, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 26 + }, + "identifierName": "__proto__" + }, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + { + "type": "ObjectProperty", + "start": 31, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 31, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 40 + }, + "identifierName": "__proto__" + }, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start": 42, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 43 + }, + "identifierName": "z" + }, + "name": "z" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start": 50, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + } +}