-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathuseInjectableProvidedInRule.ts
More file actions
38 lines (31 loc) · 1.3 KB
/
Copy pathuseInjectableProvidedInRule.ts
File metadata and controls
38 lines (31 loc) · 1.3 KB
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
import { IRuleMetadata, RuleFailure } from 'tslint';
import { AbstractRule } from 'tslint/lib/rules';
import { SourceFile } from 'typescript';
import { InjectableMetadata } from './angular';
import { NgWalker } from './angular/ngWalker';
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: "Enforces classes decorated with @Injectable to use the 'providedIn' property.",
options: null,
optionsDescription: 'Not configurable.',
rationale: "Using the 'providedIn' property makes classes decorated with @Injectable tree shakeable.",
ruleName: 'use-injectable-provided-in',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_STRING = "Classes decorated with @Injectable should use the 'providedIn' property";
apply(sourceFile: SourceFile): RuleFailure[] {
const walker = new Walker(sourceFile, this.getOptions());
return this.applyWithWalker(walker);
}
}
class Walker extends NgWalker {
protected visitNgInjectable(metadata: InjectableMetadata): void {
this.validateInjectable(metadata);
super.visitNgInjectable(metadata);
}
private validateInjectable(metadata: InjectableMetadata): void {
if (metadata.providedIn) return;
this.addFailureAtNode(metadata.decorator, Rule.FAILURE_STRING);
}
}