-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathcomponentClassSuffixRule.ts
More file actions
58 lines (50 loc) · 2.16 KB
/
Copy pathcomponentClassSuffixRule.ts
File metadata and controls
58 lines (50 loc) · 2.16 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { sprintf } from 'sprintf-js';
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { NgWalker } from './angular';
import { ComponentMetadata } from './angular/metadata';
import { F2, Maybe } from './util/function';
import { Failure } from './walkerFactory/walkerFactory';
import { all, validateComponent } from './walkerFactory/walkerFn';
export class Rule extends Lint.Rules.AbstractRule {
static readonly metadata: Lint.IRuleMetadata = {
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.',
optionExamples: [true, [true, 'Component', 'View']],
options: {
items: {
type: 'string',
},
minLength: 0,
type: 'array',
},
optionsDescription: 'Supply a list of allowed component suffixes. Defaults to "Component".',
rationale: 'Consistent conventions make it easy to quickly identify and reference assets of different types.',
ruleName: 'component-class-suffix',
type: 'style',
typescriptOnly: true,
};
static readonly FAILURE_STRING = 'The name of the class %s should end with the suffix %s (https://angular.io/styleguide#style-02-03)';
static walkerBuilder: F2<ts.SourceFile, Lint.IOptions, NgWalker> = all(
validateComponent((meta: ComponentMetadata, suffixList: string[] = []) =>
Maybe.lift(meta.controller)
.fmap((controller) => controller.name)
.fmap((name) => {
const { text } = name!;
const failures: Failure[] = [];
const suffixes = suffixList.length > 0 ? suffixList : ['Component'];
if (!Rule.validate(text, suffixes)) {
failures.push(new Failure(name!, sprintf(Rule.FAILURE_STRING, text, suffixes)));
}
return failures;
})
)
);
static validate(className: string, suffixList: string[]): boolean {
return suffixList.some((suffix) => className.endsWith(suffix));
}
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const walker = Rule.walkerBuilder(sourceFile, this.getOptions());
return this.applyWithWalker(walker);
}
}