forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoOutputOnPrefixRule.ts
More file actions
41 lines (36 loc) · 1.63 KB
/
noOutputOnPrefixRule.ts
File metadata and controls
41 lines (36 loc) · 1.63 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
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { sprintf } from 'sprintf-js';
import { NgWalker } from './angular/ngWalker';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'no-output-on-prefix',
type: 'maintainability',
description: 'Name events without the prefix on',
descriptionDetails: 'See more at https://angular.io/guide/styleguide#dont-prefix-output-properties',
rationale: `Angular allows for an alternative syntax on-*. If the event itself was prefixed with on
this would result in an on-onEvent binding expression`,
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: true
};
static FAILURE_STRING: string = 'In the class "%s", the output ' + 'property "%s" should not be prefixed with on';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new OutputWalker(sourceFile, this.getOptions()));
}
}
class OutputWalker extends NgWalker {
visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) {
const className = (<any>property).parent.name.text;
const memberName = (<any>property.name).text as string;
if (
memberName &&
memberName.startsWith('on') &&
!(memberName.length >= 3 && memberName[2] !== memberName[2].toUpperCase())
) {
const failureConfig: string[] = [Rule.FAILURE_STRING, className, memberName];
const errorMessage = sprintf.apply(null, failureConfig);
this.addFailure(this.createFailure(property.getStart(), property.getWidth(), errorMessage));
}
}
}