2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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
|
|
|
|
*/
|
|
|
|
|
2017-01-27 20:39:48 -05:00
|
|
|
import {AST, Attribute, BoundDirectivePropertyAst, BoundEventAst, ElementAst, TemplateAst, tokenReference} from '@angular/compiler';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
import {TemplateInfo} from './common';
|
|
|
|
import {getExpressionScope, getExpressionSymbol} from './expressions';
|
|
|
|
import {HtmlAstPath} from './html_path';
|
|
|
|
import {TemplateAstPath} from './template_path';
|
|
|
|
import {Definition, Location, Span, Symbol, SymbolTable} from './types';
|
|
|
|
import {inSpan, offsetSpan, spanOf} from './utils';
|
|
|
|
|
|
|
|
export interface SymbolInfo {
|
|
|
|
symbol: Symbol;
|
|
|
|
span: Span;
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:57:32 -04:00
|
|
|
export function locateSymbol(info: TemplateInfo): SymbolInfo|undefined {
|
2017-04-28 18:10:30 -04:00
|
|
|
if (!info.position) return undefined;
|
|
|
|
const templatePosition = info.position - info.template.span.start;
|
2016-11-22 12:10:23 -05:00
|
|
|
const path = new TemplateAstPath(info.templateAst, templatePosition);
|
|
|
|
if (path.tail) {
|
2017-04-28 18:10:30 -04:00
|
|
|
let symbol: Symbol|undefined = undefined;
|
|
|
|
let span: Span|undefined = undefined;
|
2016-11-22 12:10:23 -05:00
|
|
|
const attributeValueSymbol = (ast: AST, inEvent: boolean = false): boolean => {
|
|
|
|
const attribute = findAttribute(info);
|
|
|
|
if (attribute) {
|
|
|
|
if (inSpan(templatePosition, spanOf(attribute.valueSpan))) {
|
|
|
|
const scope = getExpressionScope(info, path, inEvent);
|
2017-04-28 18:10:30 -04:00
|
|
|
if (attribute.valueSpan) {
|
|
|
|
const expressionOffset = attribute.valueSpan.start.offset + 1;
|
|
|
|
const result = getExpressionSymbol(
|
|
|
|
scope, ast, templatePosition - expressionOffset, info.template.query);
|
|
|
|
if (result) {
|
|
|
|
symbol = result.symbol;
|
|
|
|
span = offsetSpan(result.span, expressionOffset);
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
path.tail.visit(
|
|
|
|
{
|
|
|
|
visitNgContent(ast) {},
|
|
|
|
visitEmbeddedTemplate(ast) {},
|
|
|
|
visitElement(ast) {
|
|
|
|
const component = ast.directives.find(d => d.directive.isComponent);
|
|
|
|
if (component) {
|
|
|
|
symbol = info.template.query.getTypeSymbol(component.directive.type.reference);
|
|
|
|
symbol = symbol && new OverrideKindSymbol(symbol, 'component');
|
2017-04-28 18:10:30 -04:00
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
} else {
|
|
|
|
// Find a directive that matches the element name
|
2017-04-28 18:10:30 -04:00
|
|
|
const directive = ast.directives.find(
|
|
|
|
d => d.directive.selector != null && d.directive.selector.indexOf(ast.name) >= 0);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (directive) {
|
|
|
|
symbol = info.template.query.getTypeSymbol(directive.directive.type.reference);
|
|
|
|
symbol = symbol && new OverrideKindSymbol(symbol, 'directive');
|
2017-04-28 18:10:30 -04:00
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visitReference(ast) {
|
2016-11-23 12:42:19 -05:00
|
|
|
symbol = info.template.query.getTypeSymbol(tokenReference(ast.value));
|
2017-04-28 18:10:30 -04:00
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
},
|
|
|
|
visitVariable(ast) {},
|
|
|
|
visitEvent(ast) {
|
|
|
|
if (!attributeValueSymbol(ast.handler, /* inEvent */ true)) {
|
2017-04-28 18:10:30 -04:00
|
|
|
symbol = findOutputBinding(info, path, ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
symbol = symbol && new OverrideKindSymbol(symbol, 'event');
|
2017-04-28 18:10:30 -04:00
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
visitElementProperty(ast) { attributeValueSymbol(ast.value); },
|
|
|
|
visitAttr(ast) {},
|
|
|
|
visitBoundText(ast) {
|
|
|
|
const expressionPosition = templatePosition - ast.sourceSpan.start.offset;
|
|
|
|
if (inSpan(expressionPosition, ast.value.span)) {
|
|
|
|
const scope = getExpressionScope(info, path, /* includeEvent */ false);
|
|
|
|
const result =
|
|
|
|
getExpressionSymbol(scope, ast.value, expressionPosition, info.template.query);
|
|
|
|
if (result) {
|
|
|
|
symbol = result.symbol;
|
|
|
|
span = offsetSpan(result.span, ast.sourceSpan.start.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visitText(ast) {},
|
|
|
|
visitDirective(ast) {
|
|
|
|
symbol = info.template.query.getTypeSymbol(ast.directive.type.reference);
|
2017-04-28 18:10:30 -04:00
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
},
|
|
|
|
visitDirectiveProperty(ast) {
|
|
|
|
if (!attributeValueSymbol(ast.value)) {
|
2017-04-28 18:10:30 -04:00
|
|
|
symbol = findInputBinding(info, path, ast);
|
|
|
|
span = spanOf(ast);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null);
|
|
|
|
if (symbol && span) {
|
|
|
|
return {symbol, span: offsetSpan(span, info.template.span.start)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:57:32 -04:00
|
|
|
function findAttribute(info: TemplateInfo): Attribute|undefined {
|
2017-04-28 18:10:30 -04:00
|
|
|
if (info.position) {
|
|
|
|
const templatePosition = info.position - info.template.span.start;
|
|
|
|
const path = new HtmlAstPath(info.htmlAst, templatePosition);
|
|
|
|
return path.first(Attribute);
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function findInputBinding(
|
2017-03-24 12:57:32 -04:00
|
|
|
info: TemplateInfo, path: TemplateAstPath, binding: BoundDirectivePropertyAst): Symbol|
|
|
|
|
undefined {
|
2016-11-22 12:10:23 -05:00
|
|
|
const element = path.first(ElementAst);
|
|
|
|
if (element) {
|
|
|
|
for (const directive of element.directives) {
|
|
|
|
const invertedInput = invertMap(directive.directive.inputs);
|
|
|
|
const fieldName = invertedInput[binding.templateName];
|
|
|
|
if (fieldName) {
|
|
|
|
const classSymbol = info.template.query.getTypeSymbol(directive.directive.type.reference);
|
|
|
|
if (classSymbol) {
|
|
|
|
return classSymbol.members().get(fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findOutputBinding(
|
2017-03-24 12:57:32 -04:00
|
|
|
info: TemplateInfo, path: TemplateAstPath, binding: BoundEventAst): Symbol|undefined {
|
2016-11-22 12:10:23 -05:00
|
|
|
const element = path.first(ElementAst);
|
|
|
|
if (element) {
|
|
|
|
for (const directive of element.directives) {
|
|
|
|
const invertedOutputs = invertMap(directive.directive.outputs);
|
|
|
|
const fieldName = invertedOutputs[binding.name];
|
|
|
|
if (fieldName) {
|
|
|
|
const classSymbol = info.template.query.getTypeSymbol(directive.directive.type.reference);
|
|
|
|
if (classSymbol) {
|
|
|
|
return classSymbol.members().get(fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function invertMap(obj: {[name: string]: string}): {[name: string]: string} {
|
|
|
|
const result: {[name: string]: string} = {};
|
|
|
|
for (const name of Object.keys(obj)) {
|
|
|
|
const v = obj[name];
|
|
|
|
result[v] = name;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap a symbol and change its kind to component.
|
|
|
|
*/
|
|
|
|
class OverrideKindSymbol implements Symbol {
|
|
|
|
constructor(private sym: Symbol, private kindOverride: string) {}
|
|
|
|
|
|
|
|
get name(): string { return this.sym.name; }
|
|
|
|
|
|
|
|
get kind(): string { return this.kindOverride; }
|
|
|
|
|
|
|
|
get language(): string { return this.sym.language; }
|
|
|
|
|
|
|
|
get type(): Symbol|undefined { return this.sym.type; }
|
|
|
|
|
|
|
|
get container(): Symbol|undefined { return this.sym.container; }
|
|
|
|
|
|
|
|
get public(): boolean { return this.sym.public; }
|
|
|
|
|
|
|
|
get callable(): boolean { return this.sym.callable; }
|
|
|
|
|
|
|
|
get definition(): Definition { return this.sym.definition; }
|
|
|
|
|
|
|
|
members() { return this.sym.members(); }
|
|
|
|
|
|
|
|
signatures() { return this.sym.signatures(); }
|
|
|
|
|
|
|
|
selectSignature(types: Symbol[]) { return this.sym.selectSignature(types); }
|
|
|
|
|
|
|
|
indexed(argument: Symbol) { return this.sym.indexed(argument); }
|
2017-01-27 20:39:48 -05:00
|
|
|
}
|