Skip to content

Commit 0110de2

Browse files
Keen Yee Liaumatsko
Keen Yee Liau
authored andcommittedJul 16, 2019
fix(language-service): Eagarly initialize data members (#31577)
Data members in TypeScriptServiceHost of Map type should be eagerly initialized to address issue/24571. This eliminates the need to constantly check for truthiness and makes code much more readable. More PRs to follow to address issue/24571. PR Close #31577
1 parent f166b6d commit 0110de2

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed
 

‎packages/language-service/src/typescript_host.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,15 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
6969
private _reflectorHost !: ReflectorHost;
7070
// TODO(issue/24571): remove '!'.
7171
private _checker !: ts.TypeChecker | null;
72-
private _typeCache: Symbol[] = [];
7372
private context: string|undefined;
7473
private lastProgram: ts.Program|undefined;
7574
private modulesOutOfDate: boolean = true;
7675
// TODO(issue/24571): remove '!'.
7776
private analyzedModules !: NgAnalyzedModules | null;
78-
// TODO(issue/24571): remove '!'.
79-
private fileToComponent !: Map<string, StaticSymbol>| null;
77+
private fileToComponent = new Map<string, StaticSymbol>();
8078
// TODO(issue/24571): remove '!'.
8179
private templateReferences !: string[] | null;
82-
// TODO(issue/24571): remove '!'.
83-
private collectedErrors !: Map<string, any[]>| null;
80+
private collectedErrors = new Map<string, any[]>();
8481
private fileVersions = new Map<string, string>();
8582

8683
constructor(private host: ts.LanguageServiceHost, private tsService: ts.LanguageService) {}
@@ -132,7 +129,7 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
132129
} else {
133130
this.ensureTemplateMap();
134131
// TODO: Cannocalize the file?
135-
const componentType = this.fileToComponent !.get(fileName);
132+
const componentType = this.fileToComponent.get(fileName);
136133
if (componentType) {
137134
return this.getSourceFromType(
138135
fileName, this.host.getScriptVersion(fileName), componentType);
@@ -168,7 +165,7 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
168165

169166
getTemplates(fileName: string): TemplateSources {
170167
this.ensureTemplateMap();
171-
const componentType = this.fileToComponent !.get(fileName);
168+
const componentType = this.fileToComponent.get(fileName);
172169
if (componentType) {
173170
const templateSource = this.getTemplateAt(fileName, 0);
174171
if (templateSource) {
@@ -224,7 +221,7 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
224221
this.analyzedModules = null;
225222
this._reflector = null;
226223
this.templateReferences = null;
227-
this.fileToComponent = null;
224+
this.fileToComponent.clear();
228225
this.ensureAnalyzedModules();
229226
this.modulesOutOfDate = false;
230227
}
@@ -274,15 +271,13 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
274271

275272
private clearCaches() {
276273
this._checker = null;
277-
this._typeCache = [];
278274
this._resolver = null;
279-
this.collectedErrors = null;
275+
this.collectedErrors.clear();
280276
this.modulesOutOfDate = true;
281277
}
282278

283279
private ensureTemplateMap() {
284-
if (!this.fileToComponent || !this.templateReferences) {
285-
const fileToComponent = new Map<string, StaticSymbol>();
280+
if (!this.templateReferences) {
286281
const templateReference: string[] = [];
287282
const ngModuleSummary = this.getAnalyzedModules();
288283
const urlResolver = createOfflineCompileUrlResolver();
@@ -293,12 +288,11 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
293288
const templateName = urlResolver.resolve(
294289
this.reflector.componentModuleUrl(directive.reference),
295290
metadata.template.templateUrl);
296-
fileToComponent.set(templateName, directive.reference);
291+
this.fileToComponent.set(templateName, directive.reference);
297292
templateReference.push(templateName);
298293
}
299294
}
300295
}
301-
this.fileToComponent = fileToComponent;
302296
this.templateReferences = templateReference;
303297
}
304298
}
@@ -412,11 +406,7 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
412406

413407
private collectError(error: any, filePath: string|null) {
414408
if (filePath) {
415-
let errorMap = this.collectedErrors;
416-
if (!errorMap || !this.collectedErrors) {
417-
errorMap = this.collectedErrors = new Map();
418-
}
419-
let errors = errorMap.get(filePath);
409+
let errors = this.collectedErrors.get(filePath);
420410
if (!errors) {
421411
errors = [];
422412
this.collectedErrors.set(filePath, errors);
@@ -517,7 +507,7 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
517507
}
518508

519509
private getCollectedErrors(defaultSpan: Span, sourceFile: ts.SourceFile): DeclarationError[] {
520-
const errors = (this.collectedErrors && this.collectedErrors.get(sourceFile.fileName));
510+
const errors = this.collectedErrors.get(sourceFile.fileName);
521511
return (errors && errors.map((e: any) => {
522512
const line = e.line || (e.position && e.position.line);
523513
const column = e.column || (e.position && e.position.column);

0 commit comments

Comments
 (0)
Please sign in to comment.