diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index eeab6202bad0..645d1daa5503 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2493,10 +2493,11 @@ export default (superClass: { 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": [] + } +}