forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
should-add-parentheses-to-logical-expression-child.js
47 lines (40 loc) · 1.19 KB
/
should-add-parentheses-to-logical-expression-child.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
/**
Check if parentheses should be added to a `node` when it's used as child of `LogicalExpression`.
@param {Node} node - The AST node to check.
@param {{operator: string, property: string}} options - Options
@returns {boolean}
*/
function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) {
// We are not using this, but we can improve this function with it
if (!property) {
throw new Error('`property` is required.');
}
if (
node.type === 'LogicalExpression'
&& node.operator === operator
) {
return false;
}
// Not really needed, but more readable
if (
node.type === 'AwaitExpression'
|| node.type === 'BinaryExpression'
) {
return true;
}
// Lower precedence than `LogicalExpression`
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
if (
node.type === 'LogicalExpression'
|| node.type === 'ConditionalExpression'
|| node.type === 'AssignmentExpression'
|| node.type === 'AssignmentExpression'
|| node.type === 'YieldExpression'
|| node.type === 'SequenceExpression'
) {
return true;
}
return false;
}
module.exports = shouldAddParenthesesToLogicalExpressionChild;