From 07862e72727db638513d66194a7376ce82a5632e Mon Sep 17 00:00:00 2001 From: Matthew Robertson Date: Mon, 1 Oct 2018 10:59:16 -0700 Subject: [PATCH] Fix perf issue in typescript parser plugin (#8792) --- packages/babel-parser/src/plugins/typescript.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index ef3c075ccc..10561da600 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -1368,10 +1368,11 @@ export default (superClass: Class): Class => return this.finishNode(nonNullExpression, "TSNonNullExpression"); } - // 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 result = this.tsTryParseAndCatch(() => { - if (this.isRelational("<")) { + if (this.isRelational("<")) { + // tsTryParseAndCatch is expensive, so avoid if not necessary. + // 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 result = this.tsTryParseAndCatch(() => { if (!noCalls && this.atPossibleAsync(base)) { // Almost certainly this is a generic async function `async () => ... // But it might be a call with a type argument `async();` @@ -1409,12 +1410,12 @@ export default (superClass: Class): Class => ); } } - } - this.unexpected(); - }); + this.unexpected(); + }); - if (result) return result; + if (result) return result; + } return super.parseSubscript(base, startPos, startLoc, noCalls, state); }