From de7af54b617b8d6eb780bd0469f6a8b729e232ad Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Thu, 11 Nov 2021 12:37:54 +0530 Subject: [PATCH 1/2] fix: no error on doubleProto with assignment expression --- .../babel-parser/src/parser/expression.js | 5 +- .../with-assignment-expression/input.js | 5 + .../with-assignment-expression/output.json | 95 +++++++++++++++++++ .../without-assignment-expression/input.js | 4 + .../without-assignment-expression/output.json | 65 +++++++++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/output.json create mode 100644 packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 1ae4558ce060..bcd609713771 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -309,7 +309,10 @@ export default class ExpressionParser extends LValParser { if (this.match(tt.eq)) { node.left = this.toAssignable(left, /* isLHS */ true); - refExpressionErrors.doubleProto = -1; // reset because double __proto__ is valid in assignment expression + + if (refExpressionErrors.doubleProto >= node.left.start) { + refExpressionErrors.doubleProto = -1; // reset because double __proto__ is valid in assignment expression + } } else { node.left = left; } diff --git a/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/input.js b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/input.js new file mode 100644 index 000000000000..f3b24d345e00 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/input.js @@ -0,0 +1,5 @@ +({ + __proto__: a, + __proto__: a, + a: a = 1 +}) diff --git a/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/output.json b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/output.json new file mode 100644 index 000000000000..a3c1c2c1370e --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/with-assignment-expression/output.json @@ -0,0 +1,95 @@ +{ + "type": "File", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, + "errors": [ + "SyntaxError: Redefinition of __proto__ property. (3:2)" + ], + "program": { + "type": "Program", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":47,"loc":{"start":{"line":1,"column":1},"end":{"line":5,"column":1}}, + "properties": [ + { + "type": "ObjectProperty", + "start":5,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, + "method": false, + "key": { + "type": "Identifier", + "start":5,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":11},"identifierName":"__proto__"}, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":14},"identifierName":"a"}, + "name": "a" + } + }, + { + "type": "ObjectProperty", + "start":21,"end":33,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":14}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":30,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":11},"identifierName":"__proto__"}, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":14},"identifierName":"a"}, + "name": "a" + } + }, + { + "type": "ObjectProperty", + "start":37,"end":45,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":10}}, + "method": false, + "key": { + "type": "Identifier", + "start":37,"end":38,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":3},"identifierName":"a"}, + "name": "a" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "AssignmentExpression", + "start":40,"end":45,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":10}}, + "operator": "=", + "left": { + "type": "Identifier", + "start":40,"end":41,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":6},"identifierName":"a"}, + "name": "a" + }, + "right": { + "type": "NumericLiteral", + "start":44,"end":45,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":10}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/input.js b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/input.js new file mode 100644 index 000000000000..fb1b2692db6b --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/input.js @@ -0,0 +1,4 @@ +({ + __proto__: a, + __proto__: a, +}) diff --git a/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/output.json b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/output.json new file mode 100644 index 000000000000..30b9b4d3ba07 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/duplicate-proto/without-assignment-expression/output.json @@ -0,0 +1,65 @@ +{ + "type": "File", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "errors": [ + "SyntaxError: Redefinition of __proto__ property. (3:2)" + ], + "program": { + "type": "Program", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":36,"loc":{"start":{"line":1,"column":1},"end":{"line":4,"column":1}}, + "properties": [ + { + "type": "ObjectProperty", + "start":5,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, + "method": false, + "key": { + "type": "Identifier", + "start":5,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":11},"identifierName":"__proto__"}, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":14},"identifierName":"a"}, + "name": "a" + } + }, + { + "type": "ObjectProperty", + "start":21,"end":33,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":14}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":30,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":11},"identifierName":"__proto__"}, + "name": "__proto__" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":14},"identifierName":"a"}, + "name": "a" + } + } + ], + "extra": { + "trailingComma": 33, + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 0b8cd9145a5d7f01b933e1df9f402ac79b18c77d Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 12 Nov 2021 16:32:18 +0530 Subject: [PATCH 2/2] improved doubleProto handling --- .../babel-parser/src/parser/expression.js | 10 ++- .../es2015/uncategorised/348/input.js | 1 - .../es2015/uncategorised/348/output.json | 72 ------------------- 3 files changed, 4 insertions(+), 79 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/es2015/uncategorised/348/input.js delete mode 100644 packages/babel-parser/test/fixtures/es2015/uncategorised/348/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index bcd609713771..4b37841ca9d1 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -310,19 +310,17 @@ export default class ExpressionParser extends LValParser { if (this.match(tt.eq)) { node.left = this.toAssignable(left, /* isLHS */ true); - if (refExpressionErrors.doubleProto >= node.left.start) { + if (refExpressionErrors.doubleProto >= startPos) { refExpressionErrors.doubleProto = -1; // reset because double __proto__ is valid in assignment expression } + if (refExpressionErrors.shorthandAssign >= startPos) { + refExpressionErrors.shorthandAssign = -1; // reset because shorthand default was used correctly + } } else { node.left = left; } - if (refExpressionErrors.shorthandAssign >= node.left.start) { - refExpressionErrors.shorthandAssign = -1; // reset because shorthand default was used correctly - } - this.checkLVal(left, "assignment expression"); - this.next(); node.right = this.parseMaybeAssign(); return this.finishNode(node, "AssignmentExpression"); diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/348/input.js b/packages/babel-parser/test/fixtures/es2015/uncategorised/348/input.js deleted file mode 100644 index ce57514e8815..000000000000 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/348/input.js +++ /dev/null @@ -1 +0,0 @@ -({ __proto__: 1, __proto__: 2 }) \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/348/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/348/output.json deleted file mode 100644 index 6101e4acbfc0..000000000000 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/348/output.json +++ /dev/null @@ -1,72 +0,0 @@ -{ - "type": "File", - "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}}, - "errors": [ - "SyntaxError: Redefinition of __proto__ property. (1:17)" - ], - "program": { - "type": "Program", - "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}}, - "sourceType": "script", - "interpreter": null, - "body": [ - { - "type": "ExpressionStatement", - "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}}, - "expression": { - "type": "ObjectExpression", - "start":1,"end":31,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":31}}, - "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": "NumericLiteral", - "start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15}}, - "extra": { - "rawValue": 1, - "raw": "1" - }, - "value": 1 - } - }, - { - "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": "NumericLiteral", - "start":28,"end":29,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":29}}, - "extra": { - "rawValue": 2, - "raw": "2" - }, - "value": 2 - } - } - ], - "extra": { - "parenthesized": true, - "parenStart": 0 - } - } - } - ], - "directives": [] - } -} \ No newline at end of file