-
Notifications
You must be signed in to change notification settings - Fork 234
/
componentClassSuffixRule.ts
62 lines (54 loc) · 2.12 KB
/
componentClassSuffixRule.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
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import {ComponentMetadata} from './angular/metadata';
import {Failure} from './walkerFactory/walkerFactory';
import {all, validateComponent} from './walkerFactory/walkerFn';
import {Maybe, F2} from './util/function';
import {IOptions} from 'tslint';
import {NgWalker} from './angular/ngWalker';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'component-class-suffix',
type: 'style',
description: `Classes decorated with @Component must have suffix "Component" (or custom) in their name.`,
descriptionDetails: `See more at https://angular.io/styleguide#style-02-03.`,
rationale: `Consistent conventions make it easy to quickly identify and reference assets of different types.`,
options: {
type: 'array',
items: {
type: 'string',
}
},
optionExamples: [
`true`,
`[true, "Component", "View"]`
],
optionsDescription: `Supply a list of allowed component suffixes. Defaults to "Component".`,
typescriptOnly: true,
};
static FAILURE: string = 'The name of the class %s should end with the suffix %s ($$02-03$$)';
static walkerBuilder: F2<ts.SourceFile, IOptions, NgWalker> =
all(
validateComponent((meta: ComponentMetadata, suffixList?: string[]) =>
Maybe.lift(meta.controller)
.fmap(controller => controller.name)
.fmap(name => {
const className = name.text;
if (suffixList.length === 0) {
suffixList = ['Component'];
}
if (!Rule.validate(className, suffixList)) {
return [new Failure(name, sprintf(Rule.FAILURE, className, suffixList))];
}
})
));
static validate(className: string, suffixList: string[]): boolean {
return suffixList.some(suffix => className.endsWith(suffix));
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
Rule.walkerBuilder(sourceFile, this.getOptions())
);
}
}