|
| 1 | +import * as Lint from 'tslint/lib/lint'; |
| 2 | +import * as ts from 'typescript'; |
| 3 | +import {sprintf} from 'sprintf-js'; |
| 4 | +import SyntaxKind = require('./util/syntaxKind'); |
| 5 | +import {SelectorValidator} from "./util/selectorValidator"; |
| 6 | + |
| 7 | +export class Rule extends Lint.Rules.AbstractRule { |
| 8 | + |
| 9 | + |
| 10 | + public prefix:string; |
| 11 | + public hasPrefix:boolean; |
| 12 | + private prefixChecker:Function; |
| 13 | + private validator:Function; |
| 14 | + |
| 15 | + constructor(ruleName:string, value:any, disabledIntervals:Lint.IDisabledInterval[]) { |
| 16 | + super(ruleName, value, disabledIntervals); |
| 17 | + if (value[1] === 'kebab-case') { |
| 18 | + this.validator = SelectorValidator.kebabCase; |
| 19 | + } |
| 20 | + if (value[2]) { |
| 21 | + this.hasPrefix = true; |
| 22 | + this.prefix = value[2]; |
| 23 | + this.prefixChecker = SelectorValidator.prefix(value[2]); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] { |
| 28 | + return this.applyWithWalker( |
| 29 | + new ClassMetadataWalker(sourceFile, this)); |
| 30 | + } |
| 31 | + |
| 32 | + public validateName(name:string):boolean { |
| 33 | + return this.validator(name); |
| 34 | + } |
| 35 | + |
| 36 | + public validatePrefix(prefix:string):boolean { |
| 37 | + return this.prefixChecker(prefix); |
| 38 | + } |
| 39 | + |
| 40 | + static FAILURE_WITHOUT_PREFIX:string = 'The name of the Pipe decorator of class %s should' + |
| 41 | + ' be named kebab-case, however its value is "%s".'; |
| 42 | + |
| 43 | + static FAILURE_WITH_PREFIX:string = 'The name of the Pipe decorator of class %s should' + |
| 44 | + ' be named kebab-case with prefix %s, however its value is "%s".'; |
| 45 | +} |
| 46 | + |
| 47 | +export class ClassMetadataWalker extends Lint.RuleWalker { |
| 48 | + |
| 49 | + constructor(sourceFile:ts.SourceFile, private rule:Rule) { |
| 50 | + super(sourceFile, rule.getOptions()); |
| 51 | + } |
| 52 | + |
| 53 | + visitClassDeclaration(node:ts.ClassDeclaration) { |
| 54 | + let className = node.name.text; |
| 55 | + let decorators = node.decorators || []; |
| 56 | + decorators.filter(d=> { |
| 57 | + let baseExpr = <any>d.expression || {}; |
| 58 | + return baseExpr.expression.text === 'Pipe' |
| 59 | + }).forEach(this.validateProperties.bind(this, className)); |
| 60 | + super.visitClassDeclaration(node); |
| 61 | + } |
| 62 | + |
| 63 | + private validateProperties(className:string, pipe:any) { |
| 64 | + let argument = this.extractArgument(pipe); |
| 65 | + if (argument.kind === SyntaxKind.current().ObjectLiteralExpression) { |
| 66 | + argument.properties.filter(n=>n.name.text === 'name') |
| 67 | + .forEach(this.validateProperty.bind(this, className)) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private extractArgument(pipe:any) { |
| 72 | + let baseExpr = <any>pipe.expression || {}; |
| 73 | + let args = baseExpr.arguments || []; |
| 74 | + return args[0]; |
| 75 | + } |
| 76 | + |
| 77 | + private validateProperty(className:string, property:any) { |
| 78 | + let propName:string = property.initializer.text; |
| 79 | + let isValidName:boolean = this.rule.validateName(propName); |
| 80 | + let isValidPrefix:boolean = (this.rule.hasPrefix?this.rule.validatePrefix(propName):true); |
| 81 | + if (!isValidName || !isValidPrefix) { |
| 82 | + this.addFailure( |
| 83 | + this.createFailure( |
| 84 | + property.getStart(), |
| 85 | + property.getWidth(), |
| 86 | + sprintf.apply(this, this.createFailureArray(className, propName)))); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private createFailureArray(className:string, pipeName:string):Array<string> { |
| 91 | + if (this.rule.hasPrefix) { |
| 92 | + return [Rule.FAILURE_WITH_PREFIX, className, this.rule.prefix, pipeName]; |
| 93 | + } |
| 94 | + return [Rule.FAILURE_WITHOUT_PREFIX, className, pipeName]; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments