From 96d9292a77928371819f11df102c5ad7ab198719 Mon Sep 17 00:00:00 2001 From: Guillaume Bilodeau Date: Wed, 7 Mar 2018 22:06:49 -0500 Subject: [PATCH] fix(537): no-output-named-after-standard-event checks output rename (#538) --- src/noOutputNamedAfterStandardEventRule.ts | 5 +- ...oOutputNamedAfterStandardEventRule.spec.ts | 60 +++++++++++++++++-- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/noOutputNamedAfterStandardEventRule.ts b/src/noOutputNamedAfterStandardEventRule.ts index df28c326e..a79b4b03d 100644 --- a/src/noOutputNamedAfterStandardEventRule.ts +++ b/src/noOutputNamedAfterStandardEventRule.ts @@ -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( @@ -204,8 +204,9 @@ export class OutputMetadataWalker extends NgWalker { visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) { let className = (property).parent.name.text; let memberName = (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( diff --git a/test/noOutputNamedAfterStandardEventRule.spec.ts b/test/noOutputNamedAfterStandardEventRule.spec.ts index 957d9dfd3..054877323 100644 --- a/test/noOutputNamedAfterStandardEventRule.spec.ts +++ b/test/noOutputNamedAfterStandardEventRule.spec.ts @@ -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 { @@ -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 { @@ -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(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + `; + + 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(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + `; + + 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 }); }); @@ -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(); + } + `; + 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(); + } + `; + assertSuccess('no-output-named-after-standard-event', source); + }); }); });