2020-11-19 16:31:34 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2020-12-09 13:09:55 -05:00
|
|
|
import {TmplAstBoundAttribute, TmplAstTextAttribute, TmplAstVariable} from '@angular/compiler';
|
2020-11-19 16:31:34 -05:00
|
|
|
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
|
|
|
|
import {absoluteFrom, absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
|
2020-12-09 13:09:55 -05:00
|
|
|
import {DirectiveSymbol, SymbolKind, TemplateTypeChecker, TypeCheckingProgramStrategy} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
|
2020-12-16 14:13:34 -05:00
|
|
|
import {ExpressionIdentifier, hasExpressionIdentifier} from '@angular/compiler-cli/src/ngtsc/typecheck/src/comments';
|
2020-11-19 16:31:34 -05:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2020-12-16 17:39:49 -05:00
|
|
|
import {getTargetAtPosition, TargetNodeKind} from './template_target';
|
2020-12-16 14:13:34 -05:00
|
|
|
import {findTightestNode} from './ts_utils';
|
2020-12-09 13:09:55 -05:00
|
|
|
import {getDirectiveMatchesForAttribute, getDirectiveMatchesForElementTag, getTemplateInfoAtPosition, isWithin, TemplateInfo, toTextSpan} from './utils';
|
2020-11-19 16:31:34 -05:00
|
|
|
|
|
|
|
export class ReferenceBuilder {
|
|
|
|
private readonly ttc = this.compiler.getTemplateTypeChecker();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly strategy: TypeCheckingProgramStrategy,
|
|
|
|
private readonly tsLS: ts.LanguageService, private readonly compiler: NgCompiler) {}
|
|
|
|
|
|
|
|
get(filePath: string, position: number): ts.ReferenceEntry[]|undefined {
|
|
|
|
this.ttc.generateAllTypeCheckBlocks();
|
|
|
|
const templateInfo = getTemplateInfoAtPosition(filePath, position, this.compiler);
|
|
|
|
return templateInfo !== undefined ?
|
|
|
|
this.getReferencesAtTemplatePosition(templateInfo, position) :
|
|
|
|
this.getReferencesAtTypescriptPosition(filePath, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getReferencesAtTemplatePosition({template, component}: TemplateInfo, position: number):
|
|
|
|
ts.ReferenceEntry[]|undefined {
|
|
|
|
// Find the AST node in the template at the position.
|
|
|
|
const positionDetails = getTargetAtPosition(template, position);
|
|
|
|
if (positionDetails === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:43:53 -05:00
|
|
|
const nodes = positionDetails.context.kind === TargetNodeKind.TwoWayBindingContext ?
|
|
|
|
positionDetails.context.nodes :
|
|
|
|
[positionDetails.context.node];
|
2020-11-18 17:07:30 -05:00
|
|
|
|
2020-12-17 17:43:53 -05:00
|
|
|
const references: ts.ReferenceEntry[] = [];
|
|
|
|
for (const node of nodes) {
|
|
|
|
// Get the information about the TCB at the template position.
|
|
|
|
const symbol = this.ttc.getSymbolOfNode(node, component);
|
|
|
|
if (symbol === null) {
|
|
|
|
continue;
|
2020-12-09 13:09:55 -05:00
|
|
|
}
|
2020-12-17 17:43:53 -05:00
|
|
|
|
|
|
|
switch (symbol.kind) {
|
|
|
|
case SymbolKind.Directive:
|
|
|
|
case SymbolKind.Template:
|
|
|
|
// References to elements, templates, and directives will be through template references
|
|
|
|
// (#ref). They shouldn't be used directly for a Language Service reference request.
|
|
|
|
break;
|
|
|
|
case SymbolKind.Element: {
|
|
|
|
const matches = getDirectiveMatchesForElementTag(symbol.templateNode, symbol.directives);
|
|
|
|
references.push(...this.getReferencesForDirectives(matches) ?? []);
|
|
|
|
break;
|
2020-12-09 13:09:55 -05:00
|
|
|
}
|
2020-12-17 17:43:53 -05:00
|
|
|
case SymbolKind.DomBinding: {
|
|
|
|
// Dom bindings aren't currently type-checked (see `checkTypeOfDomBindings`) so they don't
|
|
|
|
// have a shim location. This means we can't match dom bindings to their lib.dom
|
|
|
|
// reference, but we can still see if they match to a directive.
|
|
|
|
if (!(node instanceof TmplAstTextAttribute) && !(node instanceof TmplAstBoundAttribute)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const directives = getDirectiveMatchesForAttribute(
|
|
|
|
node.name, symbol.host.templateNode, symbol.host.directives);
|
|
|
|
references.push(...this.getReferencesForDirectives(directives) ?? []);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind.Reference: {
|
|
|
|
const {shimPath, positionInShimFile} = symbol.referenceVarLocation;
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, positionInShimFile) ?? []);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind.Variable: {
|
|
|
|
const {positionInShimFile: initializerPosition, shimPath} = symbol.initializerLocation;
|
|
|
|
const localVarPosition = symbol.localVarLocation.positionInShimFile;
|
2020-11-19 16:31:34 -05:00
|
|
|
|
2020-12-17 17:43:53 -05:00
|
|
|
if ((node instanceof TmplAstVariable)) {
|
|
|
|
if (node.valueSpan !== undefined && isWithin(position, node.valueSpan)) {
|
|
|
|
// In the valueSpan of the variable, we want to get the reference of the initializer.
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, initializerPosition) ?? []);
|
|
|
|
} else if (isWithin(position, node.keySpan)) {
|
|
|
|
// In the keySpan of the variable, we want to get the reference of the local variable.
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, localVarPosition) ?? []);
|
|
|
|
}
|
2020-11-19 16:31:34 -05:00
|
|
|
} else {
|
2020-12-17 17:43:53 -05:00
|
|
|
// If the templateNode is not the `TmplAstVariable`, it must be a usage of the variable
|
|
|
|
// somewhere in the template.
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, localVarPosition) ?? []);
|
2020-11-19 16:31:34 -05:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:43:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind.Input:
|
|
|
|
case SymbolKind.Output: {
|
|
|
|
// TODO(atscott): Determine how to handle when the binding maps to several inputs/outputs
|
|
|
|
const {shimPath, positionInShimFile} = symbol.bindings[0].shimLocation;
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, positionInShimFile) ?? []);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind.Pipe:
|
|
|
|
case SymbolKind.Expression: {
|
|
|
|
const {shimPath, positionInShimFile} = symbol.shimLocation;
|
|
|
|
references.push(
|
|
|
|
...this.getReferencesAtTypescriptPosition(shimPath, positionInShimFile) ?? []);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-19 16:31:34 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 17:43:53 -05:00
|
|
|
if (references.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return references;
|
2020-11-19 16:31:34 -05:00
|
|
|
}
|
|
|
|
|
2020-12-09 13:09:55 -05:00
|
|
|
private getReferencesForDirectives(directives: Set<DirectiveSymbol>):
|
|
|
|
ts.ReferenceEntry[]|undefined {
|
|
|
|
const allDirectiveRefs: ts.ReferenceEntry[] = [];
|
|
|
|
for (const dir of directives.values()) {
|
|
|
|
const dirClass = dir.tsSymbol.valueDeclaration;
|
|
|
|
if (dirClass === undefined || !ts.isClassDeclaration(dirClass) ||
|
|
|
|
dirClass.name === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dirFile = dirClass.getSourceFile().fileName;
|
|
|
|
const dirPosition = dirClass.name.getStart();
|
|
|
|
const directiveRefs = this.getReferencesAtTypescriptPosition(dirFile, dirPosition);
|
|
|
|
if (directiveRefs !== undefined) {
|
|
|
|
allDirectiveRefs.push(...directiveRefs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allDirectiveRefs.length > 0 ? allDirectiveRefs : undefined;
|
|
|
|
}
|
|
|
|
|
2020-11-19 16:31:34 -05:00
|
|
|
private getReferencesAtTypescriptPosition(fileName: string, position: number):
|
|
|
|
ts.ReferenceEntry[]|undefined {
|
|
|
|
const refs = this.tsLS.getReferencesAtPosition(fileName, position);
|
|
|
|
if (refs === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const entries: ts.ReferenceEntry[] = [];
|
|
|
|
for (const ref of refs) {
|
2020-12-01 12:39:27 -05:00
|
|
|
if (this.ttc.isTrackedTypeCheckFile(absoluteFrom(ref.fileName))) {
|
2020-12-16 14:13:34 -05:00
|
|
|
const entry = this.convertToTemplateReferenceEntry(ref, this.ttc);
|
2020-11-19 16:31:34 -05:00
|
|
|
if (entry !== null) {
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entries.push(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:13:34 -05:00
|
|
|
private convertToTemplateReferenceEntry(
|
|
|
|
shimReferenceEntry: ts.ReferenceEntry,
|
|
|
|
templateTypeChecker: TemplateTypeChecker): ts.ReferenceEntry|null {
|
|
|
|
const sf = this.strategy.getProgram().getSourceFile(shimReferenceEntry.fileName);
|
|
|
|
if (sf === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const tcbNode = findTightestNode(sf, shimReferenceEntry.textSpan.start);
|
|
|
|
if (tcbNode === undefined ||
|
|
|
|
hasExpressionIdentifier(sf, tcbNode, ExpressionIdentifier.EVENT_PARAMETER)) {
|
|
|
|
// If the reference result is the $event parameter in the subscribe/addEventListener function
|
|
|
|
// in the TCB, we want to filter this result out of the references. We really only want to
|
|
|
|
// return references to the parameter in the template itself.
|
|
|
|
return null;
|
|
|
|
}
|
2020-11-19 16:31:34 -05:00
|
|
|
|
2020-12-16 14:13:34 -05:00
|
|
|
// TODO(atscott): Determine how to consistently resolve paths. i.e. with the project serverHost
|
|
|
|
// or LSParseConfigHost in the adapter. We should have a better defined way to normalize paths.
|
|
|
|
const mapping = templateTypeChecker.getTemplateMappingAtShimLocation({
|
|
|
|
shimPath: absoluteFrom(shimReferenceEntry.fileName),
|
|
|
|
positionInShimFile: shimReferenceEntry.textSpan.start,
|
|
|
|
});
|
|
|
|
if (mapping === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const {templateSourceMapping, span} = mapping;
|
|
|
|
|
|
|
|
let templateUrl: AbsoluteFsPath;
|
|
|
|
if (templateSourceMapping.type === 'direct') {
|
|
|
|
templateUrl = absoluteFromSourceFile(templateSourceMapping.node.getSourceFile());
|
|
|
|
} else if (templateSourceMapping.type === 'external') {
|
|
|
|
templateUrl = absoluteFrom(templateSourceMapping.templateUrl);
|
|
|
|
} else {
|
|
|
|
// This includes indirect mappings, which are difficult to map directly to the code location.
|
|
|
|
// Diagnostics similarly return a synthetic template string for this case rather than a real
|
|
|
|
// location.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...shimReferenceEntry,
|
|
|
|
fileName: templateUrl,
|
|
|
|
textSpan: toTextSpan(span),
|
|
|
|
};
|
|
|
|
}
|
2020-11-19 16:31:34 -05:00
|
|
|
}
|