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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"contributors": [
"Minko Gechev <[email protected]>",
"Preslav Semov <[email protected]>",
"William Koza <[email protected]>"
"William Koza <[email protected]>",
"Eugenio Romano <[email protected]>"
],
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.