@@ -34,6 +34,7 @@ interface Scope {
3434 referencedSymbols ?: Set < tstl . SymbolId > ;
3535 variableDeclarations ?: tstl . VariableDeclarationStatement [ ] ;
3636 functionDefinitions ?: Map < tstl . SymbolId , FunctionDefinitionInfo > ;
37+ importStatements ?: tstl . Statement [ ] ;
3738 loopContinued ?: boolean ;
3839}
3940
@@ -273,9 +274,9 @@ export class LuaTransformer {
273274 statement . moduleSpecifier
274275 ) ;
275276
276- const importResult = this . transformImportDeclaration ( importDeclaration ) ;
277-
278- const result = Array . isArray ( importResult ) ? importResult : [ importResult ] ;
277+ // Wrap in block to prevent imports from hoisting out of `do` statement
278+ const block = ts . createBlock ( [ importDeclaration ] ) ;
279+ const result = this . transformBlock ( block ) . statements ;
279280
280281 // Now the module is imported, add the imports to the export table
281282 for ( const exportVariable of statement . exportClause . elements ) {
@@ -327,13 +328,23 @@ export class LuaTransformer {
327328
328329 const result : tstl . Statement [ ] = [ ] ;
329330
331+ const scope = this . peekScope ( ) ;
332+ if ( ! this . options . noHoisting && ! scope . importStatements ) {
333+ scope . importStatements = [ ] ;
334+ }
335+
330336 const moduleSpecifier = statement . moduleSpecifier as ts . StringLiteral ;
331337 const importPath = moduleSpecifier . text . replace ( new RegExp ( "\"" , "g" ) , "" ) ;
332338
333339 if ( ! statement . importClause ) {
334340 const requireCall = this . createModuleRequire ( statement . moduleSpecifier as ts . StringLiteral ) ;
335341 result . push ( tstl . createExpressionStatement ( requireCall ) ) ;
336- return result ;
342+ if ( scope . importStatements ) {
343+ scope . importStatements . push ( ...result ) ;
344+ return undefined ;
345+ } else {
346+ return result ;
347+ }
337348 }
338349
339350 const imports = statement . importClause . namedBindings ;
@@ -369,30 +380,41 @@ export class LuaTransformer {
369380 if ( importSpecifier . propertyName ) {
370381 const propertyIdentifier = this . transformIdentifier ( importSpecifier . propertyName ) ;
371382 const propertyName = tstl . createStringLiteral ( propertyIdentifier . text ) ;
372- const renamedImport = this . createHoistableVariableDeclarationStatement (
373- importSpecifier . name ,
383+ const renamedImport = tstl . createVariableDeclarationStatement (
384+ this . transformIdentifier ( importSpecifier . name ) ,
374385 tstl . createTableIndexExpression ( importUniqueName , propertyName ) ,
375386 importSpecifier ) ;
376387 result . push ( renamedImport ) ;
377388 } else {
378389 const name = tstl . createStringLiteral ( importSpecifier . name . text ) ;
379- const namedImport = this . createHoistableVariableDeclarationStatement (
380- importSpecifier . name ,
390+ const namedImport = tstl . createVariableDeclarationStatement (
391+ this . transformIdentifier ( importSpecifier . name ) ,
381392 tstl . createTableIndexExpression ( importUniqueName , name ) ,
382393 importSpecifier
383394 ) ;
384395 result . push ( namedImport ) ;
385396 }
386397 } ) ;
387- return result ;
398+ if ( scope . importStatements ) {
399+ scope . importStatements . push ( ...result ) ;
400+ return undefined ;
401+ } else {
402+ return result ;
403+ }
404+
388405 } else if ( ts . isNamespaceImport ( imports ) ) {
389- const requireStatement = this . createHoistableVariableDeclarationStatement (
390- imports . name ,
406+ const requireStatement = tstl . createVariableDeclarationStatement (
407+ this . transformIdentifier ( imports . name ) ,
391408 requireCall ,
392409 statement
393410 ) ;
394411 result . push ( requireStatement ) ;
395- return result ;
412+ if ( scope . importStatements ) {
413+ scope . importStatements . push ( ...result ) ;
414+ return undefined ;
415+ } else {
416+ return result ;
417+ }
396418 }
397419 }
398420
@@ -4882,6 +4904,14 @@ export class LuaTransformer {
48824904 }
48834905 }
48844906
4907+ protected hoistImportStatements ( scope : Scope , statements : tstl . Statement [ ] ) : tstl . Statement [ ] {
4908+ if ( ! scope . importStatements ) {
4909+ return statements ;
4910+ }
4911+
4912+ return [ ...scope . importStatements , ...statements ] ;
4913+ }
4914+
48854915 protected hoistFunctionDefinitions ( scope : Scope , statements : tstl . Statement [ ] ) : tstl . Statement [ ] {
48864916 if ( ! scope . functionDefinitions ) {
48874917 return statements ;
@@ -4952,6 +4982,8 @@ export class LuaTransformer {
49524982
49534983 result = this . hoistVariableDeclarations ( scope , result ) ;
49544984
4985+ result = this . hoistImportStatements ( scope , result ) ;
4986+
49554987 return result ;
49564988 }
49574989
0 commit comments