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-05-09 19:16:50 -04:00
|
|
|
import {AstPath, CompileDirectiveSummary, CompileTypeMetadata, CssSelector, DirectiveAst, ElementAst, EmbeddedTemplateAst, HtmlAstPath, Node as HtmlNode, ParseSourceSpan, RecursiveTemplateAstVisitor, RecursiveVisitor, TemplateAst, TemplateAstPath, identifierName, templateVisitAll, visitAll} from '@angular/compiler';
|
2017-08-15 20:04:45 -04:00
|
|
|
import {DiagnosticTemplateInfo} from '@angular/compiler-cli/src/language_services';
|
2017-04-14 19:04:59 -04:00
|
|
|
import * as ts from 'typescript';
|
2017-05-09 19:16:50 -04:00
|
|
|
|
2016-11-22 12:10:23 -05:00
|
|
|
import {SelectorInfo, TemplateInfo} from './common';
|
|
|
|
import {Span} from './types';
|
|
|
|
|
|
|
|
export interface SpanHolder {
|
|
|
|
sourceSpan: ParseSourceSpan;
|
2017-03-24 12:57:32 -04:00
|
|
|
endSourceSpan?: ParseSourceSpan|null;
|
2016-11-22 12:10:23 -05:00
|
|
|
children?: SpanHolder[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isParseSourceSpan(value: any): value is ParseSourceSpan {
|
|
|
|
return value && !!value.start;
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:10:30 -04:00
|
|
|
export function spanOf(span: SpanHolder): Span;
|
|
|
|
export function spanOf(span: ParseSourceSpan): Span;
|
|
|
|
export function spanOf(span: SpanHolder | ParseSourceSpan | undefined): Span|undefined;
|
2017-03-24 12:57:32 -04:00
|
|
|
export function spanOf(span?: SpanHolder | ParseSourceSpan): Span|undefined {
|
2016-11-22 12:10:23 -05:00
|
|
|
if (!span) return undefined;
|
|
|
|
if (isParseSourceSpan(span)) {
|
|
|
|
return {start: span.start.offset, end: span.end.offset};
|
|
|
|
} else {
|
|
|
|
if (span.endSourceSpan) {
|
|
|
|
return {start: span.sourceSpan.start.offset, end: span.endSourceSpan.end.offset};
|
|
|
|
} else if (span.children && span.children.length) {
|
|
|
|
return {
|
|
|
|
start: span.sourceSpan.start.offset,
|
2017-03-24 12:57:32 -04:00
|
|
|
end: spanOf(span.children[span.children.length - 1]) !.end
|
2016-11-22 12:10:23 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {start: span.sourceSpan.start.offset, end: span.sourceSpan.end.offset};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function inSpan(position: number, span?: Span, exclusive?: boolean): boolean {
|
2017-04-28 18:10:30 -04:00
|
|
|
return span != null && (exclusive ? position >= span.start && position < span.end :
|
|
|
|
position >= span.start && position <= span.end);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function offsetSpan(span: Span, amount: number): Span {
|
|
|
|
return {start: span.start + amount, end: span.end + amount};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isNarrower(spanA: Span, spanB: Span): boolean {
|
|
|
|
return spanA.start >= spanB.start && spanA.end <= spanB.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasTemplateReference(type: CompileTypeMetadata): boolean {
|
|
|
|
if (type.diDeps) {
|
|
|
|
for (let diDep of type.diDeps) {
|
2017-04-28 18:10:30 -04:00
|
|
|
if (diDep.token && diDep.token.identifier &&
|
|
|
|
identifierName(diDep.token !.identifier !) == 'TemplateRef')
|
2016-11-23 12:42:19 -05:00
|
|
|
return true;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectors(info: TemplateInfo): SelectorInfo {
|
|
|
|
const map = new Map<CssSelector, CompileDirectiveSummary>();
|
2017-03-24 12:57:32 -04:00
|
|
|
const selectors: CssSelector[] = flatten(info.directives.map(directive => {
|
|
|
|
const selectors: CssSelector[] = CssSelector.parse(directive.selector !);
|
2016-11-22 12:10:23 -05:00
|
|
|
selectors.forEach(selector => map.set(selector, directive));
|
|
|
|
return selectors;
|
|
|
|
}));
|
|
|
|
return {selectors, map};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function flatten<T>(a: T[][]) {
|
|
|
|
return (<T[]>[]).concat(...a);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeSuffix(value: string, suffix: string) {
|
|
|
|
if (value.endsWith(suffix)) return value.substring(0, value.length - suffix.length);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function uniqueByName < T extends {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
> (elements: T[] | undefined): T[]|undefined {
|
|
|
|
if (elements) {
|
|
|
|
const result: T[] = [];
|
|
|
|
const set = new Set<string>();
|
|
|
|
for (const element of elements) {
|
|
|
|
if (!set.has(element.name)) {
|
|
|
|
set.add(element.name);
|
|
|
|
result.push(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 19:04:59 -04:00
|
|
|
|
|
|
|
export function isTypescriptVersion(low: string, high?: string) {
|
|
|
|
const version = ts.version;
|
|
|
|
|
|
|
|
if (version.substring(0, low.length) < low) return false;
|
|
|
|
|
|
|
|
if (high && (version.substring(0, high.length) > high)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-09 19:16:50 -04:00
|
|
|
|
|
|
|
export function diagnosticInfoFromTemplateInfo(info: TemplateInfo): DiagnosticTemplateInfo {
|
|
|
|
return {
|
|
|
|
fileName: info.fileName,
|
|
|
|
offset: info.template.span.start,
|
|
|
|
query: info.template.query,
|
|
|
|
members: info.template.members,
|
|
|
|
htmlAst: info.htmlAst,
|
|
|
|
templateAst: info.templateAst
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findTemplateAstAt(
|
|
|
|
ast: TemplateAst[], position: number, allowWidening: boolean = false): TemplateAstPath {
|
|
|
|
const path: TemplateAst[] = [];
|
|
|
|
const visitor = new class extends RecursiveTemplateAstVisitor {
|
|
|
|
visit(ast: TemplateAst, context: any): any {
|
|
|
|
let span = spanOf(ast);
|
|
|
|
if (inSpan(position, span)) {
|
|
|
|
const len = path.length;
|
|
|
|
if (!len || allowWidening || isNarrower(span, spanOf(path[len - 1]))) {
|
|
|
|
path.push(ast);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Returning a value here will result in the children being skipped.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any {
|
|
|
|
return this.visitChildren(context, visit => {
|
|
|
|
// Ignore reference, variable and providers
|
|
|
|
visit(ast.attrs);
|
|
|
|
visit(ast.directives);
|
|
|
|
visit(ast.children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
visitElement(ast: ElementAst, context: any): any {
|
|
|
|
return this.visitChildren(context, visit => {
|
|
|
|
// Ingnore providers
|
|
|
|
visit(ast.attrs);
|
|
|
|
visit(ast.inputs);
|
|
|
|
visit(ast.outputs);
|
|
|
|
visit(ast.references);
|
|
|
|
visit(ast.directives);
|
|
|
|
visit(ast.children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
visitDirective(ast: DirectiveAst, context: any): any {
|
|
|
|
// Ignore the host properties of a directive
|
|
|
|
const result = this.visitChildren(context, visit => { visit(ast.inputs); });
|
|
|
|
// We never care about the diretive itself, just its inputs.
|
|
|
|
if (path[path.length - 1] == ast) {
|
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
templateVisitAll(visitor, ast);
|
|
|
|
|
|
|
|
return new AstPath<TemplateAst>(path, position);
|
|
|
|
}
|