X Tutup
Skip to content

Commit 08aa778

Browse files
authored
fixed paths in source maps (#859)
* fixed paths in source maps - 'sources' now contain full relative paths to original ts file - 'sourceRoot' is only set if specified explicitly from tsconfig.json fixes #855 * updated based on feedback and fixed incorrect test discovered as a result Co-authored-by: Tom <tomblind@users.noreply.github.com>
1 parent 78780d3 commit 08aa778

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/LuaPrinter.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as lua from "./LuaAST";
66
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
77
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
88
import { EmitHost } from "./transpilation";
9-
import { intersperse, trimExtension } from "./utils";
9+
import { intersperse, trimExtension, normalizeSlashes } from "./utils";
1010

1111
// https://www.lua.org/pil/2.4.html
1212
// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
@@ -87,8 +87,7 @@ export interface PrintResult {
8787

8888
export function createPrinter(printers: Printer[]): Printer {
8989
if (printers.length === 0) {
90-
return (program, emitHost, fileName, ...args) =>
91-
new LuaPrinter(program.getCompilerOptions(), emitHost, fileName).print(...args);
90+
return (program, emitHost, fileName, ...args) => new LuaPrinter(emitHost, program, fileName).print(...args);
9291
} else if (printers.length === 1) {
9392
return printers[0];
9493
} else {
@@ -127,9 +126,26 @@ export class LuaPrinter {
127126

128127
private currentIndent = "";
129128
private sourceFile: string;
129+
private options: CompilerOptions;
130130

131-
public constructor(private options: CompilerOptions, private emitHost: EmitHost, fileName: string) {
132-
this.sourceFile = path.basename(fileName);
131+
public constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) {
132+
this.options = program.getCompilerOptions();
133+
134+
if (this.options.outDir) {
135+
const relativeFileName = path.relative(program.getCommonSourceDirectory(), fileName);
136+
if (this.options.sourceRoot) {
137+
// When sourceRoot is specified, just use relative path inside rootDir
138+
this.sourceFile = relativeFileName;
139+
} else {
140+
// Calculate relative path from rootDir to outDir
141+
const outputPath = path.resolve(this.options.outDir, relativeFileName);
142+
this.sourceFile = path.relative(path.dirname(outputPath), fileName);
143+
}
144+
// We want forward slashes, even in windows
145+
this.sourceFile = normalizeSlashes(this.sourceFile);
146+
} else {
147+
this.sourceFile = path.basename(fileName); // File will be in same dir as source
148+
}
133149
}
134150

135151
public print(block: lua.Block, luaLibFeatures: Set<LuaLibFeature>): PrintResult {
@@ -138,10 +154,10 @@ export class LuaPrinter {
138154
luaLibFeatures.add(LuaLibFeature.SourceMapTraceBack);
139155
}
140156

141-
const sourceRoot =
142-
this.options.sourceRoot ||
143-
(this.options.outDir ? path.relative(this.options.outDir, this.options.rootDir || process.cwd()) : ".");
144-
157+
const sourceRoot = this.options.sourceRoot
158+
? // According to spec, sourceRoot is simply prepended to the source name, so the slash should be included
159+
this.options.sourceRoot.replace(/[\\\/]+$/, "") + "/"
160+
: "";
145161
const rootSourceNode = this.printImplementation(block, luaLibFeatures);
146162
const sourceMap = this.buildSourceMap(sourceRoot, rootSourceNode);
147163

test/transpile/plugins/printer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as tstl from "../../../src";
22

33
const plugin: tstl.Plugin = {
44
printer: (program, emitHost, fileName, ...args) => {
5-
const result = new tstl.LuaPrinter(program.getCompilerOptions(), emitHost, fileName).print(...args);
5+
const result = new tstl.LuaPrinter(emitHost, program, fileName).print(...args);
66
result.code = `-- Plugin\n${result.code}`;
77
return result;
88
},

test/unit/printer/sourcemaps.spec.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,64 @@ test.each([
159159
}
160160
});
161161

162-
test("Source map has correct sources", async () => {
162+
test.each([
163+
{ fileName: "/proj/foo.ts", config: {}, mapSource: "foo.ts", fullSource: "foo.ts" },
164+
{
165+
fileName: "/proj/src/foo.ts",
166+
config: { outDir: "/proj/dst" },
167+
mapSource: "../src/foo.ts",
168+
fullSource: "../src/foo.ts",
169+
},
170+
{
171+
fileName: "/proj/src/foo.ts",
172+
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
173+
mapSource: "../src/foo.ts",
174+
fullSource: "../src/foo.ts",
175+
},
176+
{
177+
fileName: "/proj/src/sub/foo.ts",
178+
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
179+
mapSource: "../../src/sub/foo.ts",
180+
fullSource: "../../src/sub/foo.ts",
181+
},
182+
{
183+
fileName: "/proj/src/sub/main.ts",
184+
config: { rootDir: "/proj/src", outDir: "/proj/dst", sourceRoot: "bin" },
185+
mapSource: "sub/main.ts",
186+
fullSource: "bin/sub/main.ts",
187+
},
188+
])("Source map has correct sources (%p)", async ({ fileName, config, mapSource, fullSource }) => {
163189
const file = util.testModule`
164190
const foo = "foo"
165191
`
166-
.expectToHaveNoDiagnostics()
192+
.setOptions(config)
193+
.setMainFileName(fileName)
167194
.getMainLuaFileResult();
168195

196+
const sourceMap = JSON.parse(file.sourceMap);
197+
expect(sourceMap.sources.length).toBe(1);
198+
expect(sourceMap.sources[0]).toBe(mapSource);
199+
169200
const consumer = await new SourceMapConsumer(file.sourceMap);
170201
expect(consumer.sources.length).toBe(1);
171-
expect(consumer.sources[0]).toBe("main.ts");
202+
expect(consumer.sources[0]).toBe(fullSource);
172203
});
173204

174-
test("Source map has correct source root", async () => {
205+
test.each([
206+
{ configSourceRoot: undefined, mapSourceRoot: "" },
207+
{ configSourceRoot: "src", mapSourceRoot: "src/" },
208+
{ configSourceRoot: "src/", mapSourceRoot: "src/" },
209+
{ configSourceRoot: "src\\", mapSourceRoot: "src/" },
210+
])("Source map has correct source root (%p)", async ({ configSourceRoot, mapSourceRoot }) => {
175211
const file = util.testModule`
176212
const foo = "foo"
177213
`
214+
.setOptions({ sourceMap: true, sourceRoot: configSourceRoot })
178215
.expectToHaveNoDiagnostics()
179216
.getMainLuaFileResult();
180217

181218
const sourceMap = JSON.parse(file.sourceMap);
182-
expect(sourceMap.sourceRoot).toBe(".");
219+
expect(sourceMap.sourceRoot).toBe(mapSourceRoot);
183220
});
184221

185222
test.each([

0 commit comments

Comments
 (0)
X Tutup