Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-cows-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/eslint-plugin': minor
---

extend jsx-no-hardcoded-content to handle ternary and logical expressions
6 changes: 6 additions & 0 deletions packages/eslint-plugin/lib/rules/jsx-no-hardcoded-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ function checkContent(
!isEmptyString(contentNode.value)) ||
(!allowNumbers && typeof contentNode.value === 'number'))) ||
(contentNode.type === 'TemplateLiteral' && !allowStrings) ||
(contentNode.type === 'ConditionalExpression' &&
(isInvalidContent(contentNode.consequent) ||
isInvalidContent(contentNode.alternate))) ||
(contentNode.type === 'LogicalExpression' &&
(isInvalidContent(contentNode.left) ||
isInvalidContent(contentNode.right))) ||
(contentNode.type === 'JSXExpressionContainer' &&
isInvalidContent(contentNode.expression))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ ruleTester.run('jsx-no-hardcoded-content', rule, {
code: '<MyComponent foo={`bar`} />',
options: [{...checkProps, ...allowStrings}],
},
{
code: '<MyComponent>{foo ? "Content" : "Other Content"}</MyComponent>',
options: [{...checkProps, ...allowStrings}],
},
{
code: '<MyComponent>{foo && "Content"}</MyComponent>',
options: [{...checkProps, ...allowStrings}],
},
{
code: '<MyComponent>{42}</MyComponent>',
options: [
Expand Down Expand Up @@ -426,6 +434,18 @@ ruleTester.run('jsx-no-hardcoded-content', rule, {
options: [checkProps],
errors: errorsFor('MyComponent', 'foo'),
},
{
code: '<MyComponent>{foo ? "Content" : "Other Content"}</MyComponent>',
errors: errorsFor('MyComponent', 'children'),
},
{
code: '<MyComponent>{foo && "Content"}</MyComponent>',
errors: errorsFor('MyComponent', 'children'),
},
{
code: '<MyComponent>{foo || "Content"}</MyComponent>',
errors: errorsFor('MyComponent', 'children'),
},
{
code: `
import {MyComponent} from 'my-module';
Expand Down