Skip to content

Commit 1911be9

Browse files
authored
update accessibility-role typechecking (#135)
1 parent db47a25 commit 1911be9

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

__tests__/src/rules/has-valid-accessibility-role-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ ruleTester.run('has-valid-accessibility-role', rule, {
5252
{ code: '<TouchableOpacity accessibilityRole="text" />;' },
5353
{ code: '<TouchableOpacity accessibilityRole="timer" />;' },
5454
{ code: '<TouchableOpacity accessibilityRole="toolbar" />;' },
55+
{
56+
code: `<TouchableOpacity
57+
{...restProps}
58+
{...platformProps}
59+
accessibilityRole={props.accessibilityRole ? 'switch' : undefined}
60+
/>`,
61+
},
5562
].map(parserOptionsMapper),
5663
invalid: [
5764
{

src/factory/valid-prop.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ const createValidPropRule = (
3535
if (!isExpression) {
3636
// ensure we are only checking literal prop values
3737
const attrValue = getLiteralPropValue(node);
38-
let invalid = false;
38+
if (attrValue !== null) {
39+
let invalid = false;
3940

40-
if (Array.isArray(attrValue)) {
41-
const validate = attrValue.map((strValue) =>
42-
isOneOf(strValue, validValues)
43-
);
44-
invalid = validate.indexOf(false) > -1;
45-
} else {
46-
invalid = !isOneOf(attrValue, validValues);
47-
}
41+
if (Array.isArray(attrValue)) {
42+
const validate = attrValue.map((strValue) =>
43+
isOneOf(strValue, validValues)
44+
);
45+
invalid = validate.indexOf(false) > -1;
46+
} else {
47+
invalid = !isOneOf(attrValue, validValues);
48+
}
4849

49-
if (invalid) {
50-
context.report({
51-
node,
52-
message: errorMessage,
53-
});
50+
if (invalid) {
51+
context.report({
52+
node,
53+
message: errorMessage,
54+
});
55+
}
5456
}
5557
}
5658
}

src/rules/has-valid-accessibility-role.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import createValidPropRule from '../factory/valid-prop';
1313
const errorMessage = 'accessibilityRole must be one of defined values';
1414

1515
const validValues = [
16+
'togglebutton',
1617
'adjustable',
1718
'alert',
1819
'button',
@@ -36,9 +37,11 @@ const validValues = [
3637
'summary',
3738
'switch',
3839
'tab',
40+
'tabbar',
3941
'tablist',
4042
'text',
4143
'timer',
44+
'list',
4245
'toolbar',
4346
];
4447

0 commit comments

Comments
 (0)