@@ -67,7 +67,7 @@ function transformSourceFile(
67
67
}
68
68
69
69
function visitSourceFile ( sourceFile : ts . SourceFile ) : ts . SourceFile {
70
- function topLevelStatement ( node : ts . Node ) : ts . Node {
70
+ function topLevelStatement ( node : ts . Statement ) : ts . Statement {
71
71
const declarations : Declaration [ ] = [ ] ;
72
72
73
73
function visitNode ( node : ts . Node ) : ts . Node {
@@ -99,12 +99,11 @@ function transformSourceFile(
99
99
return result ;
100
100
}
101
101
102
- const traversedSource = ts . visitEachChild ( sourceFile , topLevelStatement , context ) ;
102
+ const newStatements = sourceFile . statements . map ( topLevelStatement ) ;
103
103
104
104
if ( inserts . length ) {
105
105
// Insert the declarations before the rewritten statement that references them.
106
106
const insertMap = toMap ( inserts , i => i . priorTo ) ;
107
- const newStatements : ts . Statement [ ] = [ ...traversedSource . statements ] ;
108
107
for ( let i = newStatements . length ; i >= 0 ; i -- ) {
109
108
const statement = newStatements [ i ] ;
110
109
const insert = insertMap . get ( statement ) ;
@@ -131,9 +130,16 @@ function transformSourceFile(
131
130
. map (
132
131
declaration => ts . createExportSpecifier (
133
132
/* propertyName */ undefined , declaration . name ) ) ) ) ) ;
134
- return ts . updateSourceFileNode ( traversedSource , newStatements ) ;
135
133
}
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 ;
137
143
}
138
144
139
145
return visitSourceFile ( sourceFile ) ;
0 commit comments