forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes jsx-eslint#1979
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 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
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,34 @@ | ||
# Disallow && condition inside JSX Embed. (react/jsx-embed-condition) | ||
|
||
This rule disallows use of `&&` inside JSX Embeds to avoid conditional numbers from being rendered. | ||
|
||
## Why? | ||
|
||
The Official React docs warns against using `&&` in inline JSX embed expressions. The reason behind this is explained well in the [Official React docs](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator). | ||
Imagine having a var `x` with a possible value of `1` or `0`. If you write `{x && <div />}`, it'll render `<div />` when `x` is `1` but instead of rendering | ||
nothing when `x` is `0`, it'll render `0` literal. This can lead to hard to figure out bugs, especially in React Native. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<div> | ||
{x && <MyProfile />} | ||
</div> | ||
<div> | ||
{x || y && <strong>Hello</strong>} | ||
</div> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<div> | ||
{x ? <MyProfile /> : null} | ||
</div> | ||
// -- | ||
<div> | ||
{x || y ? <strong>Hello</strong> : 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,45 @@ | ||
/** | ||
* @fileoverview Prevents usage of && condition in JSX Embeds. | ||
* @author Anees Iqbal <[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const docsUrl = require('../util/docsUrl'); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ----------------------------------------------------------------------------- | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prevents usage of && condition in JSX embed', | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: docsUrl('jsx-embed-condition') | ||
}, | ||
|
||
schema: [] | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXExpressionContainer(node) { | ||
if ( | ||
node.parent == null | ||
|| node.parent.type !== 'JSXElement' | ||
|| node.expression == null | ||
|| node.expression.type !== 'LogicalExpression' | ||
|| node.expression.operator === '??' | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead' | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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,70 @@ | ||
/** | ||
* @fileoverview Prevents usage of && condition in JSX Embeds. | ||
* @author Anees Iqbal <[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/jsx-embed-condition'); | ||
const parsers = require('../../helpers/parsers'); | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('jsx-embed-condition', rule, { | ||
valid: [].concat({ | ||
code: '<App>Test</App>' | ||
}, { | ||
code: '<App test>Another</App>' | ||
}, { | ||
code: '<App foo={e => bar(e)}>Hello World</App>' | ||
}, { | ||
code: '<App>{x ? <div></div> : null}</App>' | ||
}, { | ||
code: '<App>{x ? <div>Hello</div> : null}</App>' | ||
}, { | ||
code: '<App>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>' | ||
}, { | ||
code: '<App x={x && y}>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>' | ||
}, semver.satisfies(version, '<= 5' ? [] : [{ | ||
code: '<App test>{x ?? <div />}</App>', | ||
parserOptions: { | ||
ecmaVersion: 2020 | ||
} | ||
}, { | ||
code: '<App test>{x ?? <div />}</App>', | ||
parser: parsers.TYPESCRIPT_ESLINT | ||
}, { | ||
code: '<App test>{x ?? <div />}</App>', | ||
parser: parsers['@TYPEDCRIPT_ESLINT'] | ||
}]), | ||
|
||
invalid: [{ | ||
code: '<div>{x && <div />}</div>', | ||
output: '<div>{x && <div />}</div>', | ||
errors: [ | ||
{message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'} | ||
] | ||
}, { | ||
code: '<div>{x ? <div>{y && <div />}</div> : null}</div>', | ||
output: '<div>{x ? <div>{y && <div />}</div> : null}</div>', | ||
errors: [ | ||
{message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'} | ||
] | ||
}] | ||
}); |