From c5f68eb62eeb3fea45106548fcf5d227f474d73f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 17 May 2023 03:37:46 -0400 Subject: [PATCH] rescan gt token at the end of type args parsing (#15631) --- .../src/plugins/typescript/index.ts | 4 ++ .../input.ts | 1 + .../output.json | 58 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 65d8e2910569..1a47e8c62762 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2281,6 +2281,10 @@ export default (superClass: ClassWithMixin) => ); if (node.params.length === 0) { this.raise(TSErrors.EmptyTypeArguments, { at: node }); + } else if (!this.state.inType && this.curContext() === tc.brace) { + // rescan `>` when we are no longer in type context and JSX parsing context + // since it was tokenized when `inType` is `true`. + this.reScan_lt_gt(); } this.expect(tt.gt); return this.finishNode(node, "TSTypeParameterInstantiation"); diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/input.ts b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/input.ts new file mode 100644 index 000000000000..3b189032f39b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/input.ts @@ -0,0 +1 @@ +fn(x < y, x >= y) diff --git a/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/output.json b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/output.json new file mode 100644 index 000000000000..326dece9d2be --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-false-positive-5/output.json @@ -0,0 +1,58 @@ +{ + "type": "File", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "program": { + "type": "Program", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "expression": { + "type": "CallExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "callee": { + "type": "Identifier", + "start":0,"end":2,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":2,"index":2},"identifierName":"fn"}, + "name": "fn" + }, + "arguments": [ + { + "type": "BinaryExpression", + "start":3,"end":8,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":8,"index":8}}, + "left": { + "type": "Identifier", + "start":3,"end":4,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":4,"index":4},"identifierName":"x"}, + "name": "x" + }, + "operator": "<", + "right": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"y"}, + "name": "y" + } + }, + { + "type": "BinaryExpression", + "start":10,"end":16,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":16,"index":16}}, + "left": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":11,"index":11},"identifierName":"x"}, + "name": "x" + }, + "operator": ">=", + "right": { + "type": "Identifier", + "start":15,"end":16,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":16,"index":16},"identifierName":"y"}, + "name": "y" + } + } + ] + } + } + ], + "directives": [] + } +}