|
| 1 | +import { sprintf } from 'sprintf-js'; |
| 2 | +import * as Lint from 'tslint'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | +import { NgWalker } from './angular/ngWalker'; |
| 5 | +import { SelectorValidator } from './util/selectorValidator'; |
| 6 | +import { getDecoratorArgument } from './util/utils'; |
| 7 | + |
| 8 | +export class Rule extends Lint.Rules.AbstractRule { |
| 9 | + static readonly metadata: Lint.IRuleMetadata = { |
| 10 | + description: 'Enforce consistent prefix for pipes.', |
| 11 | + optionExamples: [[true, 'myPrefix'], [true, 'myPrefix', 'myOtherPrefix']], |
| 12 | + options: { |
| 13 | + items: [ |
| 14 | + { |
| 15 | + type: 'string' |
| 16 | + } |
| 17 | + ], |
| 18 | + minLength: 1, |
| 19 | + type: 'array' |
| 20 | + }, |
| 21 | + optionsDescription: Lint.Utils.dedent` |
| 22 | + * The list of arguments are supported prefixes (given as strings). |
| 23 | + `, |
| 24 | + rationale: 'Consistent conventions make it easy to quickly identify and reference assets of different types.', |
| 25 | + ruleName: 'pipe-prefix', |
| 26 | + type: 'style', |
| 27 | + typescriptOnly: true |
| 28 | + }; |
| 29 | + |
| 30 | + static FAILURE_STRING = `The name of the Pipe decorator of class %s should start with prefix %s, however its value is "%s"`; |
| 31 | + |
| 32 | + prefix: string; |
| 33 | + private prefixChecker: Function; |
| 34 | + |
| 35 | + constructor(options: Lint.IOptions) { |
| 36 | + super(options); |
| 37 | + |
| 38 | + let args = options.ruleArguments; |
| 39 | + if (!(args instanceof Array)) { |
| 40 | + args = [args]; |
| 41 | + } |
| 42 | + this.prefix = args.join(','); |
| 43 | + let prefixExpression = args.join('|'); |
| 44 | + this.prefixChecker = SelectorValidator.prefix(prefixExpression, 'camelCase'); |
| 45 | + } |
| 46 | + |
| 47 | + apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 48 | + return this.applyWithWalker(new ClassMetadataWalker(sourceFile, this)); |
| 49 | + } |
| 50 | + |
| 51 | + isEnabled(): boolean { |
| 52 | + const { |
| 53 | + metadata: { |
| 54 | + options: { minLength } |
| 55 | + } |
| 56 | + } = Rule; |
| 57 | + const { length } = this.ruleArguments; |
| 58 | + |
| 59 | + return super.isEnabled() && length >= minLength; |
| 60 | + } |
| 61 | + |
| 62 | + validatePrefix(prefix: string): boolean { |
| 63 | + return this.prefixChecker(prefix); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export class ClassMetadataWalker extends NgWalker { |
| 68 | + constructor(sourceFile: ts.SourceFile, private rule: Rule) { |
| 69 | + super(sourceFile, rule.getOptions()); |
| 70 | + } |
| 71 | + |
| 72 | + protected visitNgPipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) { |
| 73 | + let className = controller.name!.text; |
| 74 | + this.validateProperties(className, decorator); |
| 75 | + super.visitNgPipe(controller, decorator); |
| 76 | + } |
| 77 | + |
| 78 | + private validateProperties(className: string, pipe: ts.Decorator) { |
| 79 | + const argument = getDecoratorArgument(pipe)!; |
| 80 | + |
| 81 | + argument.properties |
| 82 | + .filter(p => p.name && ts.isIdentifier(p.name) && p.name.text === 'name') |
| 83 | + .forEach(this.validateProperty.bind(this, className)); |
| 84 | + } |
| 85 | + |
| 86 | + private validateProperty(className: string, property: ts.Node) { |
| 87 | + const initializer = ts.isPropertyAssignment(property) ? property.initializer : undefined; |
| 88 | + |
| 89 | + if (initializer && ts.isStringLiteral(initializer)) { |
| 90 | + const propName = initializer.text; |
| 91 | + const isValid = this.rule.validatePrefix(propName); |
| 92 | + |
| 93 | + if (!isValid) { |
| 94 | + this.addFailureAtNode(property, sprintf(Rule.FAILURE_STRING, className, this.rule.prefix, propName)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments