forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean.js
131 lines (118 loc) · 2.95 KB
/
boolean.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const isLogicalExpression = require('./is-logical-expression.js');
const isLogicNot = node =>
node.type === 'UnaryExpression' &&
node.operator === '!';
const isLogicNotArgument = node =>
isLogicNot(node.parent) &&
node.parent.argument === node;
const isBooleanCallArgument = node =>
isBooleanCall(node.parent) &&
node.parent.arguments[0] === node;
const isBooleanCall = node =>
node.type === 'CallExpression' &&
!node.optional &&
node.callee.type === 'Identifier' &&
node.callee.name === 'Boolean' &&
node.arguments.length === 1;
const isObjectIsCall = node =>
node.type === 'CallExpression' &&
!node.optional &&
node.callee.type === 'MemberExpression' &&
!node.callee.computed &&
!node.callee.optional &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'Object' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'is';
const isVueBooleanAttributeValue = node =>
node.type === 'VExpressionContainer' &&
node.parent.type === 'VAttribute' &&
node.parent.directive &&
node.parent.value === node &&
node.parent.key.type === 'VDirectiveKey' &&
node.parent.key.name.type === 'VIdentifier' &&
(
node.parent.key.name.rawName === 'if' ||
node.parent.key.name.rawName === 'else-if' ||
node.parent.key.name.rawName === 'show'
);
const isBooleanLiteral = node =>
node.type === 'Literal' &&
typeof node.value === 'boolean';
// https://github.com/estree/estree/blob/master/es5.md#binaryoperator
const comparisonOperators = new Set([
'==',
'!=',
'===',
'!==',
'<',
'<=',
'>',
'>=',
'in',
'instanceof'
]);
const isComparison = node =>
node.type === 'BinaryExpression' &&
comparisonOperators.has(node.operator);
/**
Check if the value of node is a `boolean`.
@param {Node} node
@returns {boolean}
*/
function isBooleanNode(node) {
if (
isLogicNot(node) ||
isLogicNotArgument(node) ||
isBooleanCall(node) ||
isBooleanCallArgument(node) ||
isBooleanLiteral(node) ||
isComparison(node) ||
isObjectIsCall(node)
) {
return true;
}
const {parent} = node;
if (isVueBooleanAttributeValue(parent)) {
return true;
}
if (
(
parent.type === 'IfStatement' ||
parent.type === 'ConditionalExpression' ||
parent.type === 'WhileStatement' ||
parent.type === 'DoWhileStatement' ||
parent.type === 'ForStatement'
) &&
parent.test === node
) {
return true;
}
if (isLogicalExpression(parent)) {
return isBooleanNode(parent);
}
return false;
}
/**
Get the boolean type-casting ancestor.
@typedef {{ node: Node, isNegative: boolean }} Result
@param {Node} node
@returns {Result}
*/
function getBooleanAncestor(node) {
let isNegative = false;
// eslint-disable-next-line no-constant-condition
while (true) {
if (isLogicNotArgument(node)) {
isNegative = !isNegative;
node = node.parent;
} else if (isBooleanCallArgument(node)) {
node = node.parent;
} else {
break;
}
}
return {node, isNegative};
}
module.exports = {isBooleanNode, getBooleanAncestor};