From e5de5888b87b41e376e5863a77c5828c22355ef3 Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Sun, 4 Nov 2018 20:18:23 -0800 Subject: [PATCH] Restrict TS try/catch logic to cases where backtracking is needed Fixes #326 When porting some babel-parser changes, there was some code in TS parsing that was unconditionally wrapping some parsing in a try/catch, but actually it only cared about cases that started with `<`. By inverting the logic a little, we avoid having to throw and catch an exception in the common case of parsing, which was dramatically slowing down TypeScript parsing. --- src/parser/plugins/typescript.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/parser/plugins/typescript.ts b/src/parser/plugins/typescript.ts index 03aed6c4..f63cc14c 100644 --- a/src/parser/plugins/typescript.ts +++ b/src/parser/plugins/typescript.ts @@ -1093,10 +1093,10 @@ export function tsParseSubscript( return; } - // There are number of things we are going to "maybe" parse, like type arguments on - // tagged template expressions. If any of them fail, walk it back and continue. - const success = tsTryParseAndCatch(() => { - if (match(tt.lessThan)) { + if (match(tt.lessThan)) { + // There are number of things we are going to "maybe" parse, like type arguments on + // tagged template expressions. If any of them fail, walk it back and continue. + const success = tsTryParseAndCatch(() => { if (!noCalls && atPossibleAsync()) { // Almost certainly this is a generic async function `async () => ... // But it might be a call with a type argument `async();` @@ -1108,17 +1108,14 @@ export function tsParseSubscript( tsParseTypeArguments(); if (!noCalls && eat(tt.parenL)) { parseCallExpressionArguments(tt.parenR); - return; } else if (match(tt.backQuote)) { // Tagged template with a type argument. parseTemplate(); - return; } + }); + if (success) { + return; } - unexpected(); - }); - if (success) { - return; } baseParseSubscript(startPos, noCalls, stopState); }