@@ -6,7 +6,7 @@ import * as lua from "./LuaAST";
66import { loadLuaLibFeatures , LuaLibFeature } from "./LuaLib" ;
77import { isValidLuaIdentifier } from "./transformation/utils/safe-names" ;
88import { EmitHost } from "./transpilation" ;
9- import { intersperse , trimExtension , normalizeSlashes } from "./utils" ;
9+ import { intersperse , normalizeSlashes , trimExtension } from "./utils" ;
1010
1111// https://www.lua.org/pil/2.4.html
1212// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
@@ -71,13 +71,7 @@ function isSimpleExpression(expression: lua.Expression): boolean {
7171
7272type SourceChunk = string | SourceNode ;
7373
74- export type Printer = (
75- program : ts . Program ,
76- emitHost : EmitHost ,
77- fileName : string ,
78- block : lua . Block ,
79- luaLibFeatures : Set < LuaLibFeature >
80- ) => PrintResult ;
74+ export type Printer = ( program : ts . Program , emitHost : EmitHost , fileName : string , file : lua . File ) => PrintResult ;
8175
8276export interface PrintResult {
8377 code : string ;
@@ -87,7 +81,7 @@ export interface PrintResult {
8781
8882export function createPrinter ( printers : Printer [ ] ) : Printer {
8983 if ( printers . length === 0 ) {
90- return ( program , emitHost , fileName , ... args ) => new LuaPrinter ( emitHost , program , fileName ) . print ( ... args ) ;
84+ return ( program , emitHost , fileName , file ) => new LuaPrinter ( emitHost , program , fileName ) . print ( file ) ;
9185 } else if ( printers . length === 1 ) {
9286 return printers [ 0 ] ;
9387 } else {
@@ -148,17 +142,17 @@ export class LuaPrinter {
148142 }
149143 }
150144
151- public print ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > ) : PrintResult {
145+ public print ( file : lua . File ) : PrintResult {
152146 // Add traceback lualib if sourcemap traceback option is enabled
153147 if ( this . options . sourceMapTraceback ) {
154- luaLibFeatures . add ( LuaLibFeature . SourceMapTraceBack ) ;
148+ file . luaLibFeatures . add ( LuaLibFeature . SourceMapTraceBack ) ;
155149 }
156150
157151 const sourceRoot = this . options . sourceRoot
158152 ? // According to spec, sourceRoot is simply prepended to the source name, so the slash should be included
159153 this . options . sourceRoot . replace ( / [ \\ / ] + $ / , "" ) + "/"
160154 : "" ;
161- const rootSourceNode = this . printImplementation ( block , luaLibFeatures ) ;
155+ const rootSourceNode = this . printFile ( file ) ;
162156 const sourceMap = this . buildSourceMap ( sourceRoot , rootSourceNode ) ;
163157
164158 let code = rootSourceNode . toString ( ) ;
@@ -203,8 +197,8 @@ export class LuaPrinter {
203197 return `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${ mapString } );` ;
204198 }
205199
206- private printImplementation ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > ) : SourceNode {
207- let header = "" ;
200+ private printFile ( file : lua . File ) : SourceNode {
201+ let header = file . trivia ;
208202
209203 if ( ! this . options . noHeader ) {
210204 header += "--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n" ;
@@ -213,23 +207,21 @@ export class LuaPrinter {
213207 const luaLibImport = this . options . luaLibImport ?? LuaLibImportKind . Require ;
214208 if (
215209 luaLibImport === LuaLibImportKind . Always ||
216- ( luaLibImport === LuaLibImportKind . Require && luaLibFeatures . size > 0 )
210+ ( luaLibImport === LuaLibImportKind . Require && file . luaLibFeatures . size > 0 )
217211 ) {
218212 // Require lualib bundle
219213 header += 'require("lualib_bundle");\n' ;
220- } else if ( luaLibImport === LuaLibImportKind . Inline && luaLibFeatures . size > 0 ) {
214+ } else if ( luaLibImport === LuaLibImportKind . Inline && file . luaLibFeatures . size > 0 ) {
221215 // Inline lualib features
222216 header += "-- Lua Library inline imports\n" ;
223- header += loadLuaLibFeatures ( luaLibFeatures , this . emitHost ) ;
217+ header += loadLuaLibFeatures ( file . luaLibFeatures , this . emitHost ) ;
224218 }
225219
226220 if ( this . options . sourceMapTraceback ) {
227221 header += "{#SourceMapTraceback}\n" ;
228222 }
229223
230- const fileBlockNode = this . printBlock ( block ) ;
231-
232- return this . concatNodes ( header , fileBlockNode ) ;
224+ return this . concatNodes ( header , ...this . printStatementArray ( file . statements ) ) ;
233225 }
234226
235227 protected pushIndent ( ) : void {
0 commit comments