diff --git a/__tests__/src/rules/no-noninteractive-tabindex-test.js b/__tests__/src/rules/no-noninteractive-tabindex-test.js index f5c4f25bd..2c8f1ac4b 100644 --- a/__tests__/src/rules/no-noninteractive-tabindex-test.js +++ b/__tests__/src/rules/no-noninteractive-tabindex-test.js @@ -65,8 +65,28 @@ ruleTester.run(`${ruleName}:recommended`, rule, { valid: [ ...alwaysValid, { code: '
' }, - // Expressions should fail in strict mode + // Expressions should pass in recommended mode { code: '
{}} tabIndex="0" />;' }, + // Cases for allowExpressionValues set to true + { + code: '
{}} tabIndex="0" />;', + options: [{ allowExpressionValues: true }], + }, + // Specific case for ternary operator with literals on both side + { + code: '
{}} tabIndex="0" />;', + options: [{ allowExpressionValues: true }], + }, + { + code: '
{}} tabIndex="0" />;', + options: [{ allowExpressionValues: true }], + errors: [expectedError], + }, + { + code: '
{}} tabIndex="0"/>;', + options: [{ allowExpressionValues: true }], + errors: [expectedError], + }, ] .map(ruleOptionsMapperFactory(recommendedOptions)) .map(parserOptionsMapper), @@ -86,5 +106,17 @@ ruleTester.run(`${ruleName}:strict`, rule, { { code: '
', errors: [expectedError] }, // Expressions should fail in strict mode { code: '
{}} tabIndex="0" />;', errors: [expectedError] }, + // Cases for allowExpressionValues set to false + { + code: '
{}} tabIndex="0" />;', + options: [{ allowExpressionValues: false }], + errors: [expectedError], + }, + // Specific case for ternary operator with literals on both side + { + code: '
{}} tabIndex="0" />;', + options: [{ allowExpressionValues: false }], + errors: [expectedError], + }, ].map(parserOptionsMapper), }); diff --git a/src/rules/no-noninteractive-tabindex.js b/src/rules/no-noninteractive-tabindex.js index 88f1b023c..78f605ea0 100644 --- a/src/rules/no-noninteractive-tabindex.js +++ b/src/rules/no-noninteractive-tabindex.js @@ -82,6 +82,18 @@ export default ({ allowExpressionValues === true && isNonLiteralProperty(attributes, 'role') ) { + // Special case if role is assigned using ternary with literals on both side + const roleProp = getProp(attributes, 'role'); + if (roleProp && roleProp.type === 'JSXAttribute' && roleProp.value.type === 'JSXExpressionContainer') { + if (roleProp.value.expression.type === 'ConditionalExpression') { + if ( + roleProp.value.expression.consequent.type === 'Literal' + && roleProp.value.expression.alternate.type === 'Literal' + ) { + return; + } + } + } return; } if (