@@ -84,6 +84,8 @@ namespace ts {
8484 let lexicalEnvironmentFunctionDeclarations : FunctionDeclaration [ ] ;
8585 let lexicalEnvironmentVariableDeclarationsStack : VariableDeclaration [ ] [ ] = [ ] ;
8686 let lexicalEnvironmentFunctionDeclarationsStack : FunctionDeclaration [ ] [ ] = [ ] ;
87+ let lexicalEnvironmentScopingStack : LexicalEnvironmentScoping [ ] = [ ] ;
88+ let lexicalEnvironmentScoping : LexicalEnvironmentScoping ;
8789 let lexicalEnvironmentStackOffset = 0 ;
8890 let lexicalEnvironmentSuspended = false ;
8991 let emitHelpers : EmitHelper [ ] | undefined ;
@@ -258,7 +260,7 @@ namespace ts {
258260 * Starts a new lexical environment. Any existing hoisted variable or function declarations
259261 * are pushed onto a stack, and the related storage variables are reset.
260262 */
261- function startLexicalEnvironment ( ) : void {
263+ function startLexicalEnvironment ( scoping : LexicalEnvironmentScoping = LexicalEnvironmentScoping . Function ) : void {
262264 Debug . assert ( state > TransformationState . Uninitialized , "Cannot modify the lexical environment during initialization." ) ;
263265 Debug . assert ( state < TransformationState . Completed , "Cannot modify the lexical environment after transformation has completed." ) ;
264266 Debug . assert ( ! lexicalEnvironmentSuspended , "Lexical environment is suspended." ) ;
@@ -267,11 +269,13 @@ namespace ts {
267269 // stack size variable. This allows us to reuse existing array slots we've
268270 // already allocated between transformations to avoid allocation and GC overhead during
269271 // transformation.
272+ lexicalEnvironmentScopingStack [ lexicalEnvironmentStackOffset ] = lexicalEnvironmentScoping ;
270273 lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] = lexicalEnvironmentVariableDeclarations ;
271274 lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] = lexicalEnvironmentFunctionDeclarations ;
272275 lexicalEnvironmentStackOffset ++ ;
273276 lexicalEnvironmentVariableDeclarations = undefined ! ;
274277 lexicalEnvironmentFunctionDeclarations = undefined ! ;
278+ lexicalEnvironmentScoping = scoping ;
275279 }
276280
277281 /** Suspends the current lexical environment, usually after visiting a parameter list. */
@@ -308,7 +312,10 @@ namespace ts {
308312 if ( lexicalEnvironmentVariableDeclarations ) {
309313 const statement = createVariableStatement (
310314 /*modifiers*/ undefined ,
311- createVariableDeclarationList ( lexicalEnvironmentVariableDeclarations )
315+ createVariableDeclarationList (
316+ lexicalEnvironmentVariableDeclarations ,
317+ lexicalEnvironmentScoping === LexicalEnvironmentScoping . Block ? NodeFlags . Let : undefined
318+ )
312319 ) ;
313320
314321 if ( ! statements ) {
@@ -324,9 +331,11 @@ namespace ts {
324331 lexicalEnvironmentStackOffset -- ;
325332 lexicalEnvironmentVariableDeclarations = lexicalEnvironmentVariableDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
326333 lexicalEnvironmentFunctionDeclarations = lexicalEnvironmentFunctionDeclarationsStack [ lexicalEnvironmentStackOffset ] ;
334+ lexicalEnvironmentScoping = lexicalEnvironmentScopingStack [ lexicalEnvironmentStackOffset ] ;
327335 if ( lexicalEnvironmentStackOffset === 0 ) {
328336 lexicalEnvironmentVariableDeclarationsStack = [ ] ;
329337 lexicalEnvironmentFunctionDeclarationsStack = [ ] ;
338+ lexicalEnvironmentScopingStack = [ ] ;
330339 }
331340 return statements ;
332341 }
@@ -358,6 +367,7 @@ namespace ts {
358367 lexicalEnvironmentVariableDeclarationsStack = undefined ! ;
359368 lexicalEnvironmentFunctionDeclarations = undefined ! ;
360369 lexicalEnvironmentFunctionDeclarationsStack = undefined ! ;
370+ lexicalEnvironmentScopingStack = undefined ! ;
361371 onSubstituteNode = undefined ! ;
362372 onEmitNode = undefined ! ;
363373 emitHelpers = undefined ;
0 commit comments