|
| 1 | +import * as Lint from 'tslint'; |
| 2 | +import * as ts from 'typescript'; |
| 3 | +import { sprintf } from 'sprintf-js'; |
| 4 | +import { NgWalker } from './angular/ngWalker'; |
| 5 | + |
| 6 | +export class Rule extends Lint.Rules.AbstractRule { |
| 7 | + public static metadata: Lint.IRuleMetadata = { |
| 8 | + ruleName: 'no-on-prefix-output-name', |
| 9 | + type: 'maintainability', |
| 10 | + description: `Name events without the prefix on`, |
| 11 | + descriptionDetails: `See more at https://angular.io/guide/styleguide#dont-prefix-output-properties`, |
| 12 | + rationale: `Angular allows for an alternative syntax on-*. If the event itself was prefixed with on |
| 13 | + this would result in an on-onEvent binding expression`, |
| 14 | + options: null, |
| 15 | + optionsDescription: `Not configurable.`, |
| 16 | + typescriptOnly: true, |
| 17 | + }; |
| 18 | + |
| 19 | + static FAILURE_STRING: string = 'In the class "%s", the output ' + |
| 20 | + 'property "%s" should not be prefixed with on'; |
| 21 | + |
| 22 | + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 23 | + return this.applyWithWalker( |
| 24 | + new OutputWalker(sourceFile, |
| 25 | + this.getOptions())); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export class OutputWalker extends NgWalker { |
| 30 | + visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) { |
| 31 | + let className = (<any>property).parent.name.text; |
| 32 | + let memberName = (<any>property.name).text; |
| 33 | + |
| 34 | + if (memberName && memberName.startsWith('on')) { |
| 35 | + let failureConfig: string[] = [className, memberName]; |
| 36 | + failureConfig.unshift(Rule.FAILURE_STRING); |
| 37 | + this.addFailure( |
| 38 | + this.createFailure( |
| 39 | + property.getStart(), |
| 40 | + property.getWidth(), |
| 41 | + sprintf.apply(this, failureConfig))); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments