forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextualLifecycleRule.ts
80 lines (64 loc) · 2.8 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
import { sprintf } from 'sprintf-js';
import { IRuleMetadata, RuleFailure } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { ClassDeclaration, Decorator, SourceFile } from 'typescript';
import { NgWalker } from './angular/ngWalker';
import {
getClassName,
getDecoratorName,
isLifecycleMethod,
isMetadataType,
LifecycleMethodKeys,
LifecycleMethods,
METADATA_TYPE_LIFECYCLE_MAPPER,
MetadataTypeKeys,
MetadataTypes
} from './util/utils';
import { InjectableMetadata, PipeMetadata } from './angular';
interface FailureParameters {
readonly className: string;
readonly metadataType: MetadataTypeKeys;
readonly methodName: LifecycleMethodKeys;
}
export const getFailureMessage = (failureParameters: FailureParameters): string =>
sprintf(Rule.FAILURE_STRING, failureParameters.methodName, failureParameters.className, failureParameters.metadataType);
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that classes use allowed lifecycle method in its body.',
options: null,
optionsDescription: 'Not configurable.',
rationale: `Some lifecycle methods can only be used in certain class types. For example, ${
LifecycleMethods.ngOnInit
}() method should not be used in an @${MetadataTypes.Injectable} class.`,
ruleName: 'contextual-lifecycle',
type: 'functionality',
typescriptOnly: true
};
static readonly FAILURE_STRING = 'The method "%s" is not allowed for class "%s" because it is decorated with "%s"';
apply(sourceFile: SourceFile): RuleFailure[] {
return this.applyWithWalker(new ContextualLifecycleWalker(sourceFile, this.getOptions()));
}
}
class ContextualLifecycleWalker extends NgWalker {
visitNgInjectable(metadata: InjectableMetadata): void {
this.validateDecorator(metadata.controller, metadata.decorator, METADATA_TYPE_LIFECYCLE_MAPPER.Injectable);
super.visitNgInjectable(metadata);
}
visitNgPipe(metadata: PipeMetadata): void {
this.validateDecorator(metadata.controller, metadata.decorator, METADATA_TYPE_LIFECYCLE_MAPPER.Pipe);
super.visitNgPipe(metadata);
}
private validateDecorator(controller: ClassDeclaration, decorator: Decorator, allowedMethods: ReadonlySet<LifecycleMethodKeys>): void {
const className = getClassName(controller)!;
const metadataType = getDecoratorName(decorator);
if (!metadataType || !isMetadataType(metadataType)) return;
for (const member of controller.members) {
const { name: memberName } = member;
if (!memberName) continue;
const methodName = memberName.getText();
if (!isLifecycleMethod(methodName) || allowedMethods.has(methodName)) continue;
const failure = getFailureMessage({ className, metadataType, methodName });
this.addFailureAtNode(member, failure);
}
}
}