Skip to content

Commit 4059a72

Browse files
tboschjasonaden
authored andcommitted
fix(compiler): workaround bugs in TS when combining transformers (#18912)
PR Close #18912
1 parent f1e526f commit 4059a72

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/compiler-cli/src/transformers/lower_expressions.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function transformSourceFile(
6767
}
6868

6969
function visitSourceFile(sourceFile: ts.SourceFile): ts.SourceFile {
70-
function topLevelStatement(node: ts.Node): ts.Node {
70+
function topLevelStatement(node: ts.Statement): ts.Statement {
7171
const declarations: Declaration[] = [];
7272

7373
function visitNode(node: ts.Node): ts.Node {
@@ -99,12 +99,11 @@ function transformSourceFile(
9999
return result;
100100
}
101101

102-
const traversedSource = ts.visitEachChild(sourceFile, topLevelStatement, context);
102+
const newStatements = sourceFile.statements.map(topLevelStatement);
103103

104104
if (inserts.length) {
105105
// Insert the declarations before the rewritten statement that references them.
106106
const insertMap = toMap(inserts, i => i.priorTo);
107-
const newStatements: ts.Statement[] = [...traversedSource.statements];
108107
for (let i = newStatements.length; i >= 0; i--) {
109108
const statement = newStatements[i];
110109
const insert = insertMap.get(statement);
@@ -131,9 +130,16 @@ function transformSourceFile(
131130
.map(
132131
declaration => ts.createExportSpecifier(
133132
/* propertyName */ undefined, declaration.name)))));
134-
return ts.updateSourceFileNode(traversedSource, newStatements);
135133
}
136-
return traversedSource;
134+
// Note: We cannot use ts.updateSourcefile here as
135+
// it does not work well with decorators.
136+
// See https://github.com/Microsoft/TypeScript/issues/17384
137+
const newSf = ts.getMutableClone(sourceFile);
138+
if (!(sourceFile.flags & ts.NodeFlags.Synthesized)) {
139+
newSf.flags &= ~ts.NodeFlags.Synthesized;
140+
}
141+
newSf.statements = ts.setTextRange(ts.createNodeArray(newStatements), sourceFile.statements);
142+
return newSf;
137143
}
138144

139145
return visitSourceFile(sourceFile);

0 commit comments

Comments
 (0)