2016-11-22 09:10:23 -08:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-11-22 09:10:23 -08:00
|
|
|
*
|
|
|
|
* 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-04-03 20:57:39 -07:00
|
|
|
import * as ts from 'typescript'; // used as value and is provided at runtime
|
2020-04-08 20:05:32 -07:00
|
|
|
|
2020-01-14 18:54:40 -08:00
|
|
|
import {locateSymbols} from './locate_symbol';
|
2020-04-08 20:05:32 -07:00
|
|
|
import {AstResult, Span} from './types';
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2019-08-01 13:07:32 -07:00
|
|
|
/**
|
|
|
|
* Convert Angular Span to TypeScript TextSpan. Angular Span has 'start' and
|
|
|
|
* 'end' whereas TS TextSpan has 'start' and 'length'.
|
|
|
|
* @param span Angular Span
|
|
|
|
*/
|
|
|
|
function ngSpanToTsTextSpan(span: Span): ts.TextSpan {
|
|
|
|
return {
|
|
|
|
start: span.start,
|
|
|
|
length: span.end - span.start,
|
|
|
|
};
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-07-31 10:55:45 -07:00
|
|
|
|
2019-08-21 14:36:00 -07:00
|
|
|
/**
|
|
|
|
* Traverse the template AST and look for the symbol located at `position`, then
|
|
|
|
* return its definition and span of bound text.
|
|
|
|
* @param info
|
|
|
|
* @param position
|
|
|
|
*/
|
|
|
|
export function getDefinitionAndBoundSpan(
|
|
|
|
info: AstResult, position: number): ts.DefinitionInfoAndBoundSpan|undefined {
|
2020-01-14 18:54:40 -08:00
|
|
|
const symbols = locateSymbols(info, position);
|
|
|
|
if (!symbols.length) {
|
2019-08-01 13:07:32 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-01-14 18:54:40 -08:00
|
|
|
|
2020-01-27 08:13:37 -08:00
|
|
|
const seen = new Set<string>();
|
2020-01-14 18:54:40 -08:00
|
|
|
const definitions: ts.DefinitionInfo[] = [];
|
|
|
|
for (const symbolInfo of symbols) {
|
|
|
|
const {symbol} = symbolInfo;
|
|
|
|
|
2019-08-01 13:07:32 -07:00
|
|
|
// symbol.definition is really the locations of the symbol. There could be
|
|
|
|
// more than one. No meaningful info could be provided without any location.
|
2020-01-14 18:54:40 -08:00
|
|
|
const {kind, name, container, definition: locations} = symbol;
|
|
|
|
if (!locations || !locations.length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const containerKind =
|
|
|
|
container ? container.kind as ts.ScriptElementKind : ts.ScriptElementKind.unknown;
|
|
|
|
const containerName = container ? container.name : '';
|
2020-01-27 08:13:37 -08:00
|
|
|
|
|
|
|
for (const {fileName, span} of locations) {
|
|
|
|
const textSpan = ngSpanToTsTextSpan(span);
|
|
|
|
// In cases like two-way bindings, a request for the definitions of an expression may return
|
|
|
|
// two of the same definition:
|
|
|
|
// [(ngModel)]="prop"
|
|
|
|
// ^^^^ -- one definition for the property binding, one for the event binding
|
|
|
|
// To prune duplicate definitions, tag definitions with unique location signatures and ignore
|
|
|
|
// definitions whose locations have already been seen.
|
|
|
|
const signature = `${textSpan.start}:${textSpan.length}@${fileName}`;
|
|
|
|
if (seen.has(signature)) continue;
|
|
|
|
|
|
|
|
definitions.push({
|
2020-01-14 18:54:40 -08:00
|
|
|
kind: kind as ts.ScriptElementKind,
|
|
|
|
name,
|
|
|
|
containerKind,
|
|
|
|
containerName,
|
2020-01-27 08:13:37 -08:00
|
|
|
textSpan: ngSpanToTsTextSpan(span),
|
|
|
|
fileName: fileName,
|
|
|
|
});
|
|
|
|
seen.add(signature);
|
|
|
|
}
|
2019-08-01 13:07:32 -07:00
|
|
|
}
|
2020-01-14 18:54:40 -08:00
|
|
|
|
2019-07-31 10:55:45 -07:00
|
|
|
return {
|
2020-01-23 11:34:27 -08:00
|
|
|
definitions,
|
|
|
|
textSpan: symbols[0].span,
|
2019-07-31 10:55:45 -07:00
|
|
|
};
|
|
|
|
}
|