diff --git a/__tests__/src/rules/no-static-element-interactions-test.js b/__tests__/src/rules/no-static-element-interactions-test.js index 214e3903c..7cc1a937a 100644 --- a/__tests__/src/rules/no-static-element-interactions-test.js +++ b/__tests__/src/rules/no-static-element-interactions-test.js @@ -429,6 +429,11 @@ ruleTester.run(`${ruleName}:recommended`, rule, { // Expressions should pass in recommended mode { code: '
{}} />;' }, { code: '
this.handleKeyPress(e)}>{this.props.children}
' }, + // Specific case for ternary operator with literals on both side + { + code: '
{}} />;', + options: [{ allowExpressionValues: true }], + }, ) .map(ruleOptionsMapperFactory(recommendedOptions)) .map(parserOptionsMapper), @@ -465,5 +470,11 @@ ruleTester.run(`${ruleName}:strict`, rule, { // Expressions should fail in strict mode { code: '
{}} />;', errors: [expectedError] }, { code: '
this.handleKeyPress(e)}>{this.props.children}
', errors: [expectedError] }, + // Specific case for ternary operator with literals on both side + { + code: '
{}} />;', + options: [{ allowExpressionValues: false }], + errors: [expectedError], + }, ).map(parserOptionsMapper), }); diff --git a/src/rules/no-static-element-interactions.js b/src/rules/no-static-element-interactions.js index 5fd12ee17..fc0a8a590 100644 --- a/src/rules/no-static-element-interactions.js +++ b/src/rules/no-static-element-interactions.js @@ -58,6 +58,7 @@ export default ({ JSXOpeningElement: (node: JSXOpeningElement) => { const { attributes } = node; const type = elementType(node); + const { allowExpressionValues, handlers = defaultInteractiveProps, @@ -99,7 +100,18 @@ export default ({ allowExpressionValues === true && isNonLiteralProperty(attributes, 'role') ) { - // This rule has no opinion about non-literal roles. + // 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; }