Skip to content

Commit

Permalink
fix(compiler): workaround bugs in TS when combining transformers (#18912
Browse files Browse the repository at this point in the history
)

PR Close #18912
  • Loading branch information
tbosch authored and jasonaden committed Aug 31, 2017
1 parent f1e526f commit 4059a72
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/compiler-cli/src/transformers/lower_expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 4059a72

Please sign in to comment.