forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngWalker.ts
243 lines (204 loc) · 9.11 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import * as compiler from '@angular/compiler';
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { logger } from '../util/logger';
import { getClassName, getDecoratorName, maybeNodeArray } from '../util/utils';
import { Config } from './config';
import { ComponentMetadata, DirectiveMetadata, InjectableMetadata, ModuleMetadata, PipeMetadata, StyleMetadata } from './metadata';
import { MetadataReader } from './metadataReader';
import { ngWalkerFactoryUtils } from './ngWalkerFactoryUtils';
import { BasicCssAstVisitor, CssAstVisitorCtrl } from './styles/basicCssAstVisitor';
import { CssAst } from './styles/cssAst';
import { parseCss } from './styles/parseCss';
import { BasicTemplateAstVisitor, RecursiveAngularExpressionVisitorCtr, TemplateAstVisitorCtr } from './templates/basicTemplateAstVisitor';
import { RecursiveAngularExpressionVisitor } from './templates/recursiveAngularExpressionVisitor';
import { ReferenceCollectorVisitor } from './templates/referenceCollectorVisitor';
import { parseTemplate } from './templates/templateParser';
const getDecoratorStringArgs = (decorator: ts.Decorator): string[] => {
const { expression } = decorator;
const args = ts.isCallExpression(expression) ? expression.arguments : ts.createNodeArray();
return args.filter(ts.isStringLiteral).map(x => x.text);
};
const getPosition = (node: ts.Node) => {
let pos = 0;
if (node) {
pos = node.pos + 1;
try {
pos = node.getStart() + 1;
} catch {}
}
return pos;
};
export interface NgWalkerConfig {
cssVisitorCtrl?: CssAstVisitorCtrl;
expressionVisitorCtrl?: RecursiveAngularExpressionVisitorCtr;
templateVisitorCtrl?: TemplateAstVisitorCtr;
}
// tslint:disable-next-line: deprecation
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(
{
cssVisitorCtrl: BasicCssAstVisitor,
templateVisitorCtrl: BasicTemplateAstVisitor,
expressionVisitorCtrl: RecursiveAngularExpressionVisitor
},
this._config || {}
);
}
visitClassDeclaration(declaration: ts.ClassDeclaration) {
if (this.hasClassName(declaration)) {
const metadata = this._metadataReader!.read(declaration);
if (metadata instanceof ComponentMetadata) {
this.visitNgComponent(metadata);
} else if (metadata instanceof DirectiveMetadata) {
this.visitNgDirective(metadata);
} else if (metadata instanceof PipeMetadata) {
this.visitNgPipe(metadata);
} else if (metadata instanceof ModuleMetadata) {
this.visitNgModule(metadata);
} else if (metadata instanceof InjectableMetadata) {
this.visitNgInjectable(metadata);
}
}
maybeNodeArray(ts.createNodeArray(declaration.decorators)).forEach(this.visitClassDecorator.bind(this));
super.visitClassDeclaration(declaration);
}
visitMethodDeclaration(method: ts.MethodDeclaration) {
maybeNodeArray(ts.createNodeArray(method.decorators)).forEach(this.visitMethodDecorator.bind(this));
super.visitMethodDeclaration(method);
}
visitPropertyDeclaration(prop: ts.PropertyDeclaration) {
maybeNodeArray(ts.createNodeArray(prop.decorators)).forEach(this.visitPropertyDecorator.bind(this));
super.visitPropertyDeclaration(prop);
}
protected visitMethodDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
if (name === 'HostListener') {
this.visitNgHostListener(decorator.parent as ts.MethodDeclaration, decorator, getDecoratorStringArgs(decorator));
}
}
protected visitPropertyDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
switch (name) {
case 'Input':
this.visitNgInput(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'Output':
this.visitNgOutput(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'HostBinding':
this.visitNgHostBinding(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'ContentChild':
this.visitNgContentChild(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'ContentChildren':
this.visitNgContentChildren(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'ViewChild':
this.visitNgViewChild(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
case 'ViewChildren':
this.visitNgViewChildren(decorator.parent as ts.PropertyDeclaration, decorator, getDecoratorStringArgs(decorator));
break;
}
}
protected visitNgComponent(metadata: ComponentMetadata) {
const { styles = [] } = metadata;
for (const style of styles) {
try {
const cssAst = parseCss(style!.style.code);
this.visitNgStyleHelper(cssAst, metadata, style!, getPosition(style!.node!));
} catch (e) {
const {
controller: { name }
} = metadata;
const text = name && ts.isIdentifier(name) ? name.text : '';
logger.error('Cannot parse the styles of', text, e);
}
}
const { template } = metadata;
if (template && template.template) {
try {
const templateAst = parseTemplate(template.template.code, Config.predefinedDirectives);
this.visitNgTemplateHelper(templateAst, metadata, getPosition(template.node!));
} catch (e) {
const {
controller: { name }
} = metadata;
const text = name && ts.isIdentifier(name) ? name.text : '';
logger.error('Cannot parse the template of', text, e);
}
}
}
protected visitClassDecorator(decorator: ts.Decorator) {}
protected visitNgModule(metadata: ModuleMetadata) {}
protected visitNgDirective(metadata: DirectiveMetadata) {}
protected visitNgPipe(metadata: DirectiveMetadata) {}
protected visitNgInjectable(metadata: InjectableMetadata) {}
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 visitNgContentChild(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}
protected visitNgContentChildren(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}
protected visitNgViewChild(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}
protected visitNgViewChildren(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}
protected visitNgTemplateHelper(roots: compiler.TemplateAst[], context: ComponentMetadata, baseStart: number) {
if (!roots || !roots.length) {
return;
}
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));
// tslint:disable-next-line:deprecation
visitor.getFailures().forEach(f => this.addFailure(f));
}
protected visitNgStyleHelper(style: CssAst, context: ComponentMetadata, styleMetadata: StyleMetadata, baseStart: number) {
if (!style) {
return;
}
const sourceFile = this.getContextSourceFile(styleMetadata.url!, styleMetadata.style.source!);
const visitor = new this._config!.cssVisitorCtrl!(sourceFile, this._originalOptions, context, styleMetadata, baseStart);
style.visit(visitor);
// tslint:disable-next-line:deprecation
visitor.getFailures().forEach(f => this.addFailure(f));
}
protected getContextSourceFile(path: string, content: string) {
if (!path) {
return this.getSourceFile();
}
const sf = ts.createSourceFile(path, `\`${content}\``, ts.ScriptTarget.ES5);
const original = sf.getFullText;
sf.getFullText = () => {
const text = original.apply(sf);
return text.substring(1, text.length - 1);
};
return sf;
}
private hasClassName(node: ts.Decorator | ts.ClassDeclaration) {
if (ts.isDecorator(node) && !getClassName(node.parent as ts.ClassDeclaration)) {
return false;
} else if (ts.isClassDeclaration(node) && !getClassName(node)) {
return false;
}
return true;
}
}