@@ -6,6 +6,7 @@ import * as lua from "./LuaAST";
66import { loadLuaLibFeatures , LuaLibFeature } from "./LuaLib" ;
77import { isValidLuaIdentifier , luaKeywords } from "./transformation/utils/safe-names" ;
88import { EmitHost } from "./Transpile" ;
9+ import { trimExtension } from "./utils" ;
910
1011// https://www.lua.org/pil/2.4.html
1112// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
@@ -76,27 +77,32 @@ function isSimpleExpression(expression: lua.Expression): boolean {
7677
7778type SourceChunk = string | SourceNode ;
7879
80+ export type Printer = (
81+ program : ts . Program ,
82+ emitHost : EmitHost ,
83+ fileName : string ,
84+ block : lua . Block ,
85+ luaLibFeatures : Set < LuaLibFeature >
86+ ) => PrintResult ;
87+
7988export interface PrintResult {
8089 code : string ;
8190 sourceMap : string ;
8291}
8392
84- export type PrinterFactory = ( program : ts . Program , emitHost : EmitHost ) => Printer ;
85- export type Printer = LuaPrinter [ "print" ] ;
86-
87- export function createPrinter ( program : ts . Program , emitHost : EmitHost , factories : PrinterFactory [ ] ) : Printer {
88- if ( factories . length === 0 ) {
89- const printer = new LuaPrinter ( program . getCompilerOptions ( ) , emitHost ) ;
90- return ( ...args ) => printer . print ( ...args ) ;
91- } else if ( factories . length === 1 ) {
92- return factories [ 0 ] ( program , emitHost ) ;
93+ export function createPrinter ( printers : Printer [ ] ) : Printer {
94+ if ( printers . length === 0 ) {
95+ return ( program , emitHost , fileName , ...args ) =>
96+ new LuaPrinter ( program . getCompilerOptions ( ) , emitHost , fileName ) . print ( ...args ) ;
97+ } else if ( printers . length === 1 ) {
98+ return printers [ 0 ] ;
9399 } else {
94100 throw new Error ( "Only one plugin can specify 'createPrinter'" ) ;
95101 }
96102}
97103
98104export class LuaPrinter {
99- private static operatorMap : { [ key in lua . Operator ] : string } = {
105+ private static operatorMap : Record < lua . Operator , string > = {
100106 [ lua . SyntaxKind . AdditionOperator ] : "+" ,
101107 [ lua . SyntaxKind . SubtractionOperator ] : "-" ,
102108 [ lua . SyntaxKind . MultiplicationOperator ] : "*" ,
@@ -125,11 +131,13 @@ export class LuaPrinter {
125131 } ;
126132
127133 private currentIndent = "" ;
128- private sourceFile ! : string ;
134+ private sourceFile : string ;
129135
130- public constructor ( private options : CompilerOptions , private emitHost : EmitHost ) { }
136+ public constructor ( private options : CompilerOptions , private emitHost : EmitHost , fileName : string ) {
137+ this . sourceFile = path . basename ( fileName ) ;
138+ }
131139
132- public print ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > , fileName : string ) : PrintResult {
140+ public print ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > ) : PrintResult {
133141 // Add traceback lualib if sourcemap traceback option is enabled
134142 if ( this . options . sourceMapTraceback ) {
135143 luaLibFeatures . add ( LuaLibFeature . SourceMapTraceBack ) ;
@@ -139,8 +147,8 @@ export class LuaPrinter {
139147 this . options . sourceRoot ||
140148 ( this . options . outDir ? path . relative ( this . options . outDir , this . options . rootDir || process . cwd ( ) ) : "." ) ;
141149
142- const rootSourceNode = this . printImplementation ( block , luaLibFeatures , fileName ) ;
143- const sourceMap = this . buildSourceMap ( fileName , sourceRoot , rootSourceNode ) ;
150+ const rootSourceNode = this . printImplementation ( block , luaLibFeatures ) ;
151+ const sourceMap = this . buildSourceMap ( sourceRoot , rootSourceNode ) ;
144152
145153 let code = rootSourceNode . toString ( ) ;
146154
@@ -187,7 +195,7 @@ export class LuaPrinter {
187195 return `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${ mapString } );` ;
188196 }
189197
190- private printImplementation ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > , sourceFile : string ) : SourceNode {
198+ private printImplementation ( block : lua . Block , luaLibFeatures : Set < LuaLibFeature > ) : SourceNode {
191199 let header = "" ;
192200
193201 if ( ! this . options . noHeader ) {
@@ -208,8 +216,6 @@ export class LuaPrinter {
208216 header += loadLuaLibFeatures ( luaLibFeatures , this . emitHost ) ;
209217 }
210218
211- this . sourceFile = path . basename ( sourceFile ) ;
212-
213219 if ( this . options . sourceMapTraceback ) {
214220 header += "{#SourceMapTraceback}\n" ;
215221 }
@@ -796,9 +802,9 @@ export class LuaPrinter {
796802
797803 // The key difference between this and SourceNode.toStringWithSourceMap() is that SourceNodes with null line/column
798804 // will not generate 'empty' mappings in the source map that point to nothing in the original TS.
799- private buildSourceMap ( sourceFile : string , sourceRoot : string , rootSourceNode : SourceNode ) : SourceMapGenerator {
805+ private buildSourceMap ( sourceRoot : string , rootSourceNode : SourceNode ) : SourceMapGenerator {
800806 const map = new SourceMapGenerator ( {
801- file : path . basename ( sourceFile , path . extname ( sourceFile ) ) + ".lua" ,
807+ file : trimExtension ( this . sourceFile ) + ".lua" ,
802808 sourceRoot,
803809 } ) ;
804810
0 commit comments