Skip to content

Commit

Permalink
fix(537): no-output-named-after-standard-event checks output rename (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gbilodeau authored and mgechev committed Mar 8, 2018
1 parent 5851306 commit 96d9292
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/noOutputNamedAfterStandardEventRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};

static FAILURE_STRING: string = 'In the class "%s", the output ' +
'property "%s" should not be named after a standard event.';
'property "%s" should not be named or renamed after a standard event.';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
Expand Down Expand Up @@ -204,8 +204,9 @@ export class OutputMetadataWalker extends NgWalker {
visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) {
let className = (<any>property).parent.name.text;
let memberName = (<any>property.name).text;
let outputName = args.length === 0 ? memberName : args[0];

if (memberName && this.standardEventNames.get(memberName)) {
if (outputName && this.standardEventNames.get(outputName)) {
const failureConfig: string[] = [Rule.FAILURE_STRING, className, memberName];
const errorMessage = sprintf.apply(null, failureConfig);
this.addFailure(
Expand Down
60 changes: 56 additions & 4 deletions test/noOutputNamedAfterStandardEventRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertSuccess, assertAnnotated } from './testHelper';

describe('no-output-named-after-standard-event', () => {
describe('invalid directive output property', () => {
it('should fail, when component output property is named with change prefix', () => {
it('should fail, when component output property is named "change"', () => {
const source = `
@Component()
class ButtonComponent {
Expand All @@ -13,12 +13,12 @@ describe('no-output-named-after-standard-event', () => {

assertAnnotated({
ruleName: 'no-output-named-after-standard-event',
message: 'In the class "ButtonComponent", the output property "change" should not be named after a standard event.',
message: 'In the class "ButtonComponent", the output property "change" should not be named or renamed after a standard event.',
source
});
});

it('should fail, when directive output property is named with change prefix', () => {
it('should fail, when directive output property is named "change"', () => {
const source = `
@Directive()
class ButtonDirective {
Expand All @@ -29,7 +29,39 @@ describe('no-output-named-after-standard-event', () => {

assertAnnotated({
ruleName: 'no-output-named-after-standard-event',
message: 'In the class "ButtonDirective", the output property "change" should not be named after a standard event.',
message: 'In the class "ButtonDirective", the output property "change" should not be named or renamed after a standard event.',
source
});
});

it('should fail, when component output property is renamed to "change"', () => {
const source = `
@Component()
class ButtonComponent {
@Output("change") _change = new EventEmitter<any>();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
`;

assertAnnotated({
ruleName: 'no-output-named-after-standard-event',
message: 'In the class "ButtonComponent", the output property "_change" should not be named or renamed after a standard event.',
source
});
});

it('should fail, when directive output property is renamed to "change"', () => {
const source = `
@Directive()
class ButtonDirective {
@Output("change") _change = new EventEmitter<any>();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
`;

assertAnnotated({
ruleName: 'no-output-named-after-standard-event',
message: 'In the class "ButtonDirective", the output property "_change" should not be named or renamed after a standard event.',
source
});
});
Expand All @@ -55,5 +87,25 @@ describe('no-output-named-after-standard-event', () => {
`;
assertSuccess('no-output-named-after-standard-event', source);
});

it('should succeed, when a component output property is properly renamed', () => {
const source = `
@Component()
class ButtonComponent {
@Output("buttonChange") _buttonChange = new EventEmitter<any>();
}
`;
assertSuccess('no-output-named-after-standard-event', source);
});

it('should succeed, when a directive output property is properly renamed', () => {
const source = `
@Directive()
class ButtonDirective {
@Output("buttonChange") _buttonChange = new EventEmitter<any>();
}
`;
assertSuccess('no-output-named-after-standard-event', source);
});
});
});

0 comments on commit 96d9292

Please sign in to comment.