From 4059a72710f8f2ffa8566291a3e526798cf714b3 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 29 Aug 2017 16:26:37 -0700 Subject: [PATCH] fix(compiler): workaround bugs in TS when combining transformers (#18912) PR Close #18912 --- .../src/transformers/lower_expressions.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/compiler-cli/src/transformers/lower_expressions.ts b/packages/compiler-cli/src/transformers/lower_expressions.ts index a6a1e5e47d..11666c96dc 100644 --- a/packages/compiler-cli/src/transformers/lower_expressions.ts +++ b/packages/compiler-cli/src/transformers/lower_expressions.ts @@ -67,7 +67,7 @@ function transformSourceFile( } function visitSourceFile(sourceFile: ts.SourceFile): ts.SourceFile { - function topLevelStatement(node: ts.Node): ts.Node { + function topLevelStatement(node: ts.Statement): ts.Statement { const declarations: Declaration[] = []; function visitNode(node: ts.Node): ts.Node { @@ -99,12 +99,11 @@ function transformSourceFile( return result; } - const traversedSource = ts.visitEachChild(sourceFile, topLevelStatement, context); + const newStatements = sourceFile.statements.map(topLevelStatement); if (inserts.length) { // Insert the declarations before the rewritten statement that references them. const insertMap = toMap(inserts, i => i.priorTo); - const newStatements: ts.Statement[] = [...traversedSource.statements]; for (let i = newStatements.length; i >= 0; i--) { const statement = newStatements[i]; const insert = insertMap.get(statement); @@ -131,9 +130,16 @@ function transformSourceFile( .map( declaration => ts.createExportSpecifier( /* propertyName */ undefined, declaration.name))))); - return ts.updateSourceFileNode(traversedSource, newStatements); } - return traversedSource; + // Note: We cannot use ts.updateSourcefile here as + // it does not work well with decorators. + // See https://github.com/Microsoft/TypeScript/issues/17384 + const newSf = ts.getMutableClone(sourceFile); + if (!(sourceFile.flags & ts.NodeFlags.Synthesized)) { + newSf.flags &= ~ts.NodeFlags.Synthesized; + } + newSf.statements = ts.setTextRange(ts.createNodeArray(newStatements), sourceFile.statements); + return newSf; } return visitSourceFile(sourceFile);