Skip to content

Commit

Permalink
perf(bootstrap): Implement using getProjectVersion() in @ts-morph/boo…
Browse files Browse the repository at this point in the history
…tstrap.
  • Loading branch information
dsherret committed Nov 21, 2020
1 parent 7c524ec commit 646f797
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/bootstrap/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class Project {
compilerOptions: this.compilerOptions,
getNewLine: () => newLineKind,
resolutionHost: resolutionHost || {},
getProjectVersion: () => this._sourceFileCache.getProjectVersion().toString(),
});
this.languageServiceHost = languageServiceHost;
this.compilerHost = compilerHost;
Expand Down
6 changes: 6 additions & 0 deletions packages/bootstrap/src/SourceFileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DocumentRegistry, ScriptKind, ts, StringUtils, TsSourceFileContainer, F

export class SourceFileCache implements TsSourceFileContainer {
private readonly sourceFilesByFilePath = new Map<StandardizedFilePath, ts.SourceFile>();
private projectVersion = 0;

readonly documentRegistry: DocumentRegistry;

Expand All @@ -25,6 +26,10 @@ export class SourceFileCache implements TsSourceFileContainer {
return this.sourceFilesByFilePath.values();
}

getProjectVersion() {
return this.projectVersion;
}

getSourceFileVersion(sourceFile: ts.SourceFile) {
return this.documentRegistry.getSourceFileVersion(sourceFile);
}
Expand Down Expand Up @@ -91,6 +96,7 @@ export class SourceFileCache implements TsSourceFileContainer {
this.fileSystemWrapper.queueMkdir(dirPath);

this.sourceFilesByFilePath.set(standardizedFilePath, sourceFile);
this.projectVersion++;
}

removeSourceFile(filePath: StandardizedFilePath) {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/lib/ts-morph-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export interface CreateHostsOptions {
getNewLine: () => "\r\n" | "\n";
/** The resolution host used for resolving modules and type reference directives. */
resolutionHost: ResolutionHost;
/** Provides the current project version to be used for up-to-date checks. */
/** Provides the current project version to be used to tell if source files have
* changed. Provide this for a performance improvement. */
getProjectVersion?: () => string;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/compiler/createHosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export interface CreateHostsOptions {
getNewLine: () => "\r\n" | "\n";
/** The resolution host used for resolving modules and type reference directives. */
resolutionHost: ResolutionHost;
/** Provides the current project version to be used for up-to-date checks. */
/** Provides the current project version to be used to tell if source files have
* changed. Provide this for a performance improvement. */
getProjectVersion?: () => string;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class LanguageService {
/** @internal */
private _context: ProjectContext;
/** @internal */
private _projectVersion: number = 0;
private _projectVersion = 0;

/**
* Gets the compiler language service.
Expand Down

0 comments on commit 646f797

Please sign in to comment.