GeezerVison is showing github.com — Made source file paths in sourcemaps relative again (#1140) · TypeScriptToLua/TypeScriptToLua@1f9be79
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/LuaPrinter.ts +14-9Lines changed: 14 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as path from "path"; | 1 | 2 | import { Mapping, SourceMapGenerator, SourceNode } from "source-map"; | | 3 | +import { getEmitPath } from "."; | 2 | 4 | import * as ts from "typescript"; | 3 | 5 | import { CompilerOptions, isBundleEnabled, LuaLibImportKind } from "./CompilerOptions"; | 4 | 6 | import * as lua from "./LuaAST"; | 5 | 7 | import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib"; | 6 | 8 | import { isValidLuaIdentifier } from "./transformation/utils/safe-names"; | 7 | 9 | import { EmitHost } from "./transpilation"; | 8 | | -import { intersperse, trimExtension } from "./utils"; | | 10 | +import { intersperse, normalizeSlashes } from "./utils"; | 9 | 11 |
| 10 | 12 | // https://www.lua.org/pil/2.4.html | 11 | 13 | // https://www.ecma-international.org/ecma-262/10.0/index.html#table-34 | @@ -120,14 +122,17 @@ export class LuaPrinter { | 120 | 122 | }; | 121 | 123 |
| 122 | 124 | private currentIndent = ""; | 123 | | - private sourceFile: string; | | 125 | + private luaFile: string; | | 126 | + private relativeSourcePath: string; | 124 | 127 | private options: CompilerOptions; | 125 | 128 |
| 126 | 129 | public static readonly sourceMapTracebackPlaceholder = "{#SourceMapTraceback}"; | 127 | 130 |
| 128 | | - constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) { | | 131 | + constructor(private emitHost: EmitHost, private program: ts.Program, private sourceFile: string) { | 129 | 132 | 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)); | 131 | 136 | } | 132 | 137 |
| 133 | 138 | public print(file: lua.File): PrintResult { | @@ -230,12 +235,12 @@ export class LuaPrinter { | 230 | 235 | const { line, column } = lua.getOriginalPos(node); | 231 | 236 |
| 232 | 237 | 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); | 235 | 240 | } | 236 | 241 |
| 237 | 242 | protected concatNodes(...chunks: SourceChunk[]): SourceNode { | 238 | | - return new SourceNode(null, null, this.sourceFile, chunks); | | 243 | + return new SourceNode(null, null, this.relativeSourcePath, chunks); | 239 | 244 | } | 240 | 245 |
| 241 | 246 | protected printBlock(block: lua.Block): SourceNode { | @@ -757,7 +762,7 @@ export class LuaPrinter { | 757 | 762 | } | 758 | 763 |
| 759 | 764 | 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]); | 761 | 766 | } | 762 | 767 |
| 763 | 768 | protected joinChunksWithComma(chunks: SourceChunk[]): SourceChunk[] { | @@ -787,7 +792,7 @@ export class LuaPrinter { | 787 | 792 | // will not generate 'empty' mappings in the source map that point to nothing in the original TS. | 788 | 793 | private buildSourceMap(sourceRoot: string, rootSourceNode: SourceNode): SourceMapGenerator { | 789 | 794 | const map = new SourceMapGenerator({ | 790 | | - file: trimExtension(this.sourceFile) + ".lua", | | 795 | + file: path.basename(this.luaFile), | 791 | 796 | sourceRoot, | 792 | 797 | }); | 793 | 798 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/transpilation/diagnostics.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,7 @@ const createDiagnosticFactory = <TArgs extends any[]>( | 8 | 8 |
| 9 | 9 | export const couldNotResolveRequire = createDiagnosticFactory( | 10 | 10 | (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}.` | 12 | 12 | ); | 13 | 13 |
| 14 | 14 | export const couldNotReadDependency = createDiagnosticFactory( | |
Collapse file Copy file name to clipboardExpand all lines: test/unit/modules/__snapshots__/resolution.spec.ts.snap +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,4 +7,4 @@ local ____ = module | 7 | 7 | return ____exports" | 8 | 8 | `; | 9 | 9 |
| 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."`; |
Collapse file Copy file name to clipboardExpand all lines: test/unit/printer/sourcemaps.spec.ts +17-7Lines changed: 17 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -163,25 +163,35 @@ test.each([ | 163 | 163 | }); | 164 | 164 |
| 165 | 165 | 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 | + }, | 167 | 171 | { | 168 | 172 | 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 | 170 | 177 | }, | 171 | 178 | { | 172 | 179 | fileName: "/proj/src/foo.ts", | 173 | 180 | config: { rootDir: "/proj/src", outDir: "/proj/dst" }, | | 181 | + expectedSourcePath: "../src/foo.ts", // path from proj/dst outDir to proj/src/foo.ts | 174 | 182 | }, | 175 | 183 | { | 176 | 184 | fileName: "/proj/src/sub/foo.ts", | 177 | 185 | 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 | 178 | 187 | }, | 179 | 188 | { | 180 | 189 | 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", | 183 | 193 | }, | 184 | | -])("Source map has correct sources (%p)", async ({ fileName, config, fullSource }) => { | | 194 | +])("Source map has correct sources (%p)", async ({ fileName, config, fullSource, expectedSourcePath }) => { | 185 | 195 | const file = util.testModule` | 186 | 196 | const foo = "foo" | 187 | 197 | ` | @@ -191,11 +201,11 @@ test.each([ | 191 | 201 |
| 192 | 202 | const sourceMap = JSON.parse(file.luaSourceMap); | 193 | 203 | expect(sourceMap.sources).toHaveLength(1); | 194 | | - expect(sourceMap.sources[0]).toBe(fileName); | | 204 | + expect(sourceMap.sources[0]).toBe(expectedSourcePath); | 195 | 205 |
| 196 | 206 | const consumer = await new SourceMapConsumer(file.luaSourceMap); | 197 | 207 | expect(consumer.sources).toHaveLength(1); | 198 | | - expect(consumer.sources[0]).toBe(fullSource ?? fileName); | | 208 | + expect(consumer.sources[0]).toBe(fullSource ?? expectedSourcePath); | 199 | 209 | }); | 200 | 210 |
| 201 | 211 | test.each([ | |
|