Skip to content

Commit

Permalink
Fix bug outputting files to the wrong location in some cases (#1541)
Browse files Browse the repository at this point in the history
  • Loading branch information
Perryvw committed Mar 1, 2024
1 parent 91c05b5 commit b6fce8d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/transpilation/transpiler.ts
Expand Up @@ -203,7 +203,9 @@ export function getSourceDir(program: ts.Program): string {
if (rootDir && rootDir.length > 0) {
return path.isAbsolute(rootDir) ? rootDir : path.resolve(getProjectRoot(program), rootDir);
}
return program.getCommonSourceDirectory();

// If no rootDir is given, source is relative to the project root
return getProjectRoot(program);
}

export function getEmitOutDir(program: ts.Program): string {
Expand Down
Expand Up @@ -10,8 +10,8 @@ exports[`supports complicated paths configuration 1`] = `

exports[`supports paths configuration 1`] = `
[
"/paths-simple/myprogram/dist/main.lua",
"/paths-simple/myprogram/dist/mypackage/bar.lua",
"/paths-simple/myprogram/dist/mypackage/index.lua",
"/paths-simple/myprogram/dist/myprogram/main.lua",
]
`;
34 changes: 21 additions & 13 deletions test/transpile/paths.spec.ts
@@ -1,6 +1,6 @@
import * as path from "path";
import * as ts from "typescript";
import { getSourceDir } from "../../src";
import { getEmitPath, getSourceDir } from "../../src";
import * as util from "../util";

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

// getCommonSourceDirectory does not work right so mock it
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);

// If rootdir is specified, rootDir is the sourceDir
expect(getSourceDir(program)).toBe(path.join(cwd, "src"));
});

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

// getCommonSourceDirectory does not work right so mock it
jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd);

// Common sources directory is project root
// If rootDir is not specified, root dir is where the config file is
expect(normalize(getSourceDir(program))).toBe(cwd);
});

test("without rootDir in src dir", () => {
const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], {
configFilePath,
});
test("without config file in src dir", () => {
const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], {});

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

// Common sources directory is src
// If there is no config file, return the common source directory
expect(normalize(getSourceDir(program))).toBe(path.join(cwd, "src"));
});
});
Expand Down Expand Up @@ -145,6 +138,21 @@ describe("getEmitPath", () => {
expect(fileNames).toHaveLength(1);
expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.scar"));
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1540
test("puts files next to their source if no config is given (#1540)", () => {
const file1 = path.join("src", "main.ts");
const file2 = path.join("src", "otherfile.ts");
const file3 = path.join("src", "nested", "nestedfile.ts");
const program = ts.createProgram([file1, file2, file3], { configFilePath });

// If rootDir is not specified, root dir is where the config file is
const configRoot = path.dirname(configFilePath);
const replaceExtension = (f: string) => f.replace(/\.ts$/, ".lua");
expect(getEmitPath(file1, program)).toBe(replaceExtension(path.join(configRoot, file1)));
expect(getEmitPath(file2, program)).toBe(replaceExtension(path.join(configRoot, file2)));
expect(getEmitPath(file3, program)).toBe(replaceExtension(path.join(configRoot, file3)));
});
});

function normalize(path: string) {
Expand Down

0 comments on commit b6fce8d

Please sign in to comment.