Skip to content

Commit

Permalink
fix: Project.getSourceFileOrThrow() should always output the passed i…
Browse files Browse the repository at this point in the history
…n string in the error message.

Previously it wouldn't do this if the path was relative.
  • Loading branch information
dsherret committed Oct 6, 2018
1 parent 762254f commit c81081e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
32 changes: 14 additions & 18 deletions src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ export class Project {
getSourceFileOrThrow(fileNameOrSearchFunction: string | ((file: SourceFile) => boolean)): SourceFile {
const sourceFile = this.getSourceFile(fileNameOrSearchFunction);
if (sourceFile == null) {
const filePathOrSearchFunction = getFilePathOrSearchFunction(this.context.fileSystemWrapper, fileNameOrSearchFunction);
if (typeof filePathOrSearchFunction === "string")
throw new errors.InvalidOperationError(`Could not find source file based on the provided name or path: ${filePathOrSearchFunction}.`);
if (typeof fileNameOrSearchFunction === "string")
throw new errors.InvalidOperationError(`Could not find source file based on the provided name or path: ${fileNameOrSearchFunction}`);
else
throw new errors.InvalidOperationError(`Could not find source file based on the provided condition.`);
}
Expand All @@ -309,11 +308,22 @@ export class Project {
*/
getSourceFile(fileNameOrSearchFunction: string | ((file: SourceFile) => boolean)): SourceFile | undefined;
getSourceFile(fileNameOrSearchFunction: string | ((file: SourceFile) => boolean)): SourceFile | undefined {
const filePathOrSearchFunction = getFilePathOrSearchFunction(this.context.fileSystemWrapper, fileNameOrSearchFunction);
const filePathOrSearchFunction = getFilePathOrSearchFunction(this.context.fileSystemWrapper);

if (typeof filePathOrSearchFunction === "string")
return this.context.compilerFactory.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction);
return ArrayUtils.find(this.context.compilerFactory.getSourceFilesByDirectoryDepth(), filePathOrSearchFunction);

function getFilePathOrSearchFunction(fileSystemWrapper: FileSystemWrapper): string | ((file: SourceFile) => boolean) {
if (fileNameOrSearchFunction instanceof Function)
return fileNameOrSearchFunction;

const fileNameOrPath = FileUtils.standardizeSlashes(fileNameOrSearchFunction);
if (FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0)
return fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath);
else
return def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrPath);
}
}

/**
Expand Down Expand Up @@ -478,17 +488,3 @@ export class Project {
return this.context.compilerFactory.forgetNodesCreatedInBlock(block);
}
}

function getFilePathOrSearchFunction(
fileSystemWrapper: FileSystemWrapper,
fileNameOrSearchFunction: string | ((file: SourceFile) => boolean)
): string | ((file: SourceFile) => boolean) {
if (fileNameOrSearchFunction instanceof Function)
return fileNameOrSearchFunction;

const fileNameOrPath = FileUtils.standardizeSlashes(fileNameOrSearchFunction);
if (FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0)
return fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath);
else
return def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrPath);
}
14 changes: 11 additions & 3 deletions src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,22 @@ describe(nameof(Project), () => {
});

describe(nameof<Project>(project => project.getSourceFileOrThrow), () => {
it("should throw when it can't find the source file based on a provided path", () => {
it("should throw when it can't find the source file based on a provided file name", () => {
const project = new Project({ useVirtualFileSystem: true });
expect(() => project.getSourceFileOrThrow("some path")).to.throw();
expect(() => project.getSourceFileOrThrow("fileName.ts")).to.throw(errors.InvalidOperationError,
"Could not find source file based on the provided name or path: fileName.ts");
});

it("should throw when it can't find the source file based on a provided absolute path", () => {
const project = new Project({ useVirtualFileSystem: true });
expect(() => project.getSourceFileOrThrow("/fileName.ts")).to.throw(errors.InvalidOperationError,
"Could not find source file based on the provided name or path: /fileName.ts");
});

it("should throw when it can't find the source file based on a provided condition", () => {
const project = new Project({ useVirtualFileSystem: true });
expect(() => project.getSourceFileOrThrow(s => false)).to.throw();
expect(() => project.getSourceFileOrThrow(s => false)).to.throw(errors.InvalidOperationError,
"Could not find source file based on the provided condition.");
});

it("should not throw when it finds the file", () => {
Expand Down

0 comments on commit c81081e

Please sign in to comment.