Skip to content

Commit 7690be3

Browse files
hazzard993Perryvw
authored andcommitted
Dot slash path fix (#426)
* Fixed ./ paths * Added tests * Changed statement to ternary
1 parent bc844df commit 7690be3

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/LuaTransformer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,12 +3917,12 @@ export class LuaTransformer {
39173917
}
39183918

39193919
private getImportPath(relativePath: string): string {
3920-
const rootDir = this.options.rootDir || path.resolve(".");
3921-
const absoluteImportPath = this.formatPathToLuaPath(this.getAbsoluteImportPath(relativePath));
3922-
const absoluteRootDirPath = this.formatPathToLuaPath(rootDir);
3920+
const rootDir = this.options.rootDir ? path.resolve(this.options.rootDir) : path.resolve(".");
3921+
const absoluteImportPath = path.format(path.parse(this.getAbsoluteImportPath(relativePath)));
3922+
const absoluteRootDirPath = path.format(path.parse(rootDir));
39233923
if (absoluteImportPath.includes(absoluteRootDirPath)) {
3924-
const relativePathToRoot = absoluteImportPath.replace(absoluteRootDirPath, "").slice(1);
3925-
return relativePathToRoot;
3924+
return this.formatPathToLuaPath(
3925+
absoluteImportPath.replace(absoluteRootDirPath, "").slice(1));
39263926
} else {
39273927
throw TSTLErrors.UnresolvableRequirePath(undefined,
39283928
`Cannot create require path. Module does not exist within --rootDir`,

test/unit/require.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { CompilerOptions } from "../../src/CompilerOptions";
77
export class RequireTests {
88

99
@TestCase("file.ts", "./folder/Module", "folder.Module", { rootDir: "." }, false)
10+
@TestCase("file.ts", "./folder/Module", "folder.Module", { rootDir: "./" }, false)
1011
@TestCase("src/file.ts", "./folder/Module", "src.folder.Module", { rootDir: "." }, false)
1112
@TestCase("file.ts", "folder/Module", "folder.Module", { rootDir: ".", baseUrl: "." }, false)
13+
@TestCase("file.ts", "folder/Module", "folder.Module", { rootDir: "./", baseUrl: "." }, false)
1214
@TestCase("src/file.ts", "./folder/Module", "folder.Module", { rootDir: "src" }, false)
1315
@TestCase("src/file.ts", "./folder/Module", "folder.Module", { rootDir: "./src" }, false)
1416
@TestCase("file.ts", "../Module", "", { rootDir: "./src" }, true)
@@ -22,7 +24,6 @@ export class RequireTests {
2224
options: CompilerOptions,
2325
throwsError: boolean): void {
2426
const regex = /require\("(.*?)"\)/; // This regex extracts `hello` from require("hello")
25-
options.rootDir = path.resolve(options.rootDir); // This happens automatically from the command line
2627
if (throwsError) {
2728
Expect(() => util.transpileString(`import * from "${usedPath}";`, options, true, filePath)).toThrow();
2829
} else {

0 commit comments

Comments
 (0)