@@ -50,50 +50,45 @@ export class LuaPrinter {
5050 this . currentIndent = "" ;
5151 }
5252
53- public print ( block : tstl . Block , luaLibFeatures ?: Set < LuaLibFeature > , sourceFile ?: string ) : string {
54- if ( this . options . inlineSourceMap === true ) {
55- const rootSourceNode = this . printImplementation ( block , luaLibFeatures , sourceFile ) ;
53+ public print ( block : tstl . Block , luaLibFeatures ?: Set < LuaLibFeature > , sourceFile ?: string ) : [ string , string ] {
54+ // Add traceback lualib if sourcemap traceback option is enabled
55+ if ( this . options . sourceMapTraceback ) {
56+ if ( luaLibFeatures === undefined ) {
57+ luaLibFeatures = new Set ( ) ;
58+ }
59+ luaLibFeatures . add ( LuaLibFeature . SourceMapTraceBack ) ;
60+ }
5661
57- const codeWithMap = rootSourceNode
58- // TODO is the file: part really required? and should this be handled in the printer?
59- . toStringWithSourceMap ( { file : path . basename ( sourceFile , path . extname ( sourceFile ) ) + ".lua" } ) ;
62+ const rootSourceNode = this . printImplementation ( block , luaLibFeatures , sourceFile ) ;
6063
61- let inlineSourceMap = this . printInlineSourceMap ( codeWithMap . map ) ;
64+ const codeWithSourceMap = rootSourceNode
65+ // TODO is the file: part really required? and should this be handled in the printer?
66+ . toStringWithSourceMap ( { file : path . basename ( sourceFile , path . extname ( sourceFile ) ) + ".lua" } ) ;
6267
63- // TODO: Put this behind a compiler option?
64- const stackTraceOverride = this . printStackTraceOverride ( rootSourceNode ) ;
65- inlineSourceMap = stackTraceOverride + inlineSourceMap ;
68+ let codeResult = codeWithSourceMap . code ;
6669
67- return codeWithMap . code + "\n" + inlineSourceMap ;
68- } else {
69- return this . printImplementation ( block , luaLibFeatures , sourceFile ) . toString ( ) ;
70+ if ( this . options . inlineSourceMap ) {
71+ codeResult += "\n" + this . printInlineSourceMap ( codeWithSourceMap . map ) ;
7072 }
71- }
72-
73- public printWithSourceMap (
74- block : tstl . Block ,
75- luaLibFeatures ?: Set < LuaLibFeature > ,
76- sourceFile ?: string ) : [ string , string ] {
77-
78- const codeWithMap =
79- this . printImplementation ( block , luaLibFeatures , sourceFile )
80- // TODO is the file: part really required? and should this be handled in the printer?
81- . toStringWithSourceMap ( { file : path . basename ( sourceFile , path . extname ( sourceFile ) ) + ".lua" } ) ;
8273
74+ if ( this . options . sourceMapTraceback ) {
75+ const stackTraceOverride = this . printStackTraceOverride ( rootSourceNode ) ;
76+ codeResult = codeResult . replace ( "{#SourceMapTraceback}" , stackTraceOverride ) ;
77+ }
8378
84- return [ codeWithMap . code , codeWithMap . map . toString ( ) ] ;
79+ return [ codeResult , codeWithSourceMap . map . toString ( ) ] ;
8580 }
8681
8782 private printInlineSourceMap ( sourceMap : SourceMapGenerator ) : string {
8883 const map = sourceMap . toString ( ) ;
8984 const base64Map = Buffer . from ( map ) . toString ( 'base64' ) ;
9085
91- return " //# sourceMappingURL=data:application/json;base64," + base64Map ;
86+ return ` //# sourceMappingURL=data:application/json;base64,${ base64Map } \n` ;
9287 }
9388
9489 private printStackTraceOverride ( rootNode : SourceNode ) : string {
9590 let line = 1 ;
96- const map = { } ;
91+ const map : { [ line : number ] : number } = { } ;
9792 rootNode . walk ( ( chunk , mappedPosition ) => {
9893 if ( mappedPosition . line !== undefined && mappedPosition . line > 0 ) {
9994 if ( map [ line ] === undefined ) {
@@ -104,8 +99,15 @@ export class LuaPrinter {
10499 }
105100 line += chunk . split ( "\n" ) . length - 1 ;
106101 } ) ;
107- console . log ( map ) ;
108- return "" ;
102+
103+ const mapItems = [ ] ;
104+ for ( const lineNr in map ) {
105+ mapItems . push ( `["${ lineNr } "] = ${ map [ lineNr ] } ` ) ;
106+ }
107+
108+ const mapString = "{" + mapItems . join ( "," ) + "}" ;
109+
110+ return `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${ mapString } );` ;
109111 }
110112
111113 private printImplementation (
@@ -136,9 +138,13 @@ export class LuaPrinter {
136138
137139 this . sourceFile = path . basename ( sourceFile ) ;
138140
139- const blockNode = this . createSourceNode ( block , this . printBlock ( block ) ) ;
141+ if ( this . options . sourceMapTraceback ) {
142+ header += "{#SourceMapTraceback}\n" ;
143+ }
144+
145+ const fileBlockNode = this . createSourceNode ( block , this . printBlock ( block ) ) ;
140146
141- return this . concatNodes ( header , blockNode ) ;
147+ return this . concatNodes ( header , fileBlockNode ) ;
142148 }
143149
144150 private pushIndent ( ) : void {
0 commit comments