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
This commit is contained in:
Andrew Scott 2020-12-01 09:39:27 -08:00 committed by Misko Hevery
parent 06a782a2e3
commit 75fc89384d
3 changed files with 12 additions and 3 deletions

View File

@ -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.
*

View File

@ -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()) {

View File

@ -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);