Skip to content

Commit 58e9e82

Browse files
jelhanTurbo87
authored andcommitted
add support for ariaRole property
1 parent b4db0bf commit 58e9e82

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

lib/__tests__/__snapshots__/transform.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ foo
2323
=========="
2424
`;
2525
26+
exports[`handles \`ariaRole\` correctly 1`] = `
27+
"==========
28+
29+
export default Component.extend({
30+
ariaRole: 'button',
31+
});
32+
33+
~~~~~~~~~~
34+
foo
35+
~~~~~~~~~~
36+
=> tagName: div
37+
~~~~~~~~~~
38+
39+
export default Component.extend({
40+
tagName: \\"\\",
41+
});
42+
43+
~~~~~~~~~~
44+
<div role=\\"button\\" ...attributes>
45+
foo
46+
</div>
47+
=========="
48+
`;
49+
2650
exports[`handles \`attributeBindings\` correctly 1`] = `
2751
"==========
2852

lib/__tests__/transform.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ test('handles `classNameBindings` correctly', () => {
9999
expect(generateSnapshot(source, template)).toMatchSnapshot();
100100
});
101101

102+
test('handles `ariaRole` correctly', () => {
103+
let source = `
104+
export default Component.extend({
105+
ariaRole: 'button',
106+
});
107+
`;
108+
109+
let template = `foo`;
110+
111+
expect(generateSnapshot(source, template)).toMatchSnapshot();
112+
});
113+
102114
test('throws if `Component.extend({ ... })` is not found', () => {
103115
let source = `
104116
export default class extends Component {
@@ -191,6 +203,20 @@ test('throws if component is using `click()`', () => {
191203
);
192204
});
193205

206+
test('throws if component is using a computed property for `ariaRole`', () => {
207+
let source = `
208+
export default Component.extend({
209+
ariaRole: computed(function() {
210+
return 'button';
211+
}),
212+
});
213+
`;
214+
215+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
216+
`"Codemod does not support computed properties for \`ariaRole\`."`
217+
);
218+
});
219+
194220
test('multi-line template', () => {
195221
let source = `export default Component.extend({});`;
196222

lib/transform/classic.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
findClassNames,
99
findClassNameBindings,
1010
findAttributeBindings,
11+
findAriaRole,
1112
isMethod,
1213
isProperty,
1314
} = require('../utils');
@@ -124,6 +125,14 @@ module.exports = function transformClassicComponent(root, options) {
124125
let classNameBindings = findClassNameBindings(properties);
125126
debug('classNameBindings: %o', classNameBindings);
126127

128+
let ariaRole;
129+
try {
130+
ariaRole = findAriaRole(properties);
131+
} catch (error) {
132+
throw new SilentError('Codemod does not support computed properties for `ariaRole`.');
133+
}
134+
debug('ariaRole: %o', ariaRole);
135+
127136
// set `tagName: ''`
128137
let tagNamePath = j(properties)
129138
.find(j.ObjectProperty)
@@ -145,11 +154,20 @@ module.exports = function transformClassicComponent(root, options) {
145154
isProperty(path, 'elementId') ||
146155
isProperty(path, 'attributeBindings') ||
147156
isProperty(path, 'classNames') ||
148-
isProperty(path, 'classNameBindings')
157+
isProperty(path, 'classNameBindings') ||
158+
isProperty(path, 'ariaRole')
149159
)
150160
.remove();
151161

152162
let newSource = root.toSource();
153163

154-
return { newSource, tagName, elementId, classNames, classNameBindings, attributeBindings };
164+
return {
165+
newSource,
166+
tagName,
167+
elementId,
168+
classNames,
169+
classNameBindings,
170+
attributeBindings,
171+
ariaRole,
172+
};
155173
};

lib/transform/template.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const PLACEHOLDER = '@@@PLACEHOLDER@@@';
88

99
module.exports = function transformTemplate(
1010
template,
11-
{ tagName, elementId, classNames, classNameBindings, attributeBindings },
11+
{ tagName, elementId, classNames, classNameBindings, attributeBindings, ariaRole },
1212
options
1313
) {
1414
// wrap existing template with root element
@@ -31,6 +31,9 @@ module.exports = function transformTemplate(
3131
if (elementId) {
3232
attrs.push(b.attr('id', b.text(elementId)));
3333
}
34+
if (ariaRole) {
35+
attrs.push(b.attr('role', b.text(ariaRole)));
36+
}
3437
attributeBindings.forEach((value, key) => {
3538
attrs.push(b.attr(key, b.mustache(`this.${value}`)));
3639
});

lib/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function findElementId(properties) {
3434
return findStringProperty(properties, 'elementId');
3535
}
3636

37+
function findAriaRole(properties) {
38+
return findStringProperty(properties, 'ariaRole');
39+
}
40+
3741
function findStringArrayProperties(properties, name) {
3842
let propertyPath = properties.filter(path => isProperty(path, name))[0];
3943
if (!propertyPath) {
@@ -99,6 +103,7 @@ module.exports = {
99103
isMethod,
100104
findStringProperty,
101105
findStringArrayProperties,
106+
findAriaRole,
102107
findAttributeBindings,
103108
findClassNames,
104109
findClassNameBindings,

0 commit comments

Comments
 (0)