-
Notifications
You must be signed in to change notification settings - Fork 234
/
ngWalker.ts
199 lines (170 loc) · 7.58 KB
/
ngWalker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import * as Lint from 'tslint';
import * as ts from 'typescript';
import * as compiler from '@angular/compiler';
import { parseTemplate } from './templates/templateParser';
import {parseCss} from './styles/parseCss';
import {CssAst} from './styles/cssAst';
import {BasicCssAstVisitor, CssAstVisitorCtrl} from './styles/basicCssAstVisitor';
import {RecursiveAngularExpressionVisitorCtr, BasicTemplateAstVisitor, TemplateAstVisitorCtr} from './templates/basicTemplateAstVisitor';
import {RecursiveAngularExpressionVisitor} from './templates/recursiveAngularExpressionVisitor';
import {ReferenceCollectorVisitor} from './templates/referenceCollectorVisitor';
import {MetadataReader} from './metadataReader';
import {ComponentMetadata, DirectiveMetadata, StyleMetadata} from './metadata';
import {ngWalkerFactoryUtils} from './ngWalkerFactoryUtils';
import {Config} from './config';
import {logger} from '../util/logger';
import {getDecoratorName} from '../util/utils';
const getDecoratorStringArgs = (decorator: ts.Decorator) => {
let baseExpr = <any>decorator.expression || {};
let args = baseExpr.arguments || [];
return args.map(a => (a.kind === ts.SyntaxKind.StringLiteral) ? a.text : null);
};
export interface NgWalkerConfig {
expressionVisitorCtrl?: RecursiveAngularExpressionVisitorCtr;
templateVisitorCtrl?: TemplateAstVisitorCtr;
cssVisitorCtrl?: CssAstVisitorCtrl;
}
export class NgWalker extends Lint.RuleWalker {
constructor(sourceFile: ts.SourceFile,
protected _originalOptions: Lint.IOptions,
private _config?: NgWalkerConfig,
protected _metadataReader?: MetadataReader) {
super(sourceFile, _originalOptions);
this._metadataReader = this._metadataReader || ngWalkerFactoryUtils.defaultMetadataReader();
this._config = Object.assign({
templateVisitorCtrl: BasicTemplateAstVisitor,
expressionVisitorCtrl: RecursiveAngularExpressionVisitor,
cssVisitorCtrl: BasicCssAstVisitor
}, this._config || {});
this._config = Object.assign({
templateVisitorCtrl: BasicTemplateAstVisitor,
expressionVisitorCtrl: RecursiveAngularExpressionVisitor,
cssVisitorCtrl: BasicCssAstVisitor
}, this._config || {});
// this._config = ngWalkerFactoryUtils.normalizeConfig(this._config);
}
visitClassDeclaration(declaration: ts.ClassDeclaration) {
const metadata = this._metadataReader.read(declaration);
if (metadata instanceof ComponentMetadata) {
this.visitNgComponent(metadata);
} else if (metadata instanceof DirectiveMetadata) {
this.visitNgDirective(metadata);
}
(<ts.Decorator[]>declaration.decorators || []).forEach(this.visitClassDecorator.bind(this));
super.visitClassDeclaration(declaration);
}
visitMethodDeclaration(method: ts.MethodDeclaration) {
(<ts.Decorator[]>method.decorators || []).forEach(this.visitMethodDecorator.bind(this));
super.visitMethodDeclaration(method);
}
visitPropertyDeclaration(prop: ts.PropertyDeclaration) {
(<ts.Decorator[]>prop.decorators || []).forEach(this.visitPropertyDecorator.bind(this));
super.visitPropertyDeclaration(prop);
}
protected visitMethodDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
if (name === 'HostListener') {
this.visitNgHostListener(<ts.MethodDeclaration>decorator.parent,
decorator, getDecoratorStringArgs(decorator));
}
}
protected visitPropertyDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
switch (name) {
case 'Input':
this.visitNgInput(<ts.PropertyDeclaration>decorator.parent,
decorator, getDecoratorStringArgs(decorator));
break;
case 'Output':
this.visitNgOutput(<ts.PropertyDeclaration>decorator.parent,
decorator, getDecoratorStringArgs(decorator));
break;
case 'HostBinding':
this.visitNgHostBinding(<ts.PropertyDeclaration>decorator.parent,
decorator, getDecoratorStringArgs(decorator));
break;
}
}
protected visitClassDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
// Not invoked @Component or @Pipe, or @Directive
if (!(<ts.CallExpression>decorator.expression).arguments ||
!(<ts.CallExpression>decorator.expression).arguments.length ||
!(<ts.ObjectLiteralExpression>(<ts.CallExpression>decorator.expression).arguments[0]).properties) {
return;
}
if (name === 'Pipe') {
this.visitNgPipe(<ts.ClassDeclaration>decorator.parent, decorator);
}
}
protected visitNgComponent(metadata: ComponentMetadata) {
const template = metadata.template;
const getPosition = (node: any) => {
let pos = 0;
if (node) {
pos = node.pos + 1;
try {
pos = node.getStart() + 1;
} catch (e) {}
}
return pos;
};
if (template && template.template) {
try {
const templateAst = parseTemplate(template.template.code, Config.predefinedDirectives);
this.visitNgTemplateHelper(templateAst, metadata, getPosition(template.node));
} catch (e) {
logger.error('Cannot parse the template of', ((<any>metadata.controller || {}).name || {}).text, e);
}
}
const styles = metadata.styles;
if (styles && styles.length) {
for (let i = 0; i < styles.length; i += 1) {
const style = styles[i];
try {
this.visitNgStyleHelper(parseCss(style.style.code), metadata, style, getPosition(style.node));
} catch (e) {
logger.error('Cannot parse the styles of', ((<any>metadata.controller || {}).name || {}).text, e);
}
}
}
}
protected visitNgDirective(metadata: DirectiveMetadata) {}
protected visitNgPipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {}
protected visitNgInput(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}
protected visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) {}
protected visitNgHostBinding(property: ts.PropertyDeclaration, decorator: ts.Decorator, args: string[]) {}
protected visitNgHostListener(method: ts.MethodDeclaration, decorator: ts.Decorator, args: string[]) {}
protected visitNgTemplateHelper(roots: compiler.TemplateAst[], context: ComponentMetadata, baseStart: number) {
if (!roots || !roots.length) {
return;
} else {
const sourceFile = this.getContextSourceFile(context.template.url, context.template.template.source);
const referenceVisitor = new ReferenceCollectorVisitor();
const visitor =
new this._config.templateVisitorCtrl(
sourceFile, this._originalOptions, context, baseStart, this._config.expressionVisitorCtrl);
compiler.templateVisitAll(referenceVisitor, roots, null);
visitor._variables = referenceVisitor.variables;
roots.forEach(r => visitor.visit(r, context.controller));
visitor.getFailures().forEach(f => this.addFailure(f));
}
}
protected visitNgStyleHelper(style: CssAst, context: ComponentMetadata, styleMetadata: StyleMetadata, baseStart: number) {
if (!style) {
return;
} else {
const sourceFile = this.getContextSourceFile(styleMetadata.url, styleMetadata.style.source);
const visitor = new this._config.cssVisitorCtrl(sourceFile, this._originalOptions, context, styleMetadata, baseStart);
style.visit(visitor);
visitor.getFailures().forEach(f => this.addFailure(f));
}
}
protected getContextSourceFile(path: string, content: string) {
const current = this.getSourceFile();
if (!path) {
return current;
}
return ts.createSourceFile(path, `\`${content}\``, ts.ScriptTarget.ES5);
}
}