Skip to content

don't prefix output properties check rule #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});