Skip to content

Commit

Permalink
feat: Ability to get source file from directory based on relative or …
Browse files Browse the repository at this point in the history
…absolute path.

This should have been the original design.
  • Loading branch information
dsherret committed Dec 26, 2017
1 parent 91ac044 commit fb72396
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,44 +118,44 @@ export class Directory {
}

/**
* Gets a child source file with the specified name or throws if not found.
* @param fileName - File name.
* Gets a child source file with the specified path or throws if not found.
* @param path - Relative or absolute path to the file.
*/
getSourceFileOrThrow(fileName: string): SourceFile;
getSourceFileOrThrow(path: string): SourceFile;
/**
* Gets a child source file by the specified condition or throws if not found.
* @param condition - Condition to check the source file with.
*/
getSourceFileOrThrow(condition: (sourceFile: SourceFile) => boolean): SourceFile;
/** @internal */
getSourceFileOrThrow(fileNameOrCondition: string | ((sourceFile: SourceFile) => boolean)): SourceFile;
getSourceFileOrThrow(fileNameOrCondition: string | ((sourceFile: SourceFile) => boolean)) {
return errors.throwIfNullOrUndefined(this.getSourceFile(fileNameOrCondition), () => {
if (typeof fileNameOrCondition === "string")
return `Could not find child source file with name '${fileNameOrCondition}'.`;
getSourceFileOrThrow(pathOrCondition: string | ((sourceFile: SourceFile) => boolean)): SourceFile;
getSourceFileOrThrow(pathOrCondition: string | ((sourceFile: SourceFile) => boolean)) {
return errors.throwIfNullOrUndefined(this.getSourceFile(pathOrCondition), () => {
if (typeof pathOrCondition === "string")
return `Could not find child source file at path '${FileUtils.getStandardizedAbsolutePath(this.global.fileSystem, pathOrCondition, this.getPath())}'.`;
return "Could not find child source file that matched condition.";
});
}

/**
* Gets a child source file with the specified name or undefined if not found.
* @param fileName - File name.
* Gets a child source file with the specified path or undefined if not found.
* @param path - Relative or absolute path to the file.
*/
getSourceFile(fileName: string): SourceFile | undefined;
getSourceFile(path: string): SourceFile | undefined;
/**
* Gets a child source file by the specified condition or undefined if not found.
* @param condition - Condition to check the source file with.
*/
getSourceFile(condition: (sourceFile: SourceFile) => boolean): SourceFile | undefined;
/** @internal */
getSourceFile(fileNameOrCondition: string | ((sourceFile: SourceFile) => boolean)): SourceFile | undefined;
getSourceFile(fileNameOrCondition: string | ((sourceFile: SourceFile) => boolean)) {
if (typeof fileNameOrCondition === "string") {
const name = fileNameOrCondition;
fileNameOrCondition = sourceFile => FileUtils.getBaseName(sourceFile.getFilePath()) === name;
getSourceFile(pathOrCondition: string | ((sourceFile: SourceFile) => boolean)): SourceFile | undefined;
getSourceFile(pathOrCondition: string | ((sourceFile: SourceFile) => boolean)) {
if (typeof pathOrCondition === "string") {
const path = FileUtils.getStandardizedAbsolutePath(this.global.fileSystem, pathOrCondition, this.getPath());
return this.global.compilerFactory.getSourceFileFromFilePath(path);
}

return ArrayUtils.find(this.getSourceFiles(), fileNameOrCondition);
return ArrayUtils.find(this.getSourceFiles(), pathOrCondition);
}

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ export class Directory {

/**
* Creates a directory if it doesn't exist.
* @param path - Directory name or path to the directory that should be created.
* @param path - Relative or absolute path to the directory that should be created.
*/
createDirectory(path: string) {
const dirPath = FileUtils.getStandardizedAbsolutePath(this.global.fileSystem, path, this.getPath());
Expand Down
12 changes: 12 additions & 0 deletions src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,17 @@ describe(nameof(Directory), () => {
const directory = ast.createDirectory("dir");
const child1 = directory.createSourceFile("child1.ts");
const child2 = directory.createSourceFile("child2.ts");
const subDir = directory.createDirectory("subDir");
const child3 = subDir.createSourceFile("child3.ts");

it("should get based on the name", () => {
expect(directory.getSourceFile("child2.ts")!.getFilePath()).to.equal(child2.getFilePath());
});

it("should get based on the path", () => {
expect(directory.getSourceFile("subDir/child3.ts")!.getFilePath()).to.equal(child3.getFilePath());
});

it("should get based on a condition", () => {
expect(directory.getSourceFile(f => FileUtils.getBaseName(f.getFilePath()) === "child2.ts")!.getFilePath()).to.equal(child2.getFilePath());
});
Expand All @@ -460,11 +466,17 @@ describe(nameof(Directory), () => {
const directory = ast.createDirectory("dir");
const child1 = directory.createSourceFile("child1.ts");
const child2 = directory.createSourceFile("child2.ts");
const subDir = directory.createDirectory("subDir");
const child3 = subDir.createSourceFile("child3.ts");

it("should get based on the name", () => {
expect(directory.getSourceFileOrThrow("child2.ts").getFilePath()).to.equal(child2.getFilePath());
});

it("should get based on the path", () => {
expect(directory.getSourceFileOrThrow("subDir/child3.ts").getFilePath()).to.equal(child3.getFilePath());
});

it("should get based on a condition", () => {
expect(directory.getSourceFileOrThrow(f => FileUtils.getBaseName(f.getFilePath()) === "child2.ts").getFilePath()).to.equal(child2.getFilePath());
});
Expand Down

0 comments on commit fb72396

Please sign in to comment.