@@ -350,40 +350,47 @@ test("sourceMapTraceback maps anonymous function locations in .lua files (#1665)
350350 // Nested IIFEs produce <file.lua:N> anonymous function notation in traceback.
351351 // Old pattern (%S+)%.lua:(%d+) captures "<main" from "<main.lua:4>",
352352 // failing the sourcemap lookup. Fix: ([^%s<]+) excludes "<".
353- //
354- // Compiled Lua for the nested IIFEs below:
355- // line 3: (function()
356- // line 4: (function()
357- // line 5: print(debug.traceback())
358- // line 6: end)(nil)
359- // line 7: end)(nil)
353+
354+ // mapping is copied from the emitted Lua, not invented.
355+ const mapping = `{["5"] = 1,["6"] = 2,["7"] = 3,["8"] = 4,["9"] = 3,["10"] = 2,["11"] = 1}` ;
356+
357+ // Test harness executes via luaL_dostring (chunk names are [string "..."]), so we mock a file-based traceback.
360358 const fakeTraceback = [
361359 "stack traceback:" ,
362- "\tmain.lua:5 : in function <main.lua:4 >" ,
363- "\tmain.lua:6 : in function <main.lua:3 >" ,
360+ "\tmain.lua:8 : in function <main.lua:7 >" ,
361+ "\tmain.lua:7 : in function <main.lua:6 >" ,
364362 "\t[C]: in ?" ,
365363 ] . join ( "\n" ) ;
366364
367- const result = util . testFunction `
365+ const builder = util . testFunction `
368366 return (() => {
369367 return (() => {
370- return debug.traceback();
368+ return ( debug.traceback as (this: void) => string) ();
371369 })();
372370 })();
373371 `
374- . setLuaHeader (
375- `__TS__sourcemap = { ["main.lua"] = {["3"] = 7, ["4"] = 8, ["5"] = 9, ["6"] = 8} }\n` +
376- `local __real_tb = debug.traceback\n` +
377- `debug.traceback = function() return ${ JSON . stringify ( fakeTraceback ) } end`
378- )
379- . setOptions ( { sourceMapTraceback : true } )
380- . getLuaExecutionResult ( ) ;
381-
382- // Regular line references should be mapped
383- expect ( result ) . toContain ( "main.ts:9" ) ;
384- expect ( result ) . toContain ( "main.ts:8" ) ;
385- // Anonymous function definitions <main.lua:4> and <main.lua:3> should also be mapped
372+ // Inject sourcemap for "main.lua" and mock debug.traceback to return file-based frames.
373+ . setLuaHeader ( `
374+ __TS__sourcemap = { ["main.lua"] = ${ mapping } };
375+ local __real_tb = debug.traceback
376+ debug.traceback = function() return ${ JSON . stringify ( fakeTraceback ) } end
377+ ` )
378+ . setOptions ( { sourceMapTraceback : true } ) ;
379+
380+ const lua = builder . getMainLuaCodeChunk ( ) ;
381+ // Sanity check: emitted code registers the same mapping literal we inject above.
382+ expect ( lua ) . toContain ( `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${ mapping } );` ) ;
383+
384+ const result = builder . getLuaExecutionResult ( ) ;
385+ expect ( result ) . toEqual ( expect . any ( String ) ) ;
386+ // Both `main.lua:N` and `<main.lua:N>` frames should be rewritten using the sourcemap.
386387 expect ( result ) . not . toContain ( "main.lua" ) ;
388+ // Regular line references
389+ expect ( result ) . toContain ( "\tmain.ts:4:" ) ;
390+ expect ( result ) . toContain ( "\tmain.ts:3:" ) ;
391+ // Anonymous function references must keep <> format
392+ expect ( result ) . toContain ( "<main.ts:3>" ) ;
393+ expect ( result ) . toContain ( "<main.ts:2>" ) ;
387394} ) ;
388395
389396util . testEachVersion (
0 commit comments