From 852afb312aeb1c453177204288d296085a8aae68 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Wed, 28 Aug 2019 10:59:37 -0700 Subject: [PATCH] fix(language-service): Create DirectiveKind enum (#32376) Hovering over a selector, the QuickInfo display string is something like: ``` (component) AppComponent ``` where `component` is the symbol kind. Prior to this, there was no types to indicate the possible values of a symbol. This PR creates an enum to represent that. PR Close #32376 --- packages/language-service/src/locate_symbol.ts | 12 ++++++------ packages/language-service/src/types.ts | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/language-service/src/locate_symbol.ts b/packages/language-service/src/locate_symbol.ts index 01f27a25ac..827bc136c6 100644 --- a/packages/language-service/src/locate_symbol.ts +++ b/packages/language-service/src/locate_symbol.ts @@ -11,7 +11,7 @@ import {getExpressionScope} from '@angular/compiler-cli/src/language_services'; import {AstResult} from './common'; import {getExpressionSymbol} from './expressions'; -import {Definition, Span, Symbol} from './types'; +import {Definition, DirectiveKind, Span, Symbol} from './types'; import {diagnosticInfoFromTemplateInfo, findTemplateAstAt, inSpan, offsetSpan, spanOf} from './utils'; export interface SymbolInfo { @@ -58,7 +58,7 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde const component = ast.directives.find(d => d.directive.isComponent); if (component) { symbol = info.template.query.getTypeSymbol(component.directive.type.reference); - symbol = symbol && new OverrideKindSymbol(symbol, 'component'); + symbol = symbol && new OverrideKindSymbol(symbol, DirectiveKind.COMPONENT); span = spanOf(ast); } else { // Find a directive that matches the element name @@ -66,7 +66,7 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde d => d.directive.selector != null && d.directive.selector.indexOf(ast.name) >= 0); if (directive) { symbol = info.template.query.getTypeSymbol(directive.directive.type.reference); - symbol = symbol && new OverrideKindSymbol(symbol, 'directive'); + symbol = symbol && new OverrideKindSymbol(symbol, DirectiveKind.DIRECTIVE); span = spanOf(ast); } } @@ -79,7 +79,7 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde visitEvent(ast) { if (!attributeValueSymbol(ast.handler, /* inEvent */ true)) { symbol = findOutputBinding(info, path, ast); - symbol = symbol && new OverrideKindSymbol(symbol, 'event'); + symbol = symbol && new OverrideKindSymbol(symbol, DirectiveKind.EVENT); span = spanOf(ast); } }, @@ -170,8 +170,8 @@ function invertMap(obj: {[name: string]: string}): {[name: string]: string} { * Wrap a symbol and change its kind to component. */ class OverrideKindSymbol implements Symbol { - public readonly kind: string; - constructor(private sym: Symbol, kindOverride: string) { this.kind = kindOverride; } + public readonly kind: DirectiveKind; + constructor(private sym: Symbol, kindOverride: DirectiveKind) { this.kind = kindOverride; } get name(): string { return this.sym.name; } diff --git a/packages/language-service/src/types.ts b/packages/language-service/src/types.ts index 46ba469002..45e5d954c2 100644 --- a/packages/language-service/src/types.ts +++ b/packages/language-service/src/types.ts @@ -249,6 +249,15 @@ export enum DiagnosticKind { Warning, } +/** + * The type of Angular directive. Used for QuickInfo in template. + */ +export enum DirectiveKind { + COMPONENT = 'component', + DIRECTIVE = 'directive', + EVENT = 'event', +} + /** * A template diagnostics message chain. This is similar to the TypeScript * DiagnosticMessageChain. The messages are intended to be formatted as separate