Skip to content

Commit 2fb72aa

Browse files
committed
[new] - Implement BinaryExpression expression handler.
1 parent e4da9cf commit 2fb72aa

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
import getValue from './index';
4+
5+
6+
7+
/**
8+
* Extractor function for a BinaryExpression type value node.
9+
* A binary expression has a left and right side separated by an operator
10+
* such as `a + b`.
11+
*
12+
* @param - value - AST Value object with type `BinaryExpression`
13+
* @returns - The extracted value converted to correct type.
14+
*/
15+
export default function extractValueFromBinaryExpression(value) {
16+
const { operator, left, right } = value;
17+
const leftVal = getValue(left);
18+
const rightVal = getValue(right);
19+
20+
switch (operator) {
21+
case '==':
22+
return leftVal == rightVal;
23+
case '!=':
24+
return leftVal != rightVal;
25+
case '===':
26+
return leftVal === rightVal;
27+
case '!==':
28+
return leftVal !== rightVal;
29+
case '<':
30+
return leftVal < rightVal;
31+
case '<=':
32+
return leftVal <= rightVal;
33+
case '>':
34+
return leftVal > rightVal;
35+
case '>=':
36+
return leftVal >= rightVal;
37+
case '<<':
38+
return leftVal << rightVal;
39+
case '>>':
40+
return leftVal >> rightVal;
41+
case '>>>':
42+
return leftVal >>> rightVal;
43+
case '+':
44+
return leftVal + rightVal;
45+
case '-':
46+
return leftVal - rightVal;
47+
case '*':
48+
return leftVal * rightVal;
49+
case '/':
50+
return leftVal / rightVal;
51+
case '%':
52+
return leftVal % rightVal;
53+
case '|':
54+
return leftVal | rightVal;
55+
case '^':
56+
return leftVal ^ rightVal;
57+
case '&':
58+
return leftVal & rightVal;
59+
case 'in':
60+
return leftVal in rightVal;
61+
case 'instanceof':
62+
return leftVal instanceof rightVal;
63+
default:
64+
return undefined;
65+
}
66+
}

0 commit comments

Comments
 (0)