Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce JSX inline conditional as a ternary #3318

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/rules/jsx-inline-conditional.md
@@ -0,0 +1,33 @@
# Enforce JSX inline conditional as a ternary (react/jsx-inline-conditional)

This rule helps avoid common rendering bugs where the left side of an inline conditional is falsy (e.g. zero) and renders the value of the condition (e.g. `0`) instead of nothing. See the note in the [official React docs](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator).

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
Fixer will fix whitespace and tabs indentation.

## Rule Details

This rule is aimed to enforce consistent indentation style. The default style is `4 spaces`.

Examples of **incorrect** code for this rule:

```jsx
<div>
{someCondition && <SomeComponent />}
</div>
<div>
{someCondition || someOtherCondition && <SomeComponent />}
</div>
```

Examples of **correct** code for this rule:

```jsx
<div>
{someCondition ? <SomeComponent /> : null}
</div>
// --
<div>
{someCondition || someOtherCondition ? <SomeComponent /> : null}
</div>
```
14 changes: 5 additions & 9 deletions index.js
Expand Up @@ -30,6 +30,7 @@ const allRules = {
'jsx-handler-names': require('./lib/rules/jsx-handler-names'),
'jsx-indent': require('./lib/rules/jsx-indent'),
'jsx-indent-props': require('./lib/rules/jsx-indent-props'),
'jsx-inline-conditional': require('./lib/rules/jsx-inline-conditional'),
'jsx-key': require('./lib/rules/jsx-key'),
'jsx-max-depth': require('./lib/rules/jsx-max-depth'),
'jsx-max-props-per-line': require('./lib/rules/jsx-max-props-per-line'),
Expand Down Expand Up @@ -124,16 +125,15 @@ module.exports = {
rules: allRules,
configs: {
recommended: {
plugins: [
'react',
],
plugins: ['react'],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
rules: {
'react/display-name': 2,
'react/jsx-inline-conditional': 2,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is helps identify+fix a certain class of rendering bugs and isn't strictly stylistic, I enabled this by default. Let me know if I should change this!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a rule to the recommended config is a breaking change, so we'll basically never be doing that. Please remove it.

'react/jsx-key': 2,
'react/jsx-no-comment-textnodes': 2,
'react/jsx-no-duplicate-props': 2,
Expand All @@ -158,9 +158,7 @@ module.exports = {
},
},
all: {
plugins: [
'react',
],
plugins: ['react'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert unrelated formatting changes; if perhaps you're running prettier on repos that do not use it, please don't do that either :-)

parserOptions: {
ecmaFeatures: {
jsx: true,
Expand All @@ -169,9 +167,7 @@ module.exports = {
rules: activeRulesConfig,
},
'jsx-runtime': {
plugins: [
'react',
],
plugins: ['react'],
parserOptions: {
ecmaFeatures: {
jsx: true,
Expand Down
55 changes: 55 additions & 0 deletions lib/rules/jsx-inline-conditional.js
@@ -0,0 +1,55 @@
/**
* @fileoverview Enforce JSX inline conditional as a ternary
* @author Kevin Ingersoll
*/

'use strict';

const docsUrl = require('../util/docsUrl');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const messages = {
inlineConditional: 'Conditional rendering in JSX should use a full ternary expression to avoid unintentionally rendering falsy values (i.e. zero)',
};

module.exports = {
meta: {
docs: {
description: 'Enforce JSX inline conditional as a ternary',
category: 'Possible Errors',
recommended: true,
url: docsUrl('jsx-inline-conditional'),
},
fixable: 'code',
messages,
schema: [],
},

create(context) {
const sourceCode = context.getSourceCode();

return {
JSXExpressionContainer(node) {
if (
node.expression.type === 'LogicalExpression'
&& node.expression.operator === '&&'
&& node.expression.right.type === 'JSXElement'
) {
context.report({
node,
messageId: 'inlineConditional',
fix: (fixer) => fixer.replaceText(
node,
`{${sourceCode.getText(
node.expression.left
)} ? ${sourceCode.getText(node.expression.right)} : null}`
),
});
}
},
};
},
};
76 changes: 76 additions & 0 deletions tests/lib/rules/jsx-inline-conditional.js
@@ -0,0 +1,76 @@
/**
* @fileoverview Enforce JSX inline conditional as a ternary
* @author Kevin Ingersoll
*/

'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-inline-conditional');

const parsers = require('../../helpers/parsers');

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
};

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({ parserOptions });
ruleTester.run('jsx-inline-conditional', rule, {
valid: parsers.all([
{ code: '<div>{someCondition ? <div></div> : null}</div>' },
{ code: '<div>{someCondition ? <SomeComponent /> : null}</div>' },
{
code: '<div>{someCondition ? <div>{anotherCondition ? <SomeComponent /> : null}</div> : null}</div>',
},
{
code: '<div>{someCondition && someOtherCondition ? <SomeComponent /> : null}</div>',
},
{
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parserOptions: {
ecmaVersion: 2020,
},
Comment on lines +41 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parserOptions: {
ecmaVersion: 2020,
},
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
features: ['nullish coalescing'],

and then we'll need to add that feature to the parsers.all helper

},
{
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parser: parsers.TYPESCRIPT_ESLINT,
},
{
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parser: parsers['@TYPESCRIPT_ESLINT'],
},
Comment on lines +46 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should not be needed, since the parsers.all helper should automatically run every test case in every parser.

Suggested change
{
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parser: parsers.TYPESCRIPT_ESLINT,
},
{
code: '<div>{possiblyNull ?? <SomeComponent />}</div>',
parser: parsers['@TYPESCRIPT_ESLINT'],
},

]),
invalid: parsers.all([
{
code: '<div>{someCondition && <SomeComponent />}</div>',
output: '<div>{someCondition ? <SomeComponent /> : null}</div>',
errors: [
{
messageId: 'inlineConditional',
},
],
},
{
code: '<div>{someCondition && someOtherCondition && <SomeComponent />}</div>',
output:
'<div>{someCondition && someOtherCondition ? <SomeComponent /> : null}</div>',
errors: [
{
messageId: 'inlineConditional',
},
],
},
]),
});