Skip to content

Commit b6fce8d

Browse files
authored
Fix bug outputting files to the wrong location in some cases (#1541)
1 parent 91c05b5 commit b6fce8d

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/transpilation/transpiler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ export function getSourceDir(program: ts.Program): string {
203203
if (rootDir && rootDir.length > 0) {
204204
return path.isAbsolute(rootDir) ? rootDir : path.resolve(getProjectRoot(program), rootDir);
205205
}
206-
return program.getCommonSourceDirectory();
206+
207+
// If no rootDir is given, source is relative to the project root
208+
return getProjectRoot(program);
207209
}
208210

209211
export function getEmitOutDir(program: ts.Program): string {

test/transpile/__snapshots__/module-resolution.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ exports[`supports complicated paths configuration 1`] = `
1010

1111
exports[`supports paths configuration 1`] = `
1212
[
13+
"/paths-simple/myprogram/dist/main.lua",
1314
"/paths-simple/myprogram/dist/mypackage/bar.lua",
1415
"/paths-simple/myprogram/dist/mypackage/index.lua",
15-
"/paths-simple/myprogram/dist/myprogram/main.lua",
1616
]
1717
`;

test/transpile/paths.spec.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { getSourceDir } from "../../src";
3+
import { getEmitPath, getSourceDir } from "../../src";
44
import * as util from "../util";
55

66
const cwd = process.cwd();
@@ -12,31 +12,24 @@ describe("getSourceDir", () => {
1212
test("with rootDir", () => {
1313
const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath, rootDir: "src" });
1414

15-
// getCommonSourceDirectory does not work right so mock it
16-
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);
17-
15+
// If rootdir is specified, rootDir is the sourceDir
1816
expect(getSourceDir(program)).toBe(path.join(cwd, "src"));
1917
});
2018

2119
test("without rootDir", () => {
2220
const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath });
2321

24-
// getCommonSourceDirectory does not work right so mock it
25-
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);
26-
27-
// Common sources directory is project root
22+
// If rootDir is not specified, root dir is where the config file is
2823
expect(normalize(getSourceDir(program))).toBe(cwd);
2924
});
3025

31-
test("without rootDir in src dir", () => {
32-
const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], {
33-
configFilePath,
34-
});
26+
test("without config file in src dir", () => {
27+
const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], {});
3528

3629
// getCommonSourceDirectory does not work right so mock it
3730
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(path.join(cwd, "src"));
3831

39-
// Common sources directory is src
32+
// If there is no config file, return the common source directory
4033
expect(normalize(getSourceDir(program))).toBe(path.join(cwd, "src"));
4134
});
4235
});
@@ -145,6 +138,21 @@ describe("getEmitPath", () => {
145138
expect(fileNames).toHaveLength(1);
146139
expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.scar"));
147140
});
141+
142+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1540
143+
test("puts files next to their source if no config is given (#1540)", () => {
144+
const file1 = path.join("src", "main.ts");
145+
const file2 = path.join("src", "otherfile.ts");
146+
const file3 = path.join("src", "nested", "nestedfile.ts");
147+
const program = ts.createProgram([file1, file2, file3], { configFilePath });
148+
149+
// If rootDir is not specified, root dir is where the config file is
150+
const configRoot = path.dirname(configFilePath);
151+
const replaceExtension = (f: string) => f.replace(/\.ts$/, ".lua");
152+
expect(getEmitPath(file1, program)).toBe(replaceExtension(path.join(configRoot, file1)));
153+
expect(getEmitPath(file2, program)).toBe(replaceExtension(path.join(configRoot, file2)));
154+
expect(getEmitPath(file3, program)).toBe(replaceExtension(path.join(configRoot, file3)));
155+
});
148156
});
149157

150158
function normalize(path: string) {

0 commit comments

Comments
 (0)