Skip to content

Commit

Permalink
don't prefix output properties check rule (#440)
Browse files Browse the repository at this point in the history
* don't prefix output properties check rule

* I18nRule export back

* Update index.ts
  • Loading branch information
eromano authored and mgechev committed Oct 29, 2017
1 parent 12159e4 commit 8f2b4e7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -208,6 +208,7 @@ Below you can find a recommended configuration which is based on the [Angular St
"use-host-property-decorator": true,
"no-attribute-parameter-decorator": true,
"no-input-rename": true,
"no-on-prefix-output-name": true,
"no-output-rename": true,
"no-forward-ref": true,
"use-life-cycle-interface": true,
Expand Down Expand Up @@ -292,6 +293,8 @@ module.exports = {
[<img alt="leosvelperez" src="https://avatars0.githubusercontent.com/u/12051310?v=3&s=117" width="117">](https://github.com/leosvelperez) |[<img alt="rtfpessoa" src="https://avatars3.githubusercontent.com/u/902384?v=3&s=117" width="117">](https://github.com/rtfpessoa) |[<img alt="scttcper" src="https://avatars0.githubusercontent.com/u/1400464?v=3&s=117" width="117">](https://github.com/scttcper) |[<img alt="laco0416" src="https://avatars0.githubusercontent.com/u/1529180?v=3&s=117" width="117">](https://github.com/laco0416) |[<img alt="tmair" src="https://avatars1.githubusercontent.com/u/1596276?v=3&s=117" width="117">](https://github.com/tmair) |[<img alt="cexbrayat" src="https://avatars0.githubusercontent.com/u/411874?v=3&s=117" width="117">](https://github.com/cexbrayat) |
:---: |:---: |:---: |:---: |:---: |:---: |
[leosvelperez](https://github.com/leosvelperez) |[rtfpessoa](https://github.com/rtfpessoa) |[scttcper](https://github.com/scttcper) |[laco0416](https://github.com/laco0416) |[tmair](https://github.com/tmair) |[cexbrayat](https://github.com/cexbrayat) |
[<img alt="eromano" src="https://avatars0.githubusercontent.com/u/1030050?v=3&s=117" width="117">](https://github.com/eromano) | | | | | |
[eromano](https://github.com/eromano) | | | | ||

## License

Expand Down
1 change: 1 addition & 0 deletions docs/src/worker.ts
Expand Up @@ -10,6 +10,7 @@ const rulesConfig = {
'use-host-property-decorator': true,
'no-input-rename': true,
'no-output-rename': true,
'no-on-prefix-output-name': true,
'use-life-cycle-interface': true,
'use-pipe-transform-interface': true,
'component-class-suffix': true,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -23,7 +23,8 @@
"contributors": [
"Minko Gechev <mgechev@gmail.com>",
"Preslav Semov <preslavsemov@gmail.com>",
"William Koza <william.koza@gmail.com>"
"William Koza <william.koza@gmail.com>",
"Eugenio Romano <eugenioromanodeveloper@gmail.com>"
],
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -11,6 +11,7 @@ export { Rule as ImportDestructuringSpacingRule } from './importDestructuringSpa
export { Rule as NoAttributeParameterDecoratorRule } from './noAttributeParameterDecoratorRule';
export { Rule as NoForwardRefRule } from './noForwardRefRule';
export { Rule as NoInputRenameRule } from './noInputRenameRule';
export { Rule as NoOutputOnPrefixNameRule } from './noOutputOnPrefixNameRule';
export { Rule as NoOutputRenameRule } from './noOutputRenameRule';
export { Rule as NoUnusedCssRule } from './noUnusedCssRule';
export { Rule as PipeImpureRule } from './pipeImpureRule';
Expand Down
44 changes: 44 additions & 0 deletions src/noOutputOnPrefixNameRule.ts
@@ -0,0 +1,44 @@
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-on-prefix-output-name',
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()));
}
}

export class OutputWalker extends NgWalker {
visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) {
let className = (<any>property).parent.name.text;
let memberName = (<any>property.name).text;

if (memberName && memberName.startsWith('on')) {
let failureConfig: string[] = [className, memberName];
failureConfig.unshift(Rule.FAILURE_STRING);
this.addFailure(
this.createFailure(
property.getStart(),
property.getWidth(),
sprintf.apply(this, failureConfig)));
}
}
}
27 changes: 27 additions & 0 deletions test/noOutputOnPrefixNameRule.spec.ts
@@ -0,0 +1,27 @@
import { assertSuccess, assertAnnotated } from './testHelper';

describe('no-on-prefix-output-name', () => {
describe('invalid directive output property', () => {
it(`should fail, when a directive output property is named with on prefix`, () => {
let source = `
class ButtonComponent {
@Output() onChange = new EventEmitter<any>();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}`;
assertAnnotated({
ruleName: 'no-on-prefix-output-name',
source
});
});
});

describe('valid directive output property', () => {
it('should succeed, when a directive output property is properly named', () => {
let source = `
class ButtonComponent {
@Output() change = new EventEmitter<any>();
}`;
assertSuccess('no-on-prefix-output-name', source);
});
});
});

0 comments on commit 8f2b4e7

Please sign in to comment.