@@ -154,25 +154,29 @@ namespace ts {
154
154
* @param transforms An array of Transformers.
155
155
*/
156
156
export function transformFiles ( resolver : EmitResolver , host : EmitHost , sourceFiles : SourceFile [ ] , transformers : Transformer [ ] ) : TransformationResult {
157
- const lexicalEnvironmentVariableDeclarationsStack : VariableDeclaration [ ] [ ] = [ ] ;
158
- const lexicalEnvironmentFunctionDeclarationsStack : FunctionDeclaration [ ] [ ] = [ ] ;
159
157
const enabledSyntaxKindFeatures = new Array < SyntaxKindFeatureFlags > ( SyntaxKind . Count ) ;
160
158
159
+ let lexicalEnvironmentDisabled = false ;
160
+
161
161
let lexicalEnvironmentStackOffset = 0 ;
162
- let hoistedVariableDeclarations : VariableDeclaration [ ] ;
163
- let hoistedFunctionDeclarations : FunctionDeclaration [ ] ;
164
- let lexicalEnvironmentDisabled : boolean ;
162
+ let lexicalEnvironmentVariableDeclarations : VariableDeclaration [ ] ;
163
+ let lexicalEnvironmentFunctionDeclarations : FunctionDeclaration [ ] ;
164
+ let lexicalEnvironmentVariableDeclarationsStack : VariableDeclaration [ ] [ ] = [ ] ;
165
+ let lexicalEnvironmentFunctionDeclarationsStack : FunctionDeclaration [ ] [ ] = [ ] ;
166
+ let lexicalEnvironmentSuspended = false ;
165
167
166
168
// The transformation context is provided to each transformer as part of transformer
167
169
// initialization.
168
170
const context : TransformationContext = {
169
171
getCompilerOptions : ( ) => host . getCompilerOptions ( ) ,
170
172
getEmitResolver : ( ) => resolver ,
171
173
getEmitHost : ( ) => host ,
172
- hoistVariableDeclaration,
173
- hoistFunctionDeclaration,
174
174
startLexicalEnvironment,
175
+ suspendLexicalEnvironment,
176
+ resumeLexicalEnvironment,
175
177
endLexicalEnvironment,
178
+ hoistVariableDeclaration,
179
+ hoistFunctionDeclaration,
176
180
onSubstituteNode : ( _emitContext , node ) => node ,
177
181
enableSubstitution,
178
182
isSubstitutionEnabled,
@@ -285,11 +289,11 @@ namespace ts {
285
289
function hoistVariableDeclaration ( name : Identifier ) : void {
286
290
Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot modify the lexical environment during the print phase." ) ;
287
291
const decl = createVariableDeclaration ( name ) ;
288
- if ( ! hoistedVariableDeclarations ) {
289
- hoistedVariableDeclarations = [ decl ] ;
292
+ if ( ! lexicalEnvironmentVariableDeclarations ) {
293
+ lexicalEnvironmentVariableDeclarations = [ decl ] ;
290
294
}
291
295
else {
292
- hoistedVariableDeclarations . push ( decl ) ;
296
+ lexicalEnvironmentVariableDeclarations . push ( decl ) ;
293
297
}
294
298
}
295
299
@@ -298,11 +302,11 @@ namespace ts {
298
302
*/
299
303
function hoistFunctionDeclaration ( func : FunctionDeclaration ) : void {
300
304
Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot modify the lexical environment during the print phase." ) ;
301
- if ( ! hoistedFunctionDeclarations ) {
302
- hoistedFunctionDeclarations = [ func ] ;
305
+ if ( ! lexicalEnvironmentFunctionDeclarations ) {
306
+ lexicalEnvironmentFunctionDeclarations = [ func ] ;
303
307
}
304
308
else {
305
- hoistedFunctionDeclarations . push ( func ) ;
309
+ lexicalEnvironmentFunctionDeclarations . push ( func ) ;
306
310
}
307
311
}
308
312
@@ -312,16 +316,31 @@ namespace ts {
312
316
*/
313
317
function startLexicalEnvironment ( ) : void {
314
318
Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot start a lexical environment during the print phase." ) ;
319
+ Debug . assert ( ! lexicalEnvironmentSuspended , "Lexical environment is suspended." ) ;
315
320
316
321
// Save the current lexical environment. Rather than resizing the array we adjust the
317
322
// stack size variable. This allows us to reuse existing array slots we've
318
323
// already allocated between transformations to avoid allocation and GC overhead during
319
324
// transformation.
320
- lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] = hoistedVariableDeclarations ;
321
- lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] = hoistedFunctionDeclarations ;
325
+ lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] = lexicalEnvironmentVariableDeclarations ;
326
+ lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] = lexicalEnvironmentFunctionDeclarations ;
322
327
lexicalEnvironmentStackOffset ++ ;
323
- hoistedVariableDeclarations = undefined ;
324
- hoistedFunctionDeclarations = undefined ;
328
+ lexicalEnvironmentVariableDeclarations = undefined ;
329
+ lexicalEnvironmentFunctionDeclarations = undefined ;
330
+ }
331
+
332
+ /** Suspends the current lexical environment, usually after visiting a parameter list. */
333
+ function suspendLexicalEnvironment ( ) : void {
334
+ Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot suspend a lexical environment during the print phase." ) ;
335
+ Debug . assert ( ! lexicalEnvironmentSuspended , "Lexical environment is already suspended." ) ;
336
+ lexicalEnvironmentSuspended = true ;
337
+ }
338
+
339
+ /** Resumes a suspended lexical environment, usually before visiting a function body. */
340
+ function resumeLexicalEnvironment ( ) : void {
341
+ Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot resume a lexical environment during the print phase." ) ;
342
+ Debug . assert ( lexicalEnvironmentSuspended , "Lexical environment is not suspended suspended." ) ;
343
+ lexicalEnvironmentSuspended = false ;
325
344
}
326
345
327
346
/**
@@ -330,17 +349,18 @@ namespace ts {
330
349
*/
331
350
function endLexicalEnvironment ( ) : Statement [ ] {
332
351
Debug . assert ( ! lexicalEnvironmentDisabled , "Cannot end a lexical environment during the print phase." ) ;
352
+ Debug . assert ( ! lexicalEnvironmentSuspended , "Lexical environment is suspended." ) ;
333
353
334
354
let statements : Statement [ ] ;
335
- if ( hoistedVariableDeclarations || hoistedFunctionDeclarations ) {
336
- if ( hoistedFunctionDeclarations ) {
337
- statements = [ ...hoistedFunctionDeclarations ] ;
355
+ if ( lexicalEnvironmentVariableDeclarations || lexicalEnvironmentFunctionDeclarations ) {
356
+ if ( lexicalEnvironmentFunctionDeclarations ) {
357
+ statements = [ ...lexicalEnvironmentFunctionDeclarations ] ;
338
358
}
339
359
340
- if ( hoistedVariableDeclarations ) {
360
+ if ( lexicalEnvironmentVariableDeclarations ) {
341
361
const statement = createVariableStatement (
342
362
/*modifiers*/ undefined ,
343
- createVariableDeclarationList ( hoistedVariableDeclarations )
363
+ createVariableDeclarationList ( lexicalEnvironmentVariableDeclarations )
344
364
) ;
345
365
346
366
if ( ! statements ) {
@@ -354,8 +374,12 @@ namespace ts {
354
374
355
375
// Restore the previous lexical environment.
356
376
lexicalEnvironmentStackOffset -- ;
357
- hoistedVariableDeclarations = lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
358
- hoistedFunctionDeclarations = lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
377
+ lexicalEnvironmentVariableDeclarations = lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
378
+ lexicalEnvironmentFunctionDeclarations = lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
379
+ if ( lexicalEnvironmentStackOffset === 0 ) {
380
+ lexicalEnvironmentVariableDeclarationsStack = [ ] ;
381
+ lexicalEnvironmentFunctionDeclarationsStack = [ ] ;
382
+ }
359
383
return statements ;
360
384
}
361
385
}
0 commit comments