From 368576b045840aadd85413ddf90128b1d03d5b53 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 7 Jun 2021 21:05:21 +0200 Subject: [PATCH] refactor(language-service): ensure compatibility with noImplicitOverride (#42512) Adds the `override` keyword to the `language-service` sources to ensure compatibility with `noImplicitOverride`. PR Close #42512 --- .../language-service/ivy/template_target.ts | 15 ++++++++++++-- packages/language-service/src/completions.ts | 14 ++++++------- .../src/expression_diagnostics.ts | 20 +++++++++---------- packages/language-service/src/expressions.ts | 2 +- .../language-service/src/locate_symbol.ts | 8 ++++---- .../language-service/src/typescript_host.ts | 2 +- .../src/typescript_symbols.ts | 2 +- packages/language-service/src/utils.ts | 6 +++--- packages/language-service/test/mocks.ts | 2 +- 9 files changed, 41 insertions(+), 30 deletions(-) diff --git a/packages/language-service/ivy/template_target.ts b/packages/language-service/ivy/template_target.ts index ea14bfccc6..6f748603b3 100644 --- a/packages/language-service/ivy/template_target.ts +++ b/packages/language-service/ivy/template_target.ts @@ -142,11 +142,22 @@ export interface TwoWayBindingContext { nodes: [t.BoundAttribute, t.BoundEvent]; } +/** + * Special marker AST that can be used when the cursor is within the `sourceSpan` but not + * the key or value span of a node with key/value spans. + */ +class OutsideKeyValueMarkerAst extends e.AST { + visit(): null { + return null; + } +} + /** * This special marker is added to the path when the cursor is within the sourceSpan but not the key * or value span of a node with key/value spans. */ -const OUTSIDE_K_V_MARKER = new e.AST(new ParseSpan(-1, -1), new e.AbsoluteSourceSpan(-1, -1)); +const OUTSIDE_K_V_MARKER = + new OutsideKeyValueMarkerAst(new ParseSpan(-1, -1), new e.AbsoluteSourceSpan(-1, -1)); /** * Return the template AST node or expression AST node that most accurately @@ -415,7 +426,7 @@ class ExpressionVisitor extends e.RecursiveAstVisitor { super(); } - visit(node: e.AST, path: Array) { + override visit(node: e.AST, path: Array) { if (node instanceof e.ASTWithSource) { // In order to reduce noise, do not include `ASTWithSource` in the path. // For the purpose of source spans, there is no difference between diff --git a/packages/language-service/src/completions.ts b/packages/language-service/src/completions.ts index 6474911c12..378d204f76 100644 --- a/packages/language-service/src/completions.ts +++ b/packages/language-service/src/completions.ts @@ -408,23 +408,23 @@ class ExpressionVisitor extends NullTemplateVisitor { return Array.from(this.completions.values()); } - visitDirectiveProperty(ast: BoundDirectivePropertyAst): void { + override visitDirectiveProperty(ast: BoundDirectivePropertyAst): void { this.processExpressionCompletions(ast.value); } - visitElementProperty(ast: BoundElementPropertyAst): void { + override visitElementProperty(ast: BoundElementPropertyAst): void { this.processExpressionCompletions(ast.value); } - visitEvent(ast: BoundEventAst): void { + override visitEvent(ast: BoundEventAst): void { this.processExpressionCompletions(ast.handler); } - visitElement(): void { + override visitElement(): void { // no-op for now } - visitAttr(ast: AttrAst) { + override visitAttr(ast: AttrAst) { const binding = getBindingDescriptor(ast.name); if (binding && binding.kind === ATTR.KW_MICROSYNTAX) { // This a template binding given by micro syntax expression. @@ -459,7 +459,7 @@ class ExpressionVisitor extends NullTemplateVisitor { } } - visitReference(_ast: ReferenceAst, context: ElementAst) { + override visitReference(_ast: ReferenceAst, context: ElementAst) { context.directives.forEach(dir => { const {exportAs} = dir.directive; if (exportAs) { @@ -469,7 +469,7 @@ class ExpressionVisitor extends NullTemplateVisitor { }); } - visitBoundText(ast: BoundTextAst) { + override visitBoundText(ast: BoundTextAst) { if (inSpan(this.position, ast.value.sourceSpan)) { const completions = getExpressionCompletions( this.getExpressionScope(), ast.value, this.position, this.info.template); diff --git a/packages/language-service/src/expression_diagnostics.ts b/packages/language-service/src/expression_diagnostics.ts index bd6e66a961..0327a7daa7 100644 --- a/packages/language-service/src/expression_diagnostics.ts +++ b/packages/language-service/src/expression_diagnostics.ts @@ -42,11 +42,11 @@ function getReferences(info: ng.DiagnosticTemplateInfo): SymbolDeclaration[] { } const visitor = new class extends RecursiveTemplateAstVisitor { - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { + override visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { super.visitEmbeddedTemplate(ast, context); processReferences(ast.references); } - visitElement(ast: ElementAst, context: any): any { + override visitElement(ast: ElementAst, context: any): any { super.visitElement(ast, context); processReferences(ast.references); } @@ -257,38 +257,38 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor { this.path = new AstPath([]); } - visitDirective(ast: DirectiveAst, context: any): any { + override visitDirective(ast: DirectiveAst, context: any): any { // Override the default child visitor to ignore the host properties of a directive. if (ast.inputs && ast.inputs.length) { templateVisitAll(this, ast.inputs, context); } } - visitBoundText(ast: BoundTextAst): void { + override visitBoundText(ast: BoundTextAst): void { this.push(ast); this.diagnoseExpression(ast.value, ast.sourceSpan.start.offset, false); this.pop(); } - visitDirectiveProperty(ast: BoundDirectivePropertyAst): void { + override visitDirectiveProperty(ast: BoundDirectivePropertyAst): void { this.push(ast); this.diagnoseExpression(ast.value, this.attributeValueLocation(ast), false); this.pop(); } - visitElementProperty(ast: BoundElementPropertyAst): void { + override visitElementProperty(ast: BoundElementPropertyAst): void { this.push(ast); this.diagnoseExpression(ast.value, this.attributeValueLocation(ast), false); this.pop(); } - visitEvent(ast: BoundEventAst): void { + override visitEvent(ast: BoundEventAst): void { this.push(ast); this.diagnoseExpression(ast.handler, this.attributeValueLocation(ast), true); this.pop(); } - visitVariable(ast: VariableAst): void { + override visitVariable(ast: VariableAst): void { const directive = this.directiveSummary; if (directive && ast.value) { const context = this.info.query.getTemplateContext(directive.type.reference)!; @@ -304,13 +304,13 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor { } } - visitElement(ast: ElementAst, context: any): void { + override visitElement(ast: ElementAst, context: any): void { this.push(ast); super.visitElement(ast, context); this.pop(); } - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { + override visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { const previousDirectiveSummary = this.directiveSummary; this.push(ast); diff --git a/packages/language-service/src/expressions.ts b/packages/language-service/src/expressions.ts index be315c9bbc..6f6ca46d83 100644 --- a/packages/language-service/src/expressions.ts +++ b/packages/language-service/src/expressions.ts @@ -17,7 +17,7 @@ type AstPath = AstPathBase; function findAstAt(ast: AST, position: number, excludeEmpty: boolean = false): AstPath { const path: AST[] = []; const visitor = new class extends RecursiveAstVisitor { - visit(ast: AST) { + override visit(ast: AST) { if ((!excludeEmpty || ast.sourceSpan.start < ast.sourceSpan.end) && inSpan(position, ast.sourceSpan)) { const isNotNarrower = path.length && !isNarrower(ast.span, path[path.length - 1].span); diff --git a/packages/language-service/src/locate_symbol.ts b/packages/language-service/src/locate_symbol.ts index 1017117fb3..aa4d308b24 100644 --- a/packages/language-service/src/locate_symbol.ts +++ b/packages/language-service/src/locate_symbol.ts @@ -258,28 +258,28 @@ function findParentOfBinding( } } - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { + override visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { return this.visitChildren(context, visit => { visit(ast.directives); visit(ast.children); }); } - visitElement(ast: ElementAst, context: any): any { + override visitElement(ast: ElementAst, context: any): any { return this.visitChildren(context, visit => { visit(ast.directives); visit(ast.children); }); } - visitDirective(ast: DirectiveAst) { + override visitDirective(ast: DirectiveAst) { const result = this.visitChildren(ast, visit => { visit(ast.inputs); }); return result; } - visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: DirectiveAst) { + override visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: DirectiveAst) { if (ast === binding) { res = context; } diff --git a/packages/language-service/src/typescript_host.ts b/packages/language-service/src/typescript_host.ts index f3d24ca88a..14b92d03f4 100644 --- a/packages/language-service/src/typescript_host.ts +++ b/packages/language-service/src/typescript_host.ts @@ -23,7 +23,7 @@ import {AstResult, Declaration, DeclarationError, DiagnosticMessageChain, Langua * syntactically incorrect templates. */ export class DummyHtmlParser extends HtmlParser { - parse(): ParseTreeResult { + override parse(): ParseTreeResult { return new ParseTreeResult([], []); } } diff --git a/packages/language-service/src/typescript_symbols.ts b/packages/language-service/src/typescript_symbols.ts index 7d108b1a17..12f9eb0d9e 100644 --- a/packages/language-service/src/typescript_symbols.ts +++ b/packages/language-service/src/typescript_symbols.ts @@ -351,7 +351,7 @@ class TypeWrapper implements Symbol { // If stringIndexType a primitive type(e.g. 'string'), the Symbol is undefined; // and in AstType.resolvePropertyRead method, the Symbol.type should get the right type. class StringIndexTypeWrapper extends TypeWrapper { - public readonly type = new TypeWrapper(this.tsType, this.context); + public override readonly type = new TypeWrapper(this.tsType, this.context); } class SymbolWrapper implements Symbol { diff --git a/packages/language-service/src/utils.ts b/packages/language-service/src/utils.ts index 70ca4caa38..5eddc26584 100644 --- a/packages/language-service/src/utils.ts +++ b/packages/language-service/src/utils.ts @@ -107,7 +107,7 @@ export function findTemplateAstAt(ast: TemplateAst[], position: number): Templat } } - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { + override visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { return this.visitChildren(context, visit => { // Ignore reference, variable and providers visit(ast.attrs); @@ -116,7 +116,7 @@ export function findTemplateAstAt(ast: TemplateAst[], position: number): Templat }); } - visitElement(ast: ElementAst, context: any): any { + override visitElement(ast: ElementAst, context: any): any { return this.visitChildren(context, visit => { // Ingnore providers visit(ast.attrs); @@ -128,7 +128,7 @@ export function findTemplateAstAt(ast: TemplateAst[], position: number): Templat }); } - visitDirective(ast: DirectiveAst, context: any): any { + override visitDirective(ast: DirectiveAst, context: any): any { // Ignore the host properties of a directive const result = this.visitChildren(context, visit => { visit(ast.inputs); diff --git a/packages/language-service/test/mocks.ts b/packages/language-service/test/mocks.ts index 45f3ba6a6e..56611fce47 100644 --- a/packages/language-service/test/mocks.ts +++ b/packages/language-service/test/mocks.ts @@ -180,7 +180,7 @@ export class DiagnosticContext { }; const urlResolver = createOfflineCompileUrlResolver(); const htmlParser = new class extends HtmlParser { - parse(): ParseTreeResult { + override parse(): ParseTreeResult { return new ParseTreeResult([], []); } };