From d682cdd1aace6f87312aef26131303db53a66d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 2 Aug 2022 09:18:32 -0400 Subject: [PATCH 1/3] fix: parse a>>c as a<(b>>>c) --- .../src/plugins/typescript/index.ts | 2 + .../input.ts | 3 + .../output.json | 123 ++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index d00d1a2204b6..1c9b801cefff 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2508,6 +2508,8 @@ export default (superClass: ClassWithMixin) => if ( // a>c is not (a)>c, but a<(b>>c) tokenType === tt.gt || + // a>>c is not (a)>>c, but a<(b>>>c) + tokenType === tt.bitShiftR || // ac is (ac (tokenType !== tt.parenL && tokenCanStartExpression(tokenType) && diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/input.ts new file mode 100644 index 000000000000..684d9d10b8df --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/input.ts @@ -0,0 +1,3 @@ +let f, h = 0, j = 0; + +f = h >>> 0 < j >>> 0; diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/output.json new file mode 100644 index 000000000000..c6b731393cd4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-4/output.json @@ -0,0 +1,123 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":44}}, + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":44}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":5,"index":5}}, + "id": { + "type": "Identifier", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":5,"index":5},"identifierName":"f"}, + "name": "f" + }, + "init": null + }, + { + "type": "VariableDeclarator", + "start":7,"end":12,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":12,"index":12}}, + "id": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"h"}, + "name": "h" + }, + "init": { + "type": "NumericLiteral", + "start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "VariableDeclarator", + "start":14,"end":19,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":19,"index":19}}, + "id": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":15,"index":15},"identifierName":"j"}, + "name": "j" + }, + "init": { + "type": "NumericLiteral", + "start":18,"end":19,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":19,"index":19}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start":22,"end":44,"loc":{"start":{"line":3,"column":0,"index":22},"end":{"line":3,"column":22,"index":44}}, + "expression": { + "type": "AssignmentExpression", + "start":22,"end":43,"loc":{"start":{"line":3,"column":0,"index":22},"end":{"line":3,"column":21,"index":43}}, + "operator": "=", + "left": { + "type": "Identifier", + "start":22,"end":23,"loc":{"start":{"line":3,"column":0,"index":22},"end":{"line":3,"column":1,"index":23},"identifierName":"f"}, + "name": "f" + }, + "right": { + "type": "BinaryExpression", + "start":26,"end":43,"loc":{"start":{"line":3,"column":4,"index":26},"end":{"line":3,"column":21,"index":43}}, + "left": { + "type": "BinaryExpression", + "start":26,"end":33,"loc":{"start":{"line":3,"column":4,"index":26},"end":{"line":3,"column":11,"index":33}}, + "left": { + "type": "Identifier", + "start":26,"end":27,"loc":{"start":{"line":3,"column":4,"index":26},"end":{"line":3,"column":5,"index":27},"identifierName":"h"}, + "name": "h" + }, + "operator": ">>>", + "right": { + "type": "NumericLiteral", + "start":32,"end":33,"loc":{"start":{"line":3,"column":10,"index":32},"end":{"line":3,"column":11,"index":33}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + "operator": "<", + "right": { + "type": "BinaryExpression", + "start":36,"end":43,"loc":{"start":{"line":3,"column":14,"index":36},"end":{"line":3,"column":21,"index":43}}, + "left": { + "type": "Identifier", + "start":36,"end":37,"loc":{"start":{"line":3,"column":14,"index":36},"end":{"line":3,"column":15,"index":37},"identifierName":"j"}, + "name": "j" + }, + "operator": ">>>", + "right": { + "type": "NumericLiteral", + "start":42,"end":43,"loc":{"start":{"line":3,"column":20,"index":42},"end":{"line":3,"column":21,"index":43}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + } + } + ], + "directives": [] + } +} From 90aab8e98addbdca16e2f93adb36626d22a4592b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 2 Aug 2022 09:36:22 -0400 Subject: [PATCH 2/3] fix: bail out only when they could form bitshift --- .../src/plugins/typescript/index.ts | 9 ++-- .../input.ts | 1 + .../output.json | 51 +++++++++++++++++++ .../input.ts | 1 + .../output.json | 51 +++++++++++++++++++ 5 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 1c9b801cefff..4fff24c2ba0c 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2506,10 +2506,11 @@ export default (superClass: ClassWithMixin) => const tokenType = this.state.type; if ( - // a>c is not (a)>c, but a<(b>>c) - tokenType === tt.gt || - // a>>c is not (a)>>c, but a<(b>>>c) - tokenType === tt.bitShiftR || + (this.state.start === this.state.lastTokStart + 1 && + // a>c is not (a)>c, but a<(b>>c) + (tokenType === tt.gt || + // a>>c is not (a)>>c, but a<(b>>>c) + tokenType === tt.bitShiftR)) || // ac is (ac (tokenType !== tt.parenL && tokenCanStartExpression(tokenType) && diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts new file mode 100644 index 000000000000..929f46c802e9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts @@ -0,0 +1 @@ +a > c; diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json new file mode 100644 index 000000000000..ab436ec84967 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json @@ -0,0 +1,51 @@ +{ + "type": "File", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, + "program": { + "type": "Program", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, + "expression": { + "type": "BinaryExpression", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}}, + "left": { + "type": "TSInstantiationExpression", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}}, + "expression": { + "type": "Identifier", + "start":0,"end":1,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":1,"index":1},"identifierName":"a"}, + "name": "a" + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4}}, + "params": [ + { + "type": "TSTypeReference", + "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3}}, + "typeName": { + "type": "Identifier", + "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3},"identifierName":"b"}, + "name": "b" + } + } + ] + } + }, + "operator": ">", + "right": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"c"}, + "name": "c" + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts new file mode 100644 index 000000000000..5b90b10a4511 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts @@ -0,0 +1 @@ +a >> c; diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json new file mode 100644 index 000000000000..2b5e51509615 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json @@ -0,0 +1,51 @@ +{ + "type": "File", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, + "program": { + "type": "Program", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, + "expression": { + "type": "BinaryExpression", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, + "left": { + "type": "TSInstantiationExpression", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}}, + "expression": { + "type": "Identifier", + "start":0,"end":1,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":1,"index":1},"identifierName":"a"}, + "name": "a" + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4}}, + "params": [ + { + "type": "TSTypeReference", + "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3}}, + "typeName": { + "type": "Identifier", + "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3},"identifierName":"b"}, + "name": "b" + } + } + ] + } + }, + "operator": ">>", + "right": { + "type": "Identifier", + "start":8,"end":9,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":9,"index":9},"identifierName":"c"}, + "name": "c" + } + } + } + ], + "directives": [] + } +} From 3fb64aebbf5167c96066cf7c0803bb513e04a19d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 27 Aug 2022 13:26:40 -0400 Subject: [PATCH 3/3] Revert "fix: bail out only when they could form bitshift" This reverts commit c4136710fdd7d4112eb7fd284aae21a93f3b716e. --- .../src/plugins/typescript/index.ts | 9 ++-- .../input.ts | 1 - .../output.json | 51 ------------------- .../input.ts | 1 - .../output.json | 51 ------------------- 5 files changed, 4 insertions(+), 109 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts delete mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json delete mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts delete mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 4fff24c2ba0c..1c9b801cefff 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2506,11 +2506,10 @@ export default (superClass: ClassWithMixin) => const tokenType = this.state.type; if ( - (this.state.start === this.state.lastTokStart + 1 && - // a>c is not (a)>c, but a<(b>>c) - (tokenType === tt.gt || - // a>>c is not (a)>>c, but a<(b>>>c) - tokenType === tt.bitShiftR)) || + // a>c is not (a)>c, but a<(b>>c) + tokenType === tt.gt || + // a>>c is not (a)>>c, but a<(b>>>c) + tokenType === tt.bitShiftR || // ac is (ac (tokenType !== tt.parenL && tokenCanStartExpression(tokenType) && diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts deleted file mode 100644 index 929f46c802e9..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/input.ts +++ /dev/null @@ -1 +0,0 @@ -a > c; diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json deleted file mode 100644 index ab436ec84967..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-2/output.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "type": "File", - "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, - "program": { - "type": "Program", - "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ExpressionStatement", - "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, - "expression": { - "type": "BinaryExpression", - "start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}}, - "left": { - "type": "TSInstantiationExpression", - "start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}}, - "expression": { - "type": "Identifier", - "start":0,"end":1,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":1,"index":1},"identifierName":"a"}, - "name": "a" - }, - "typeParameters": { - "type": "TSTypeParameterInstantiation", - "start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4}}, - "params": [ - { - "type": "TSTypeReference", - "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3}}, - "typeName": { - "type": "Identifier", - "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3},"identifierName":"b"}, - "name": "b" - } - } - ] - } - }, - "operator": ">", - "right": { - "type": "Identifier", - "start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"c"}, - "name": "c" - } - } - } - ], - "directives": [] - } -} diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts deleted file mode 100644 index 5b90b10a4511..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/input.ts +++ /dev/null @@ -1 +0,0 @@ -a >> c; diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json deleted file mode 100644 index 2b5e51509615..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-binary-operator-3/output.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "type": "File", - "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, - "program": { - "type": "Program", - "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ExpressionStatement", - "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, - "expression": { - "type": "BinaryExpression", - "start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}}, - "left": { - "type": "TSInstantiationExpression", - "start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}}, - "expression": { - "type": "Identifier", - "start":0,"end":1,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":1,"index":1},"identifierName":"a"}, - "name": "a" - }, - "typeParameters": { - "type": "TSTypeParameterInstantiation", - "start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4}}, - "params": [ - { - "type": "TSTypeReference", - "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3}}, - "typeName": { - "type": "Identifier", - "start":2,"end":3,"loc":{"start":{"line":1,"column":2,"index":2},"end":{"line":1,"column":3,"index":3},"identifierName":"b"}, - "name": "b" - } - } - ] - } - }, - "operator": ">>", - "right": { - "type": "Identifier", - "start":8,"end":9,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":9,"index":9},"identifierName":"c"}, - "name": "c" - } - } - } - ], - "directives": [] - } -}