Skip to content

Commit 1f9be79

Browse files
authored
Made source file paths in sourcemaps relative again (#1140)
* Sourcemap source files are relative to project root * Updated unit test * use getEmitPathRelativeToOutDir * Updated unclear require diagnostic * updated diagnostic snapshot * Change sourcenode paths to relative * fixed once and for all (hopefully) * fix weird auto-import * Make sourcemap file property only the file name, not the entire path
1 parent ae762b6 commit 1f9be79

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/LuaPrinter.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import * as path from "path";
12
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
3+
import { getEmitPath } from ".";
24
import * as ts from "typescript";
35
import { CompilerOptions, isBundleEnabled, LuaLibImportKind } from "./CompilerOptions";
46
import * as lua from "./LuaAST";
57
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
68
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
79
import { EmitHost } from "./transpilation";
8-
import { intersperse, trimExtension } from "./utils";
10+
import { intersperse, normalizeSlashes } from "./utils";
911

1012
// https://www.lua.org/pil/2.4.html
1113
// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
@@ -120,14 +122,17 @@ export class LuaPrinter {
120122
};
121123

122124
private currentIndent = "";
123-
private sourceFile: string;
125+
private luaFile: string;
126+
private relativeSourcePath: string;
124127
private options: CompilerOptions;
125128

126129
public static readonly sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
127130

128-
constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) {
131+
constructor(private emitHost: EmitHost, private program: ts.Program, private sourceFile: string) {
129132
this.options = program.getCompilerOptions();
130-
this.sourceFile = fileName;
133+
this.luaFile = normalizeSlashes(getEmitPath(this.sourceFile, this.program));
134+
// Source nodes contain relative path from mapped lua file to original TS source file
135+
this.relativeSourcePath = normalizeSlashes(path.relative(path.dirname(this.luaFile), this.sourceFile));
131136
}
132137

133138
public print(file: lua.File): PrintResult {
@@ -230,12 +235,12 @@ export class LuaPrinter {
230235
const { line, column } = lua.getOriginalPos(node);
231236

232237
return line !== undefined && column !== undefined
233-
? new SourceNode(line + 1, column, this.sourceFile, chunks, name)
234-
: new SourceNode(null, null, this.sourceFile, chunks, name);
238+
? new SourceNode(line + 1, column, this.relativeSourcePath, chunks, name)
239+
: new SourceNode(null, null, this.relativeSourcePath, chunks, name);
235240
}
236241

237242
protected concatNodes(...chunks: SourceChunk[]): SourceNode {
238-
return new SourceNode(null, null, this.sourceFile, chunks);
243+
return new SourceNode(null, null, this.relativeSourcePath, chunks);
239244
}
240245

241246
protected printBlock(block: lua.Block): SourceNode {
@@ -757,7 +762,7 @@ export class LuaPrinter {
757762
}
758763

759764
public printOperator(kind: lua.Operator): SourceNode {
760-
return new SourceNode(null, null, this.sourceFile, LuaPrinter.operatorMap[kind]);
765+
return new SourceNode(null, null, this.relativeSourcePath, LuaPrinter.operatorMap[kind]);
761766
}
762767

763768
protected joinChunksWithComma(chunks: SourceChunk[]): SourceChunk[] {
@@ -787,7 +792,7 @@ export class LuaPrinter {
787792
// will not generate 'empty' mappings in the source map that point to nothing in the original TS.
788793
private buildSourceMap(sourceRoot: string, rootSourceNode: SourceNode): SourceMapGenerator {
789794
const map = new SourceMapGenerator({
790-
file: trimExtension(this.sourceFile) + ".lua",
795+
file: path.basename(this.luaFile),
791796
sourceRoot,
792797
});
793798

src/transpilation/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const createDiagnosticFactory = <TArgs extends any[]>(
88

99
export const couldNotResolveRequire = createDiagnosticFactory(
1010
(requirePath: string, containingFile: string) =>
11-
`Could not resolve require path '${requirePath}' in file ${containingFile}.`
11+
`Could not resolve lua source files for require path '${requirePath}' in file ${containingFile}.`
1212
);
1313

1414
export const couldNotReadDependency = createDiagnosticFactory(

test/unit/modules/__snapshots__/resolution.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ local ____ = module
77
return ____exports"
88
`;
99

10-
exports[`doesn't resolve paths out of root dir: diagnostics 1`] = `"error TSTL: Could not resolve require path '../module' in file main.ts."`;
10+
exports[`doesn't resolve paths out of root dir: diagnostics 1`] = `"error TSTL: Could not resolve lua source files for require path '../module' in file main.ts."`;

test/unit/printer/sourcemaps.spec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,35 @@ test.each([
163163
});
164164

165165
test.each([
166-
{ fileName: "/proj/foo.ts", config: {} },
166+
{
167+
fileName: "/proj/foo.ts",
168+
config: {},
169+
expectedSourcePath: "foo.ts", // ts and lua will be emitted to same directory
170+
},
167171
{
168172
fileName: "/proj/src/foo.ts",
169-
config: { outDir: "/proj/dst" },
173+
config: {
174+
outDir: "/proj/dst",
175+
},
176+
expectedSourcePath: "../src/foo.ts", // path from proj/dst outDir to proj/src/foo.ts
170177
},
171178
{
172179
fileName: "/proj/src/foo.ts",
173180
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
181+
expectedSourcePath: "../src/foo.ts", // path from proj/dst outDir to proj/src/foo.ts
174182
},
175183
{
176184
fileName: "/proj/src/sub/foo.ts",
177185
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
186+
expectedSourcePath: "../../src/sub/foo.ts", // path from proj/dst/sub outDir to proj/src/sub/foo.ts
178187
},
179188
{
180189
fileName: "/proj/src/sub/main.ts",
181-
config: { rootDir: "/proj/src", outDir: "/proj/dst", sourceRoot: "bin" },
182-
fullSource: "bin/proj/src/sub/main.ts",
190+
config: { rootDir: "/proj/src", outDir: "/proj/dst", sourceRoot: "bin/binsub/binsubsub" },
191+
expectedSourcePath: "../../src/sub/main.ts", // path from proj/dst/sub outDir to proj/src/sub/foo.ts
192+
fullSource: "bin/src/sub/main.ts",
183193
},
184-
])("Source map has correct sources (%p)", async ({ fileName, config, fullSource }) => {
194+
])("Source map has correct sources (%p)", async ({ fileName, config, fullSource, expectedSourcePath }) => {
185195
const file = util.testModule`
186196
const foo = "foo"
187197
`
@@ -191,11 +201,11 @@ test.each([
191201

192202
const sourceMap = JSON.parse(file.luaSourceMap);
193203
expect(sourceMap.sources).toHaveLength(1);
194-
expect(sourceMap.sources[0]).toBe(fileName);
204+
expect(sourceMap.sources[0]).toBe(expectedSourcePath);
195205

196206
const consumer = await new SourceMapConsumer(file.luaSourceMap);
197207
expect(consumer.sources).toHaveLength(1);
198-
expect(consumer.sources[0]).toBe(fullSource ?? fileName);
208+
expect(consumer.sources[0]).toBe(fullSource ?? expectedSourcePath);
199209
});
200210

201211
test.each([

0 commit comments

Comments
 (0)