refactor(language-service): Colocate getAnalzyedModules and upToDate (#33779)

These two functions go hand-in-hand, putting them together improves
readability.

PR Close #33779
This commit is contained in:
Keen Yee Liau 2019-11-12 15:53:58 -08:00 committed by Kara Erickson
parent cf10b336e7
commit 2be0a1d35b
1 changed files with 38 additions and 38 deletions

View File

@ -191,6 +191,44 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
return this.analyzedModules;
}
/**
* Checks whether the program has changed, and invalidate caches if it has.
* Returns true if modules are up-to-date, false otherwise.
* This should only be called by getAnalyzedModules().
*/
private upToDate(): boolean {
const {lastProgram, program} = this;
if (lastProgram === program) {
return true;
}
this.lastProgram = program;
// Invalidate file that have changed in the static symbol resolver
const seen = new Set<string>();
for (const sourceFile of program.getSourceFiles()) {
const fileName = sourceFile.fileName;
seen.add(fileName);
const version = this.tsLsHost.getScriptVersion(fileName);
const lastVersion = this.fileVersions.get(fileName);
this.fileVersions.set(fileName, version);
// Should not invalidate file on the first encounter or if file hasn't changed
if (lastVersion !== undefined && version !== lastVersion) {
const symbols = this.staticSymbolResolver.invalidateFile(fileName);
this.reflector.invalidateSymbols(symbols);
}
}
// Remove file versions that are no longer in the program and invalidate them.
const missing = Array.from(this.fileVersions.keys()).filter(f => !seen.has(f));
missing.forEach(f => {
this.fileVersions.delete(f);
const symbols = this.staticSymbolResolver.invalidateFile(f);
this.reflector.invalidateSymbols(symbols);
});
return false;
}
/**
* Find all templates in the specified `file`.
* @param fileName TS or HTML file
@ -282,44 +320,6 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
return program;
}
/**
* Checks whether the program has changed, and invalidate caches if it has.
* Returns true if modules are up-to-date, false otherwise.
* This should only be called by getAnalyzedModules().
*/
private upToDate(): boolean {
const {lastProgram, program} = this;
if (lastProgram === program) {
return true;
}
this.lastProgram = program;
// Invalidate file that have changed in the static symbol resolver
const seen = new Set<string>();
for (const sourceFile of program.getSourceFiles()) {
const fileName = sourceFile.fileName;
seen.add(fileName);
const version = this.tsLsHost.getScriptVersion(fileName);
const lastVersion = this.fileVersions.get(fileName);
this.fileVersions.set(fileName, version);
// Should not invalidate file on the first encounter or if file hasn't changed
if (lastVersion !== undefined && version !== lastVersion) {
const symbols = this.staticSymbolResolver.invalidateFile(fileName);
this.reflector.invalidateSymbols(symbols);
}
}
// Remove file versions that are no longer in the program and invalidate them.
const missing = Array.from(this.fileVersions.keys()).filter(f => !seen.has(f));
missing.forEach(f => {
this.fileVersions.delete(f);
const symbols = this.staticSymbolResolver.invalidateFile(f);
this.reflector.invalidateSymbols(symbols);
});
return false;
}
/**
* Return the TemplateSource if `node` is a template node.
*