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-input-rename): aria attributes not being allowed to be renamed #665

Merged
merged 1 commit into from
Jun 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/noInputRenameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,48 @@ export const getFailureMessage = (className: string, propertyName: string): stri
return sprintf(Rule.FAILURE_STRING, className, propertyName);
};

const kebabToCamelCase = (value: string) => value.replace(/-[a-zA-Z]/g, x => x[1].toUpperCase());

// source: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques
const whiteListAliases = new Set<string>([
'aria-activedescendant',
'aria-atomic',
'aria-autocomplete',
'aria-busy',
'aria-checked',
'aria-controls',
'aria-current',
'aria-describedby',
'aria-disabled',
'aria-dragged',
'aria-dropeffect',
'aria-expanded',
'aria-flowto',
'aria-haspopup',
'aria-hidden',
'aria-invalid',
'aria-label',
'aria-labelledby',
'aria-level',
'aria-live',
'aria-multiline',
'aria-multiselectable',
'aria-orientation',
'aria-owns',
'aria-posinset',
'aria-pressed',
'aria-readonly',
'aria-relevant',
'aria-required',
'aria-selected',
'aria-setsize',
'aria-sort',
'aria-valuemax',
'aria-valuemin',
'aria-valuenow',
'aria-valuetext'
]);

export class InputMetadataWalker extends NgWalker {
private directiveSelectors!: ReadonlySet<DirectiveMetadata['selector']>;

Expand All @@ -44,7 +86,10 @@ export class InputMetadataWalker extends NgWalker {
}

private canPropertyBeAliased(propertyAlias: string, propertyName: string): boolean {
return !!(this.directiveSelectors && this.directiveSelectors.has(propertyAlias) && propertyAlias !== propertyName);
return !!(
(this.directiveSelectors && this.directiveSelectors.has(propertyAlias) && propertyAlias !== propertyName) ||
(whiteListAliases.has(propertyAlias) && propertyName === kebabToCamelCase(propertyAlias))
);
}

private validateInput(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {
Expand Down
29 changes: 29 additions & 0 deletions test/noInputRenameRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ describe(ruleName, () => {
source
});
});

it("should fail when an input alias is kebab-cased and whitelisted, but the property doesn't match the alias", () => {
const source = `
@Directive({
selector: 'foo'
})
class TestDirective {
@Input('aria-invalid') ariaBusy: string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
`;
assertAnnotated({
message: getFailureMessage('TestDirective', 'ariaBusy'),
ruleName,
source
});
});
});
});

Expand Down Expand Up @@ -146,6 +163,18 @@ describe(ruleName, () => {
`;
assertSuccess(ruleName, source);
});

it('should succeed when an input alias is kebab-cased and whitelisted', () => {
const source = `
@Directive({
selector: 'foo'
})
class TestDirective {
@Input('aria-label') ariaLabel: string;
}
`;
assertSuccess(ruleName, source);
});
});
});
});