refactor(language-service): dedupe diagnostics using ts utility function (#35086)

This commit makes a minor refactoring that replaces `uniqueBySpan` with
`ts.sortAndDeduplicateDiagnostics()`.

PR Close #35086
This commit is contained in:
Keen Yee Liau 2020-01-31 13:22:18 -08:00 committed by Miško Hevery
parent c8584e5d36
commit a8609ba0ad
2 changed files with 7 additions and 30 deletions

View File

@ -228,25 +228,3 @@ export function ngDiagnosticToTsDiagnostic(
source: 'ng', source: 'ng',
}; };
} }
/**
* Return elements filtered by unique span.
* @param elements
*/
export function uniqueBySpan<T extends{span: ng.Span}>(elements: T[]): T[] {
const result: T[] = [];
const map = new Map<number, Set<number>>();
for (const element of elements) {
const {span} = element;
let set = map.get(span.start);
if (!set) {
set = new Set();
map.set(span.start, set);
}
if (!set.has(span.end)) {
set.add(span.end);
result.push(element);
}
}
return result;
}

View File

@ -10,7 +10,7 @@ import * as tss from 'typescript/lib/tsserverlibrary';
import {getTemplateCompletions} from './completions'; import {getTemplateCompletions} from './completions';
import {getDefinitionAndBoundSpan, getTsDefinitionAndBoundSpan} from './definitions'; import {getDefinitionAndBoundSpan, getTsDefinitionAndBoundSpan} from './definitions';
import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic, uniqueBySpan} from './diagnostics'; import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic} from './diagnostics';
import {getTemplateHover, getTsHover} from './hover'; import {getTemplateHover, getTsHover} from './hover';
import * as ng from './types'; import * as ng from './types';
import {TypeScriptServiceHost} from './typescript_host'; import {TypeScriptServiceHost} from './typescript_host';
@ -29,23 +29,22 @@ class LanguageServiceImpl implements ng.LanguageService {
getSemanticDiagnostics(fileName: string): tss.Diagnostic[] { getSemanticDiagnostics(fileName: string): tss.Diagnostic[] {
const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData' const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
const results: ng.Diagnostic[] = []; const ngDiagnostics: ng.Diagnostic[] = [];
const templates = this.host.getTemplates(fileName);
const templates = this.host.getTemplates(fileName);
for (const template of templates) { for (const template of templates) {
const ast = this.host.getTemplateAst(template); const ast = this.host.getTemplateAst(template);
if (ast) { if (ast) {
results.push(...getTemplateDiagnostics(ast)); ngDiagnostics.push(...getTemplateDiagnostics(ast));
} }
} }
const declarations = this.host.getDeclarations(fileName); const declarations = this.host.getDeclarations(fileName);
if (declarations && declarations.length) { ngDiagnostics.push(...getDeclarationDiagnostics(declarations, analyzedModules, this.host));
results.push(...getDeclarationDiagnostics(declarations, analyzedModules, this.host));
}
const sourceFile = fileName.endsWith('.ts') ? this.host.getSourceFile(fileName) : undefined; const sourceFile = fileName.endsWith('.ts') ? this.host.getSourceFile(fileName) : undefined;
return uniqueBySpan(results).map(d => ngDiagnosticToTsDiagnostic(d, sourceFile)); const tsDiagnostics = ngDiagnostics.map(d => ngDiagnosticToTsDiagnostic(d, sourceFile));
return [...tss.sortAndDeduplicateDiagnostics(tsDiagnostics)];
} }
getCompletionsAtPosition( getCompletionsAtPosition(