From 7101267923612b4a1bdf4386768fefdef2618f87 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Mon, 22 Feb 2021 14:14:31 -0800 Subject: [PATCH] refactor(language-service): add type guard for NgLanguageService (#40954) Type guard should be colocated with the `NgLanguageService` interface, not in `@angular/vscode-ng-language-service`. PR Close #40954 --- packages/language-service/api.ts | 5 +++++ packages/language-service/ivy/language_service.ts | 6 +++--- packages/language-service/ivy/ts_plugin.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/language-service/api.ts b/packages/language-service/api.ts index fce2ea8c74..603f82dbb5 100644 --- a/packages/language-service/api.ts +++ b/packages/language-service/api.ts @@ -43,3 +43,8 @@ export interface NgLanguageService extends ts.LanguageService { getTcb(fileName: string, position: number): GetTcbResponse|undefined; getComponentLocationsForTemplate(fileName: string): GetComponentLocationsForTemplateResponse; } + +export function isNgLanguageService(ls: ts.LanguageService| + NgLanguageService): ls is NgLanguageService { + return 'getTcb' in ls; +} diff --git a/packages/language-service/ivy/language_service.ts b/packages/language-service/ivy/language_service.ts index eaf6113fd0..e9defa5501 100644 --- a/packages/language-service/ivy/language_service.ts +++ b/packages/language-service/ivy/language_service.ts @@ -247,8 +247,8 @@ export class LanguageService { }); } - getTcb(fileName: string, position: number): GetTcbResponse { - return this.withCompiler(compiler => { + getTcb(fileName: string, position: number): GetTcbResponse|undefined { + return this.withCompiler(compiler => { const templateInfo = getTemplateInfoAtPosition(fileName, position, compiler); if (templateInfo === undefined) { return undefined; @@ -496,4 +496,4 @@ function findTightestNodeAtPosition(program: ts.Program, fileName: string, posit } return findTightestNode(sourceFile, position); -} \ No newline at end of file +} diff --git a/packages/language-service/ivy/ts_plugin.ts b/packages/language-service/ivy/ts_plugin.ts index 9acb7679e3..87b89c4ff5 100644 --- a/packages/language-service/ivy/ts_plugin.ts +++ b/packages/language-service/ivy/ts_plugin.ts @@ -130,7 +130,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService { return diagnostics; } - function getTcb(fileName: string, position: number): GetTcbResponse { + function getTcb(fileName: string, position: number): GetTcbResponse|undefined { return ngLS.getTcb(fileName, position); }