From 8348556b776d1457bb520d63f6b0b8ca0d3f2f6c Mon Sep 17 00:00:00 2001 From: JoostK Date: Mon, 16 Nov 2020 13:02:27 +0100 Subject: [PATCH] test(compiler-cli): workaround for performance cliff in TypeScript (#39707) The type checker had to do extensive work in resolving the `NodePath.get` method call for the `NodePath` that had an intersection type of `ts.VariableDeclarator&{init:t.Expression}`. The `NodePath.get` method is typed using a conditional type which became expensive to compute with this intersection type. As a workaround, the original `init` property is explicitly omitted which avoids the performance cliff. This brings down the compile time by 15s. PR Close #39707 --- .../babel/test/babel_declaration_scope_spec.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/compiler-cli/linker/babel/test/babel_declaration_scope_spec.ts b/packages/compiler-cli/linker/babel/test/babel_declaration_scope_spec.ts index b4c5545f61..ee0ebb89d9 100644 --- a/packages/compiler-cli/linker/babel/test/babel_declaration_scope_spec.ts +++ b/packages/compiler-cli/linker/babel/test/babel_declaration_scope_spec.ts @@ -10,7 +10,6 @@ import traverse, {NodePath} from '@babel/traverse'; import * as t from '@babel/types'; import {BabelDeclarationScope} from '../src/babel_declaration_scope'; - describe('BabelDeclarationScope', () => { describe('getConstantScopeRef()', () => { it('should return a path to the ES module where the expression was imported', () => { @@ -83,11 +82,19 @@ describe('BabelDeclarationScope', () => { }); }); +/** + * The type of a variable declarator that is known to have an initializer. + * + * Note: the `init` property is explicitly omitted to workaround a performance cliff in the + * TypeScript type checker. + */ +type InitializedVariableDeclarator = Omit&{init: t.Expression}; + function findVarDeclaration( - file: t.File, varName: string): NodePath { + file: t.File, varName: string): NodePath { let varDecl: NodePath|undefined = undefined; traverse(file, { - VariableDeclarator: (path) => { + VariableDeclarator: (path: NodePath) => { const id = path.get('id'); if (id.isIdentifier() && id.node.name === varName && path.get('init') !== null) { varDecl = path;