Skip to content

Commit 304c203

Browse files
authored
Fix sourcemap traceback being in the wrong place (#1238)
1 parent f742949 commit 304c203

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/LuaPrinter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,32 +227,32 @@ export class LuaPrinter {
227227
}
228228

229229
protected printFile(file: lua.File): SourceNode {
230-
let header = file.trivia;
230+
let sourceChunks: SourceChunk[] = [file.trivia];
231231

232232
if (!this.options.noHeader) {
233-
header += tstlHeader;
233+
sourceChunks.push(tstlHeader);
234234
}
235-
let statements = file.statements;
236235

237236
const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;
238237
if (luaLibImport === LuaLibImportKind.Require && file.luaLibFeatures.size > 0) {
239238
// Import lualib features
240-
const importStatements = loadImportedLualibFeatures(file.luaLibFeatures, this.emitHost);
241-
242-
statements = importStatements.concat(statements);
239+
sourceChunks = this.printStatementArray(loadImportedLualibFeatures(file.luaLibFeatures, this.emitHost));
243240
} else if (luaLibImport === LuaLibImportKind.Inline && file.luaLibFeatures.size > 0) {
244241
// Inline lualib features
245-
header += "-- Lua Library inline imports\n";
246-
header += loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost);
242+
sourceChunks.push("-- Lua Library inline imports\n");
243+
sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost));
247244
}
248245

249246
if (this.options.sourceMapTraceback && !isBundleEnabled(this.options)) {
250247
// In bundle mode the traceback is being generated for the entire file in getBundleResult
251248
// Otherwise, traceback is being generated locally
252-
header += `${LuaPrinter.sourceMapTracebackPlaceholder}\n`;
249+
sourceChunks.push(`${LuaPrinter.sourceMapTracebackPlaceholder}\n`);
253250
}
254251

255-
return this.concatNodes(header, ...this.printStatementArray(statements));
252+
// Print reest of the statements in file
253+
sourceChunks.push(...this.printStatementArray(file.statements));
254+
255+
return this.concatNodes(...sourceChunks);
256256
}
257257

258258
protected pushIndent(): void {

test/unit/printer/sourcemaps.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ test("sourceMapTraceback saves sourcemap in _G", () => {
284284
});
285285

286286
test("sourceMapTraceback gives traceback", () => {
287+
const builder = util.testFunction`
288+
return (debug.traceback as (this: void)=>string)();
289+
`.setOptions({ sourceMapTraceback: true });
290+
291+
const traceback = builder.getLuaExecutionResult();
292+
expect(traceback).toEqual(expect.any(String));
293+
});
294+
295+
test("inline sourceMapTraceback gives traceback", () => {
287296
const builder = util.testFunction`
288297
return (debug.traceback as (this: void)=>string)();
289298
`.setOptions({ sourceMapTraceback: true, luaLibImport: tstl.LuaLibImportKind.Inline });

0 commit comments

Comments
 (0)