Skip to content

Commit

Permalink
Merge pull request #51 from jelhan/issue-49-support-for-aria-role
Browse files Browse the repository at this point in the history
Add support for `ariaRole` property
  • Loading branch information
Turbo87 authored Jan 20, 2020
2 parents b4db0bf + 58e9e82 commit 7c84f9e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/__tests__/__snapshots__/transform.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ foo
=========="
`;
exports[`handles \`ariaRole\` correctly 1`] = `
"==========
export default Component.extend({
ariaRole: 'button',
});
~~~~~~~~~~
foo
~~~~~~~~~~
=> tagName: div
~~~~~~~~~~
export default Component.extend({
tagName: \\"\\",
});
~~~~~~~~~~
<div role=\\"button\\" ...attributes>
foo
</div>
=========="
`;
exports[`handles \`attributeBindings\` correctly 1`] = `
"==========
Expand Down
26 changes: 26 additions & 0 deletions lib/__tests__/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ test('handles `classNameBindings` correctly', () => {
expect(generateSnapshot(source, template)).toMatchSnapshot();
});

test('handles `ariaRole` correctly', () => {
let source = `
export default Component.extend({
ariaRole: 'button',
});
`;

let template = `foo`;

expect(generateSnapshot(source, template)).toMatchSnapshot();
});

test('throws if `Component.extend({ ... })` is not found', () => {
let source = `
export default class extends Component {
Expand Down Expand Up @@ -191,6 +203,20 @@ test('throws if component is using `click()`', () => {
);
});

test('throws if component is using a computed property for `ariaRole`', () => {
let source = `
export default Component.extend({
ariaRole: computed(function() {
return 'button';
}),
});
`;

expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
`"Codemod does not support computed properties for \`ariaRole\`."`
);
});

test('multi-line template', () => {
let source = `export default Component.extend({});`;

Expand Down
22 changes: 20 additions & 2 deletions lib/transform/classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
findClassNames,
findClassNameBindings,
findAttributeBindings,
findAriaRole,
isMethod,
isProperty,
} = require('../utils');
Expand Down Expand Up @@ -124,6 +125,14 @@ module.exports = function transformClassicComponent(root, options) {
let classNameBindings = findClassNameBindings(properties);
debug('classNameBindings: %o', classNameBindings);

let ariaRole;
try {
ariaRole = findAriaRole(properties);
} catch (error) {
throw new SilentError('Codemod does not support computed properties for `ariaRole`.');
}
debug('ariaRole: %o', ariaRole);

// set `tagName: ''`
let tagNamePath = j(properties)
.find(j.ObjectProperty)
Expand All @@ -145,11 +154,20 @@ module.exports = function transformClassicComponent(root, options) {
isProperty(path, 'elementId') ||
isProperty(path, 'attributeBindings') ||
isProperty(path, 'classNames') ||
isProperty(path, 'classNameBindings')
isProperty(path, 'classNameBindings') ||
isProperty(path, 'ariaRole')
)
.remove();

let newSource = root.toSource();

return { newSource, tagName, elementId, classNames, classNameBindings, attributeBindings };
return {
newSource,
tagName,
elementId,
classNames,
classNameBindings,
attributeBindings,
ariaRole,
};
};
5 changes: 4 additions & 1 deletion lib/transform/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const PLACEHOLDER = '@@@PLACEHOLDER@@@';

module.exports = function transformTemplate(
template,
{ tagName, elementId, classNames, classNameBindings, attributeBindings },
{ tagName, elementId, classNames, classNameBindings, attributeBindings, ariaRole },
options
) {
// wrap existing template with root element
Expand All @@ -31,6 +31,9 @@ module.exports = function transformTemplate(
if (elementId) {
attrs.push(b.attr('id', b.text(elementId)));
}
if (ariaRole) {
attrs.push(b.attr('role', b.text(ariaRole)));
}
attributeBindings.forEach((value, key) => {
attrs.push(b.attr(key, b.mustache(`this.${value}`)));
});
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function findElementId(properties) {
return findStringProperty(properties, 'elementId');
}

function findAriaRole(properties) {
return findStringProperty(properties, 'ariaRole');
}

function findStringArrayProperties(properties, name) {
let propertyPath = properties.filter(path => isProperty(path, name))[0];
if (!propertyPath) {
Expand Down Expand Up @@ -99,6 +103,7 @@ module.exports = {
isMethod,
findStringProperty,
findStringArrayProperties,
findAriaRole,
findAttributeBindings,
findClassNames,
findClassNameBindings,
Expand Down

0 comments on commit 7c84f9e

Please sign in to comment.