-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce JSX inline conditional as a ternary
- Loading branch information
Showing
4 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
), | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
}, | ||
{ | ||
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', | ||
}, | ||
], | ||
}, | ||
]), | ||
}); |