forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeNamingRule.ts
More file actions
91 lines (77 loc) · 3.09 KB
/
pipeNamingRule.ts
File metadata and controls
91 lines (77 loc) · 3.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as Lint from 'tslint/lib/lint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
import {Ng2Walker} from './angular/ng2Walker';
import {SelectorValidator} from './util/selectorValidator';
export class Rule extends Lint.Rules.AbstractRule {
static FAILURE_WITHOUT_PREFIX: string = 'The name of the Pipe decorator of class %s should' +
' be named camelCase, however its value is "%s".';
static FAILURE_WITH_PREFIX: string = 'The name of the Pipe decorator of class %s should' +
' be named camelCase with prefix %s, however its value is "%s".';
public prefix: string;
public hasPrefix: boolean;
private prefixChecker: Function;
private validator: Function;
constructor(ruleName:string, value:any, disabledIntervals:Lint.IDisabledInterval[]) {
super(ruleName, value, disabledIntervals);
if (value[1] === 'camelCase') {
this.validator = SelectorValidator.camelCase;
}
if (value.length>2) {
this.hasPrefix = true;
let prefixExpression:string = (value.slice(2)||[]).join('|');
this.prefix =(value.slice(2)||[]).join(',');
this.prefixChecker = SelectorValidator.multiPrefix(prefixExpression);
}
}
public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
return this.applyWithWalker(
new ClassMetadataWalker(sourceFile, this));
}
public validateName(name:string):boolean {
return this.validator(name);
}
public validatePrefix(prefix:string):boolean {
return this.prefixChecker(prefix);
}
}
export class ClassMetadataWalker extends Ng2Walker {
constructor(sourceFile:ts.SourceFile, private rule:Rule) {
super(sourceFile, rule.getOptions());
}
visitNg2Pipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
let className = controller.name.text;
this.validateProperties(className, decorator);
}
private validateProperties(className:string, pipe:any) {
let argument = this.extractArgument(pipe);
if (argument.kind === SyntaxKind.current().ObjectLiteralExpression) {
argument.properties.filter(n=>n.name.text === 'name')
.forEach(this.validateProperty.bind(this, className))
}
}
private extractArgument(pipe:any) {
let baseExpr = <any>pipe.expression || {};
let args = baseExpr.arguments || [];
return args[0];
}
private validateProperty(className:string, property:any) {
let propName:string = property.initializer.text;
let isValidName:boolean = this.rule.validateName(propName);
let isValidPrefix:boolean = (this.rule.hasPrefix?this.rule.validatePrefix(propName):true);
if (!isValidName || !isValidPrefix) {
this.addFailure(
this.createFailure(
property.getStart(),
property.getWidth(),
sprintf.apply(this, this.createFailureArray(className, propName))));
}
}
private createFailureArray(className:string, pipeName:string):Array<string> {
if (this.rule.hasPrefix) {
return [Rule.FAILURE_WITH_PREFIX, className, this.rule.prefix, pipeName];
}
return [Rule.FAILURE_WITHOUT_PREFIX, className, pipeName];
}
}