Skip to content

Commit b45f93d

Browse files
ChouUnclaude
andcommitted
test: add regression test for anonymous function traceback mapping (#1665)
Nested IIFEs produce <file.lua:N> notation in debug.traceback(). The old pattern (%S+) captured the "<" prefix, causing sourcemap lookup to fail for anonymous function definition locations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4bc32b7 commit b45f93d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/unit/error.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,47 @@ test("still works without debug module", () => {
345345
});
346346
});
347347

348+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1665
349+
test("sourceMapTraceback maps anonymous function locations in .lua files (#1665)", () => {
350+
// Nested IIFEs produce <file.lua:N> anonymous function notation in traceback.
351+
// Old pattern (%S+)%.lua:(%d+) captures "<main" from "<main.lua:4>",
352+
// 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)
360+
const fakeTraceback = [
361+
"stack traceback:",
362+
"\tmain.lua:5: in function <main.lua:4>",
363+
"\tmain.lua:6: in function <main.lua:3>",
364+
"\t[C]: in ?",
365+
].join("\n");
366+
367+
const result = util.testFunction`
368+
return (() => {
369+
return (() => {
370+
return debug.traceback();
371+
})();
372+
})();
373+
`
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
386+
expect(result).not.toContain("main.lua");
387+
});
388+
348389
util.testEachVersion(
349390
"error stacktrace omits constructor and __TS_New",
350391
() => util.testFunction`

0 commit comments

Comments
 (0)