Skip to content

Commit

Permalink
feat: #733 - Soft rename addExistingSourceFile(s)(IfExists)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Ivicevic authored and dsherret committed Oct 27, 2019
1 parent fae0a94 commit 59fc2d5
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 37 deletions.
42 changes: 38 additions & 4 deletions packages/ts-morph/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,19 @@ export class Project {
* Adds source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
* @deprecated Use `addSourceFilesAtPaths`.
*/
addExistingSourceFiles(fileGlobs: string | ReadonlyArray<string>): SourceFile[] {
return this._context.directoryCoordinator.addExistingSourceFiles(fileGlobs, { markInProject: true });
return this.addSourceFilesAtPaths(fileGlobs);
}

/**
* Adds source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
*/
addSourceFilesAtPaths(fileGlobs: string | ReadonlyArray<string>): SourceFile[] {
return this._context.directoryCoordinator.addSourceFilesAtPaths(fileGlobs, { markInProject: true });
}

/**
Expand All @@ -240,9 +250,21 @@ export class Project {
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @skipOrThrowCheck
* @deprecated Use `addSourceFileAtPathIfExists`.
*/
addExistingSourceFileIfExists(filePath: string): SourceFile | undefined {
return this._context.directoryCoordinator.addExistingSourceFileIfExists(filePath, { markInProject: true });
return this.addSourceFileAtPathIfExists(filePath);
}

/**
* Adds a source file from a file path if it exists or returns undefined.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @skipOrThrowCheck
*/
addSourceFileAtPathIfExists(filePath: string): SourceFile | undefined {
return this._context.directoryCoordinator.addSourceFileAtPathIfExists(filePath, { markInProject: true });
}

/**
Expand All @@ -251,9 +273,21 @@ export class Project {
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @throws FileNotFoundError when the file is not found.
* @deprecated Use `addSourceFileAtPath`.
*/
addExistingSourceFile(filePath: string): SourceFile {
return this._context.directoryCoordinator.addExistingSourceFile(filePath, { markInProject: true });
return this.addSourceFileAtPath(filePath);
}

/**
* Adds an existing source file from a file path or throws if it doesn't exist.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @throws FileNotFoundError when the file is not found.
*/
addSourceFileAtPath(filePath: string): SourceFile {
return this._context.directoryCoordinator.addSourceFileAtPath(filePath, { markInProject: true });
}

/**
Expand All @@ -273,7 +307,7 @@ export class Project {
private _addSourceFilesForTsConfigResolver(tsConfigResolver: TsConfigResolver, compilerOptions: CompilerOptions) {
const paths = tsConfigResolver.getPaths(compilerOptions);

const addedSourceFiles = paths.filePaths.map(p => this.addExistingSourceFile(p));
const addedSourceFiles = paths.filePaths.map(p => this.addSourceFileAtPath(p));
for (const dirPath of paths.directoryPaths)
this.addExistingDirectoryIfExists(dirPath);
return addedSourceFiles;
Expand Down
40 changes: 37 additions & 3 deletions packages/ts-morph/src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,18 @@ export class Directory {
* Add source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
* @deprecated Use `addSourceFilesAtPaths`.
*/
addExistingSourceFiles(fileGlobs: string | ReadonlyArray<string>): SourceFile[] {
return this.addSourceFilesAtPaths(fileGlobs);
}

/**
* Add source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
*/
addSourceFilesAtPaths(fileGlobs: string | ReadonlyArray<string>): SourceFile[] {
fileGlobs = typeof fileGlobs === "string" ? [fileGlobs] : fileGlobs;
fileGlobs = fileGlobs.map(g => {
if (FileUtils.pathIsAbsolute(g))
Expand All @@ -250,7 +260,7 @@ export class Directory {
return FileUtils.pathJoin(this.getPath(), g);
});

return this._context.directoryCoordinator.addExistingSourceFiles(fileGlobs, { markInProject: this._isInProject() });
return this._context.directoryCoordinator.addSourceFilesAtPaths(fileGlobs, { markInProject: this._isInProject() });
}

/**
Expand Down Expand Up @@ -317,10 +327,22 @@ export class Directory {
* Will return the source file if it was already added.
* @param relativeFilePath - Relative file path to add.
* @skipOrThrowCheck
* @deprecated Use `addSourceFileAtPathIfExists`.
*/
addExistingSourceFileIfExists(relativeFilePath: string): SourceFile | undefined {
return this.addSourceFileAtPathIfExists(relativeFilePath);
}

/**
* Adds an existing source file, relative to this directory, or returns undefined.
*
* Will return the source file if it was already added.
* @param relativeFilePath - Relative file path to add.
* @skipOrThrowCheck
*/
addSourceFileAtPathIfExists(relativeFilePath: string): SourceFile | undefined {
const filePath = this._context.fileSystemWrapper.getStandardizedAbsolutePath(relativeFilePath, this.getPath());
return this._context.directoryCoordinator.addExistingSourceFileIfExists(filePath, { markInProject: this._isInProject() });
return this._context.directoryCoordinator.addSourceFileAtPathIfExists(filePath, { markInProject: this._isInProject() });
}

/**
Expand All @@ -329,10 +351,22 @@ export class Directory {
* Will return the source file if it was already added.
* @param relativeFilePath - Relative file path to add.
* @throws FileNotFoundError when the file doesn't exist.
* @deprecated Use `addSourceFileAtPath`.
*/
addExistingSourceFile(relativeFilePath: string): SourceFile {
return this.addSourceFileAtPath(relativeFilePath);
}

/**
* Adds an existing source file, relative to this directory, or throws if it doesn't exist.
*
* Will return the source file if it was already added.
* @param relativeFilePath - Relative file path to add.
* @throws FileNotFoundError when the file doesn't exist.
*/
addSourceFileAtPath(relativeFilePath: string): SourceFile {
const filePath = this._context.fileSystemWrapper.getStandardizedAbsolutePath(relativeFilePath, this.getPath());
return this._context.directoryCoordinator.addExistingSourceFile(filePath, { markInProject: this._isInProject() });
return this._context.directoryCoordinator.addSourceFileAtPath(filePath, { markInProject: this._isInProject() });
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/ts-morph/src/fileSystem/DirectoryCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ export class DirectoryCoordinator {
return this.compilerFactory.createDirectoryOrAddIfExists(dirPath, options);
}

addExistingSourceFileIfExists(filePath: string, options: { markInProject: boolean; }): SourceFile | undefined {
addSourceFileAtPathIfExists(filePath: string, options: { markInProject: boolean; }): SourceFile | undefined {
return this.compilerFactory.addOrGetSourceFileFromFilePath(filePath, {
markInProject: options.markInProject,
scriptKind: undefined
});
}

addExistingSourceFile(filePath: string, options: { markInProject: boolean; }): SourceFile {
const sourceFile = this.addExistingSourceFileIfExists(filePath, options);
addSourceFileAtPath(filePath: string, options: { markInProject: boolean; }): SourceFile {
const sourceFile = this.addSourceFileAtPathIfExists(filePath, options);
if (sourceFile == null)
throw new errors.FileNotFoundError(this.fileSystemWrapper.getStandardizedAbsolutePath(filePath));
return sourceFile;
}

addExistingSourceFiles(fileGlobs: string | ReadonlyArray<string>, options: { markInProject: boolean; }): SourceFile[] {
addSourceFilesAtPaths(fileGlobs: string | ReadonlyArray<string>, options: { markInProject: boolean; }): SourceFile[] {
if (typeof fileGlobs === "string")
fileGlobs = [fileGlobs];

const sourceFiles: SourceFile[] = [];
const globbedDirectories = FileUtils.getParentMostPaths(fileGlobs.filter(g => !FileUtils.isNegatedGlob(g)).map(g => FileUtils.getGlobDir(g)));

for (const filePath of this.fileSystemWrapper.glob(fileGlobs)) {
const sourceFile = this.addExistingSourceFileIfExists(filePath, options);
const sourceFile = this.addSourceFileAtPathIfExists(filePath, options);
if (sourceFile != null)
sourceFiles.push(sourceFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe(nameof(MemoryEmitResult), () => {
const project = new Project({ compilerOptions, useVirtualFileSystem: true });
const fileSystem = project.getFileSystem();
fileSystem.writeFileSync("file1.ts", "\uFEFFconst num1 = 1;"); // has BOM
project.addExistingSourceFile("file1.ts");
project.addSourceFileAtPath("file1.ts");
project.createSourceFile("file2.ts", "const num2 = 2;");
return { project, fileSystem };
}
Expand Down
22 changes: 11 additions & 11 deletions packages/ts-morph/src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,66 +361,66 @@ describe(nameof(Directory), () => {
});
});

describe(nameof<Directory>(d => d.addExistingSourceFileIfExists), () => {
describe(nameof<Directory>(d => d.addSourceFileAtPathIfExists), () => {
it("should return undefined if adding a source file at a non-existent path", () => {
const fileSystem = getFileSystemHostWithFiles([]);
const project = new Project({ fileSystem });
const directory = project.createDirectory("dir");
expect(directory.addExistingSourceFileIfExists("non-existent-file.ts")).to.be.undefined;
expect(directory.addSourceFileAtPathIfExists("non-existent-file.ts")).to.be.undefined;
});

it("should add a source file that exists", () => {
const fileSystem = getFileSystemHostWithFiles([{ filePath: "dir/file.ts", text: "" }], ["dir"]);
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const sourceFile = directory.addExistingSourceFileIfExists("file.ts");
const sourceFile = directory.addSourceFileAtPathIfExists("file.ts");
expect(sourceFile).to.not.be.undefined;
expect(sourceFile!.getLanguageVersion()).to.equal(ScriptTarget.Latest);
});
});

describe(nameof<Directory>(d => d.addExistingSourceFile), () => {
describe(nameof<Directory>(d => d.addSourceFileAtPath), () => {
it("should throw an exception if adding a source file at a non-existent path", () => {
const fileSystem = getFileSystemHostWithFiles([]);
const project = new Project({ fileSystem });
const directory = project.createDirectory("dir");
expect(() => {
directory.addExistingSourceFile("non-existent-file.ts");
directory.addSourceFileAtPath("non-existent-file.ts");
}).to.throw(errors.FileNotFoundError, `File not found: /dir/non-existent-file.ts`);
});

it("should add a source file that exists", () => {
const fileSystem = getFileSystemHostWithFiles([{ filePath: "dir/file.ts", text: "" }], ["dir"]);
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const sourceFile = directory.addExistingSourceFile("file.ts");
const sourceFile = directory.addSourceFileAtPath("file.ts");
expect(sourceFile).to.not.be.undefined;
expect(sourceFile.getLanguageVersion()).to.equal(ScriptTarget.Latest);
});
});

describe(nameof<Directory>(d => d.addExistingSourceFiles), () => {
describe(nameof<Directory>(d => d.addSourceFilesAtPaths), () => {
const fileSystem = getFileSystemHostWithFiles([{ filePath: "otherDir/file.ts", text: "" }, { filePath: "dir/dir1/dir1/file.ts", text: "" }],
["dir", "dir/dir1", "dir/dir2", "dir/dir1/dir1", "otherDir"]);

it("should add source files by a relative file glob", () => {
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const sourceFiles = directory.addExistingSourceFiles("**/*.ts");
const sourceFiles = directory.addSourceFilesAtPaths("**/*.ts");
expect(sourceFiles.map(s => s.getFilePath())).to.deep.equal(["/dir/dir1/dir1/file.ts"]);
});

it("should add source files by multiple file globs", () => {
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const sourceFiles = directory.addExistingSourceFiles(["**/*.ts", "../**/*.ts"]);
const sourceFiles = directory.addSourceFilesAtPaths(["**/*.ts", "../**/*.ts"]);
expect(sourceFiles.map(s => s.getFilePath())).to.deep.equal(["/dir/dir1/dir1/file.ts", "/otherDir/file.ts"]);
});

it("should add source files by an absolute file glob", () => {
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const sourceFiles = directory.addExistingSourceFiles("/otherDir/**/*.ts");
const sourceFiles = directory.addSourceFilesAtPaths("/otherDir/**/*.ts");
expect(sourceFiles.map(s => s.getFilePath())).to.deep.equal(["/otherDir/file.ts"]);
});
});
Expand Down Expand Up @@ -1440,7 +1440,7 @@ describe(nameof(Directory), () => {
const project = new Project({ fileSystem });
const directory = project.addExistingDirectory("dir");
const childDir = directory.createDirectory("childDir");
const sourceFile = directory.addExistingSourceFile("file.ts");
const sourceFile = directory.addSourceFileAtPath("file.ts");
const otherSourceFile = project.createSourceFile("otherFile.ts");

deleteImmediately(directory, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/tests/issues/413tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("tests for issue #413", () => {
fs.writeFileSync("/dir2/foo.ts", "");
fs.writeFileSync("/dir2/bar.ts", "");

const sourceFiles = project.addExistingSourceFiles(["*.ts", "dir/*.ts"]);
const sourceFiles = project.addSourceFilesAtPaths(["*.ts", "dir/*.ts"]);
expect(sourceFiles.length).to.equal(3); // won't get in dir2 directory
});
});

0 comments on commit 59fc2d5

Please sign in to comment.