From b367ce8535712192aba26bbd96baec76cfa93a0c Mon Sep 17 00:00:00 2001 From: Perryvw Date: Thu, 29 Feb 2024 22:51:30 +0100 Subject: [PATCH] Fix bug outputting files to the wrong location in some cases --- src/transpilation/transpiler.ts | 4 ++- .../module-resolution.spec.ts.snap | 2 +- test/transpile/paths.spec.ts | 34 ++++++++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/transpilation/transpiler.ts b/src/transpilation/transpiler.ts index 1c0c28444..9bac1f5bf 100644 --- a/src/transpilation/transpiler.ts +++ b/src/transpilation/transpiler.ts @@ -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 { diff --git a/test/transpile/__snapshots__/module-resolution.spec.ts.snap b/test/transpile/__snapshots__/module-resolution.spec.ts.snap index e87569ded..c9f0be298 100644 --- a/test/transpile/__snapshots__/module-resolution.spec.ts.snap +++ b/test/transpile/__snapshots__/module-resolution.spec.ts.snap @@ -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", ] `; diff --git a/test/transpile/paths.spec.ts b/test/transpile/paths.spec.ts index 0917307b8..f98b6e635 100644 --- a/test/transpile/paths.spec.ts +++ b/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(); @@ -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")); }); }); @@ -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) {