diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index c4ab4f780b38..a205518396f5 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1469,35 +1469,6 @@ export default (superClass: Class): Class => ); } - tsTryParseTypeArguments(): ?N.TsTypeParameterInstantiation { - if (!this.isRelational("<")) { - return undefined; - } - const state = this.state.clone(); - const node = this.startNode(); - const params = this.tsInType(() => - // Temporarily remove a JSX parsing context, which makes us scan different tokens. - this.tsInNoContext(() => { - this.next(); - return this.tsTryParseDelimitedList( - "TypeParametersOrArguments", - this.tsParseType.bind(this), - ); - }), - ); - if (params) { - node.params = params; - // This reads the next token after the `>` too, so do this in the enclosing context. - // But be sure not to parse a regex in the jsx expression ` />`, so set exprAllowed = false - this.state.exprAllowed = false; - if (this.eatRelational(">")) { - return this.finishNode(node, "TSTypeParameterInstantiation"); - } - } - this.state = state; - return undefined; - } - tsParseTypeArguments(): N.TsTypeParameterInstantiation { const node = this.startNode(); node.params = this.tsInType(() => @@ -1656,28 +1627,27 @@ export default (superClass: Class): Class => const node: N.CallExpression = this.startNodeAt(startPos, startLoc); node.callee = base; - const typeArguments = this.tsTryParseTypeArguments(); - if (!typeArguments) { - return undefined; - } - - if (!noCalls && this.eat(tt.parenL)) { - // possibleAsync always false here, because we would have handled it above. - // $FlowIgnore (won't be any undefined arguments) - node.arguments = this.parseCallExpressionArguments( - tt.parenR, - /* possibleAsync */ false, - ); - node.typeParameters = typeArguments; - return this.finishCallExpression(node); - } else if (this.match(tt.backQuote)) { - return this.parseTaggedTemplateExpression( - startPos, - startLoc, - base, - state, - typeArguments, - ); + const typeArguments = this.tsParseTypeArguments(); + + if (typeArguments) { + if (!noCalls && this.eat(tt.parenL)) { + // possibleAsync always false here, because we would have handled it above. + // $FlowIgnore (won't be any undefined arguments) + node.arguments = this.parseCallExpressionArguments( + tt.parenR, + /* possibleAsync */ false, + ); + node.typeParameters = typeArguments; + return this.finishCallExpression(node); + } else if (this.match(tt.backQuote)) { + return this.parseTaggedTemplateExpression( + startPos, + startLoc, + base, + state, + typeArguments, + ); + } } this.unexpected(); @@ -2473,8 +2443,12 @@ export default (superClass: Class): Class => jsxParseOpeningElementAfterName( node: N.JSXOpeningElement, ): N.JSXOpeningElement { - const typeArguments = this.tsTryParseTypeArguments(); - if (typeArguments) node.typeParameters = typeArguments; + if (this.isRelational("<")) { + const typeArguments = this.tsTryParseAndCatch(() => + this.tsParseTypeArguments(), + ); + if (typeArguments) node.typeParameters = typeArguments; + } return super.jsxParseOpeningElementAfterName(node); }