Skip to content
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

fix(no-output-on-prefix): do not warn on properly named props #481

Merged
merged 1 commit into from
Jan 7, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/noOutputOnPrefixRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,29 @@ export class Rule extends Lint.Rules.AbstractRule {
this would result in an on-onEvent binding expression`,
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: true,
typescriptOnly: true
};

static FAILURE_STRING: string = 'In the class "%s", the output ' +
'property "%s" should not be prefixed with on';
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()));
return this.applyWithWalker(new OutputWalker(sourceFile, this.getOptions()));
}
}

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

if (memberName && memberName.startsWith('on')) {
if (
memberName &&
memberName.startsWith('on') &&
!(memberName.length >= 3 && memberName[2] !== memberName[2].toUpperCase())
) {
const failureConfig: string[] = [Rule.FAILURE_STRING, className, memberName];
const errorMessage = sprintf.apply(null, failureConfig);
this.addFailure(
this.createFailure(
property.getStart(),
property.getWidth(),
errorMessage
)
);
this.addFailure(this.createFailure(property.getStart(), property.getWidth(), errorMessage));
}
}
}
22 changes: 22 additions & 0 deletions test/noOutputOnPrefixRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ describe('no-output-on-prefix', () => {
});
});

it('should fail, when a directive output property is named with on prefix', () => {
const source = `
@Directive()
class ButtonDirective {
@Output() on = new EventEmitter<any>();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}`;
assertAnnotated({
ruleName: 'no-output-on-prefix',
message: 'In the class "ButtonDirective", the output property "on" should not be prefixed with on',
source
});
});
});

describe('valid directive output property', () => {
Expand All @@ -41,5 +54,14 @@ describe('no-output-on-prefix', () => {
}`;
assertSuccess('no-output-on-prefix', source);
});

it('should succeed, when a directive output property is properly named, starting with `on`', () => {
const source = `
@Component()
class ButtonComponent {
@Output() oneProp = new EventEmitter<any>();
}`;
assertSuccess('no-output-on-prefix', source);
});
});
});