-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx-no-ampersands.js
32 lines (30 loc) · 1.01 KB
/
jsx-no-ampersands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @fileoverview Prevents usage of && condition in JSX Embeds.
* @author Anees Iqbal <i@steelbrain.me>
* @see: https://github.com/yannickcr/eslint-plugin-react/blob/ee232bbaef4fb04740413d5048877559abf06222/lib/rules/jsx-embed-condition.js
* @see: https://kentcdodds.com/blog/use-ternaries-rather-than-and-and-in-jsx
*/
'use strict';
module.exports = {
// NOTE [@niculistana]: this is a slimmed down version of the export which does not include docs
create(context) {
return {
JSXExpressionContainer(node) {
if (
node.parent == null
|| node.parent.type !== 'JSXElement'
|| node.expression == null
|| node.expression.type !== 'LogicalExpression'
|| node.expression.operator === '??'
|| node.expression.operator === "||"
) {
return;
}
context.report({
node,
message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'
});
}
};
}
};