From 75fc89384d8db4d5cc0333ba608200ae58e95aa9 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 1 Dec 2020 09:39:27 -0800 Subject: [PATCH] refactor(compiler-cli): expose TTC method to determine if file is tracked shim (#39768) The Language Service "find references" currently uses the `ngtypecheck.ts` suffix to determine if a file is a shim file. Instead, a better API would be to expose a method in the template type checker that does this verification so that the LS does not have to "know" about the typecheck suffix. This also fixes an issue (albeit unlikely) whereby a file in the user's program that _actually_ is named with the `ngtypecheck.ts` suffix would have been interpreted as a shim file. PR Close #39768 --- packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts | 7 +++++++ packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts | 4 ++++ packages/language-service/ivy/references.ts | 4 +--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts b/packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts index 25ce5e7c5e..a6bc3c4c40 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts @@ -7,6 +7,7 @@ */ import {AST, ParseError, TmplAstNode, TmplAstTemplate} from '@angular/compiler'; +import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system'; import * as ts from 'typescript'; import {FullTemplateMapping} from './api'; @@ -87,6 +88,12 @@ export interface TemplateTypeChecker { */ generateAllTypeCheckBlocks(): void; + /** + * Returns `true` if the given file is in the record of known shims generated by the compiler, + * `false` if we cannot find the file in the shim records. + */ + isTrackedTypeCheckFile(filePath: AbsoluteFsPath): boolean; + /** * Retrieve the top-level node representing the TCB for the given component. * diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts index ca5cb27478..cd009e34ef 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts @@ -172,6 +172,10 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker { return {nodes}; } + isTrackedTypeCheckFile(filePath: AbsoluteFsPath): boolean { + return this.getFileAndShimRecordsForPath(filePath) !== null; + } + private getFileAndShimRecordsForPath(shimPath: AbsoluteFsPath): {fileRecord: FileTypeCheckingData, shimRecord: ShimTypeCheckingData}|null { for (const fileRecord of this.state.values()) { diff --git a/packages/language-service/ivy/references.ts b/packages/language-service/ivy/references.ts index 2fc5170023..ad287cec66 100644 --- a/packages/language-service/ivy/references.ts +++ b/packages/language-service/ivy/references.ts @@ -103,9 +103,7 @@ export class ReferenceBuilder { const entries: ts.ReferenceEntry[] = []; for (const ref of refs) { - // TODO(atscott): Determine if a file is a shim file in a more robust way and make the API - // available in an appropriate location. - if (ref.fileName.endsWith('ngtypecheck.ts')) { + if (this.ttc.isTrackedTypeCheckFile(absoluteFrom(ref.fileName))) { const entry = convertToTemplateReferenceEntry(ref, this.ttc); if (entry !== null) { entries.push(entry);