-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathnoLifecycleInvocationRule.ts
71 lines (61 loc) · 2.21 KB
/
noLifecycleInvocationRule.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
import * as Lint from 'tslint';
import ts from 'typescript';
import minimatch from 'minimatch';
const hooks = new Set([
'ngOnChanges',
'ngOnInit',
'ngDoCheck',
'ngAfterContentInit',
'ngAfterContentChecked',
'ngAfterViewInit',
'ngAfterViewChecked',
'ngOnDestroy',
'ngDoBootstrap',
]);
/** Rule that prevents direct calls of the Angular lifecycle hooks */
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}
class Walker extends Lint.RuleWalker {
/** Whether the walker should check the current source file. */
private _enabled: boolean;
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
const fileGlobs: string[] = options.ruleArguments[0];
this._enabled = !fileGlobs.some(p => minimatch(sourceFile.fileName, p));
}
override visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
// Flag any accesses of the lifecycle hooks that are
// inside function call and don't match the allowed criteria.
if (
this._enabled &&
ts.isCallExpression(node.parent) &&
hooks.has(node.name.text) &&
!this._isAllowedAccessor(node)
) {
this.addFailureAtNode(node, 'Manually invoking Angular lifecycle hooks is not allowed.');
}
return super.visitPropertyAccessExpression(node);
}
/** Checks whether the accessor of an Angular lifecycle hook expression is allowed. */
private _isAllowedAccessor(node: ts.PropertyAccessExpression): boolean {
// We only allow accessing the lifecycle hooks via super.
if (node.expression.kind !== ts.SyntaxKind.SuperKeyword) {
return false;
}
let parent = node.parent;
// Even if the access is on a `super` expression, verify that the hook is being called
// from inside a method with the same name (e.g. to avoid calling `ngAfterViewInit` from
// inside `ngOnInit`).
while (parent && !ts.isSourceFile(parent)) {
if (ts.isMethodDeclaration(parent)) {
return (parent.name as ts.Identifier).text === node.name.text;
} else {
parent = parent.parent;
}
}
return false;
}
}