Skip to content

Commit

Permalink
fix: getRelativePathToSourceFileAsModuleSpecifier() should return `..…
Browse files Browse the repository at this point in the history
…/` instead of `./../` when going back a directory.
  • Loading branch information
dsherret committed Feb 19, 2018
1 parent f5fa32f commit a7954fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/compiler/file/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
*/
getRelativePathToSourceFileAsModuleSpecifier(sourceFile: SourceFile) {
const sourceFilePath = sourceFile.getFilePath().replace(/\/index(\.d\.ts|\.ts|\.js)$/i, "");
return "./" + FileUtils.getRelativePathTo(this.getFilePath(), sourceFilePath).replace(/((\.d\.ts$)|(\.[^/.]+$))/i, "");
const moduleSpecifier = FileUtils.getRelativePathTo(this.getFilePath(), sourceFilePath).replace(/((\.d\.ts$)|(\.[^/.]+$))/i, "");
return StringUtils.startsWith(moduleSpecifier, "../") ? moduleSpecifier : "./" + moduleSpecifier;
}

private _refreshFromFileSystemInternal(fileReadResult: string | false): FileSystemRefreshResult {
Expand Down
22 changes: 11 additions & 11 deletions src/tests/compiler/file/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ describe(nameof(SourceFile), () => {
const file4 = tsSimpleAst.createSourceFile("/file4.ts", file4Text);
sourceFile.move("/dir/NewFile.ts");
expect(file1.getFullText()).to.equal(`import {MyInterface} from "./dir/NewFile";`);
expect(file2.getFullText()).to.equal(`import * as interfaces from "./../dir/NewFile";\nimport "./../dir/NewFile";`);
expect(file3.getFullText()).to.equal(`export * from "./../dir/NewFile";`);
expect(file2.getFullText()).to.equal(`import * as interfaces from "../dir/NewFile";\nimport "../dir/NewFile";`);
expect(file3.getFullText()).to.equal(`export * from "../dir/NewFile";`);
expect(file4.getFullText()).to.equal(file4Text);
});

Expand All @@ -124,7 +124,7 @@ describe(nameof(SourceFile), () => {
const {sourceFile, tsSimpleAst} = getInfoFromText(fileText, { filePath: "/MyInterface.ts" });
const otherFile = tsSimpleAst.createSourceFile("/OtherInterface.ts", `import {MyInterface} from "./MyInterface";\nexport interface OtherInterface {}`);
sourceFile.move("/dir/NewFile.ts");
expect(sourceFile.getFullText()).to.equal(`import {OtherInterface} from "./../OtherInterface";\nexport interface MyInterface {}\nexport * from "./../OtherInterface";`);
expect(sourceFile.getFullText()).to.equal(`import {OtherInterface} from "../OtherInterface";\nexport interface MyInterface {}\nexport * from "../OtherInterface";`);
expect(otherFile.getFullText()).to.equal(`import {MyInterface} from "./dir/NewFile";\nexport interface OtherInterface {}`);
});

Expand Down Expand Up @@ -1056,7 +1056,7 @@ function myFunction(param: MyClass) {
}

it("should get the module specifier to a source file in a different directory", () => {
doTest("/dir/from.ts", "/dir2/to.ts", "./../dir2/to");
doTest("/dir/from.ts", "/dir2/to.ts", "../dir2/to");
});

it("should get the module specifier to a source file in the same directory", () => {
Expand All @@ -1068,31 +1068,31 @@ function myFunction(param: MyClass) {
});

it("should get the module specifier to a definition file", () => {
doTest("/dir/from.ts", "/dir2/to.d.ts", "./../dir2/to");
doTest("/dir/from.ts", "/dir2/to.d.ts", "../dir2/to");
});

it("should get the module specifier to a definition file that doing use a lower case extension", () => {
doTest("/dir/from.ts", "/dir2/to.D.TS", "./../dir2/to");
doTest("/dir/from.ts", "/dir2/to.D.TS", "../dir2/to");
});

it("should use an implicit index when specifying the index file in a different directory", () => {
doTest("/dir/file.ts", "/dir2/index.ts", "./../dir2");
doTest("/dir/file.ts", "/dir2/index.ts", "../dir2");
});

it("should use an implicit index when specifying the index file in a parent directory", () => {
doTest("/dir/parent/file.ts", "/dir/index.ts", "./../../dir");
doTest("/dir/parent/file.ts", "/dir/index.ts", "../../dir");
});

it("should use an implicit index when specifying the index file in a different directory that has different casing", () => {
doTest("/dir/file.ts", "/dir2/INDEX.ts", "./../dir2");
doTest("/dir/file.ts", "/dir2/INDEX.ts", "../dir2");
});

it("should use an implicit index when specifying the index file of a definition file in a different directory", () => {
doTest("/dir/file.ts", "/dir2/index.d.ts", "./../dir2");
doTest("/dir/file.ts", "/dir2/index.d.ts", "../dir2");
});

it("should use an implicit index when specifying the index file in the same directory", () => {
doTest("/dir/file.ts", "/dir/index.ts", "./../dir");
doTest("/dir/file.ts", "/dir/index.ts", "../dir");
});
});

Expand Down

0 comments on commit a7954fa

Please sign in to comment.