Skip to content

Commit 38405ad

Browse files
V2dhaljharb
authored andcommittedJul 7, 2022
[Fix] no-interactive-tabindex: allow role assignments using a ternary with literals on both sides
1 parent f0e04ce commit 38405ad

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

‎__tests__/src/rules/no-noninteractive-tabindex-test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,28 @@ ruleTester.run(`${ruleName}:recommended`, rule, {
6565
valid: [
6666
...alwaysValid,
6767
{ code: '<div role="tabpanel" tabIndex="0" />' },
68-
// Expressions should fail in strict mode
68+
// Expressions should pass in recommended mode
6969
{ code: '<div role={ROLE_BUTTON} onClick={() => {}} tabIndex="0" />;' },
70+
// Cases for allowExpressionValues set to true
71+
{
72+
code: '<div role={BUTTON} onClick={() => {}} tabIndex="0" />;',
73+
options: [{ allowExpressionValues: true }],
74+
},
75+
// Specific case for ternary operator with literals on both side
76+
{
77+
code: '<div role={isButton ? "button" : "link"} onClick={() => {}} tabIndex="0" />;',
78+
options: [{ allowExpressionValues: true }],
79+
},
80+
{
81+
code: '<div role={isButton ? "button" : LINK} onClick={() => {}} tabIndex="0" />;',
82+
options: [{ allowExpressionValues: true }],
83+
errors: [expectedError],
84+
},
85+
{
86+
code: '<div role={isButton ? BUTTON : LINK} onClick={() => {}} tabIndex="0"/>;',
87+
options: [{ allowExpressionValues: true }],
88+
errors: [expectedError],
89+
},
7090
]
7191
.map(ruleOptionsMapperFactory(recommendedOptions))
7292
.map(parserOptionsMapper),
@@ -86,5 +106,17 @@ ruleTester.run(`${ruleName}:strict`, rule, {
86106
{ code: '<div role="tabpanel" tabIndex="0" />', errors: [expectedError] },
87107
// Expressions should fail in strict mode
88108
{ code: '<div role={ROLE_BUTTON} onClick={() => {}} tabIndex="0" />;', errors: [expectedError] },
109+
// Cases for allowExpressionValues set to false
110+
{
111+
code: '<div role={BUTTON} onClick={() => {}} tabIndex="0" />;',
112+
options: [{ allowExpressionValues: false }],
113+
errors: [expectedError],
114+
},
115+
// Specific case for ternary operator with literals on both side
116+
{
117+
code: '<div role={isButton ? "button" : "link"} onClick={() => {}} tabIndex="0" />;',
118+
options: [{ allowExpressionValues: false }],
119+
errors: [expectedError],
120+
},
89121
].map(parserOptionsMapper),
90122
});

‎src/rules/no-noninteractive-tabindex.js

+12
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ export default ({
8282
allowExpressionValues === true
8383
&& isNonLiteralProperty(attributes, 'role')
8484
) {
85+
// Special case if role is assigned using ternary with literals on both side
86+
const roleProp = getProp(attributes, 'role');
87+
if (roleProp && roleProp.type === 'JSXAttribute' && roleProp.value.type === 'JSXExpressionContainer') {
88+
if (roleProp.value.expression.type === 'ConditionalExpression') {
89+
if (
90+
roleProp.value.expression.consequent.type === 'Literal'
91+
&& roleProp.value.expression.alternate.type === 'Literal'
92+
) {
93+
return;
94+
}
95+
}
96+
}
8597
return;
8698
}
8799
if (

0 commit comments

Comments
 (0)
Please sign in to comment.