Skip to content

Commit

Permalink
refactor(language-service): Colocate getAnalzyedModules and upToDate (a…
Browse files Browse the repository at this point in the history
…ngular#33779)

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

PR Close angular#33779
  • Loading branch information
Keen Yee Liau authored and kara committed Nov 13, 2019
1 parent cf10b33 commit 2be0a1d
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions packages/language-service/src/typescript_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 2be0a1d

Please sign in to comment.