-
Notifications
You must be signed in to change notification settings - Fork 234
/
contextualLifeCycleRule.ts
180 lines (155 loc) · 7.16 KB
/
contextualLifeCycleRule.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
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
import {NgWalker} from './angular/ngWalker';
import {ComponentMetadata, DirectiveMetadata} from './angular/metadata';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'contextual-life-cycle',
type: 'functionality',
description: `Ensure that classes use allowed life cycle method in its body`,
rationale: `Some life cycle methods can only be used in certain class types.
For example, ngOnInit() hook method should not be used in an @Injectable class.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: true,
};
static FAILURE_STRING: string = 'In the class "%s" which have the "%s" decorator, the ' +
'"%s" hook method is not allowed. ' +
'Please, drop it.';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new ClassMetadataWalker(sourceFile,
this));
}
}
export class ClassMetadataWalker extends NgWalker {
className: string;
isInjectable = false;
isComponent = false;
isDirective = false;
isPipe = false;
constructor(sourceFile: ts.SourceFile, private rule: Rule) {
super(sourceFile, rule.getOptions());
}
visitNgInjectable(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
this.className = controller.name.text;
this.isInjectable = true;
}
visitNgComponent(metadata: ComponentMetadata) {
this.className = metadata.controller.name.text;
this.isComponent = true;
}
visitNgDirective(metadata: DirectiveMetadata) {
this.className = metadata.controller.name.text;
this.isDirective = true;
}
visitNgPipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
this.className = controller.name.text;
this.isPipe = true;
}
visitMethodDeclaration(method: ts.MethodDeclaration) {
const methodName = (method.name as ts.StringLiteral).text;
if (methodName === 'ngOnInit') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngOnInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngOnInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngOnChanges') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngOnChanges()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngOnChanges()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngDoCheck') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngDoCheck()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngDoCheck()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngAfterContentInit') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngAfterContentInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngAfterContentInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isDirective) {
let failureConfig: string[] = [this.className, '@Directive', 'ngAfterContentInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngAfterContentChecked') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngAfterContentChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngAfterContentChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isDirective) {
let failureConfig: string[] = [this.className, '@Directive', 'ngAfterContentChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngAfterViewInit') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngAfterViewInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngAfterViewInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isDirective) {
let failureConfig: string[] = [this.className, '@Directive', 'ngAfterViewInit()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
if (methodName === 'ngAfterViewChecked') {
if (this.isInjectable) {
let failureConfig: string[] = [this.className, '@Injectable', 'ngAfterViewChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isPipe) {
let failureConfig: string[] = [this.className, '@Pipe', 'ngAfterViewChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
} else if (this.isDirective) {
let failureConfig: string[] = [this.className, '@Directive', 'ngAfterViewChecked()'];
failureConfig.unshift(Rule.FAILURE_STRING);
this.generateFailure(method.getStart(), method.getWidth(), failureConfig);
}
}
}
private generateFailure(start: number, width: number, failureConfig: string[]) {
this.addFailure(
this.createFailure(
start,
width,
sprintf.apply(this, failureConfig)));
}
}