Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,32 @@ export class LuaPrinter {
}

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

if (!this.options.noHeader) {
header += tstlHeader;
sourceChunks.push(tstlHeader);
}
let statements = file.statements;

const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;
if (luaLibImport === LuaLibImportKind.Require && file.luaLibFeatures.size > 0) {
// Import lualib features
const importStatements = loadImportedLualibFeatures(file.luaLibFeatures, this.emitHost);

statements = importStatements.concat(statements);
sourceChunks = this.printStatementArray(loadImportedLualibFeatures(file.luaLibFeatures, this.emitHost));
} else if (luaLibImport === LuaLibImportKind.Inline && file.luaLibFeatures.size > 0) {
// Inline lualib features
header += "-- Lua Library inline imports\n";
header += loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost);
sourceChunks.push("-- Lua Library inline imports\n");
sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost));
}

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

return this.concatNodes(header, ...this.printStatementArray(statements));
// Print reest of the statements in file
sourceChunks.push(...this.printStatementArray(file.statements));

return this.concatNodes(...sourceChunks);
}

protected pushIndent(): void {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/printer/sourcemaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ test("sourceMapTraceback saves sourcemap in _G", () => {
});

test("sourceMapTraceback gives traceback", () => {
const builder = util.testFunction`
return (debug.traceback as (this: void)=>string)();
`.setOptions({ sourceMapTraceback: true });

const traceback = builder.getLuaExecutionResult();
expect(traceback).toEqual(expect.any(String));
});

test("inline sourceMapTraceback gives traceback", () => {
const builder = util.testFunction`
return (debug.traceback as (this: void)=>string)();
`.setOptions({ sourceMapTraceback: true, luaLibImport: tstl.LuaLibImportKind.Inline });
Expand Down