[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = extractValueFromBinaryExpression;
|
---|
| 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 | function extractValueFromBinaryExpression(value) {
|
---|
| 16 | // eslint-disable-next-line global-require
|
---|
| 17 | var getValue = require('.').default;
|
---|
| 18 | var operator = value.operator,
|
---|
| 19 | left = value.left,
|
---|
| 20 | right = value.right;
|
---|
| 21 |
|
---|
| 22 | var leftVal = getValue(left);
|
---|
| 23 | var rightVal = getValue(right);
|
---|
| 24 |
|
---|
| 25 | switch (operator) {
|
---|
| 26 | case '==':
|
---|
| 27 | return leftVal == rightVal; // eslint-disable-line
|
---|
| 28 | case '!=':
|
---|
| 29 | return leftVal != rightVal; // eslint-disable-line
|
---|
| 30 | case '===':
|
---|
| 31 | return leftVal === rightVal;
|
---|
| 32 | case '!==':
|
---|
| 33 | return leftVal !== rightVal;
|
---|
| 34 | case '<':
|
---|
| 35 | return leftVal < rightVal;
|
---|
| 36 | case '<=':
|
---|
| 37 | return leftVal <= rightVal;
|
---|
| 38 | case '>':
|
---|
| 39 | return leftVal > rightVal;
|
---|
| 40 | case '>=':
|
---|
| 41 | return leftVal >= rightVal;
|
---|
| 42 | case '<<':
|
---|
| 43 | return leftVal << rightVal; // eslint-disable-line no-bitwise
|
---|
| 44 | case '>>':
|
---|
| 45 | return leftVal >> rightVal; // eslint-disable-line no-bitwise
|
---|
| 46 | case '>>>':
|
---|
| 47 | return leftVal >>> rightVal; // eslint-disable-line no-bitwise
|
---|
| 48 | case '+':
|
---|
| 49 | return leftVal + rightVal;
|
---|
| 50 | case '-':
|
---|
| 51 | return leftVal - rightVal;
|
---|
| 52 | case '*':
|
---|
| 53 | return leftVal * rightVal;
|
---|
| 54 | case '/':
|
---|
| 55 | return leftVal / rightVal;
|
---|
| 56 | case '%':
|
---|
| 57 | return leftVal % rightVal;
|
---|
| 58 | case '|':
|
---|
| 59 | return leftVal | rightVal; // eslint-disable-line no-bitwise
|
---|
| 60 | case '^':
|
---|
| 61 | return leftVal ^ rightVal; // eslint-disable-line no-bitwise
|
---|
| 62 | case '&':
|
---|
| 63 | return leftVal & rightVal; // eslint-disable-line no-bitwise
|
---|
| 64 | case 'in':
|
---|
| 65 | try {
|
---|
| 66 | return leftVal in rightVal;
|
---|
| 67 | } catch (err) {
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
| 70 | case 'instanceof':
|
---|
| 71 | if (typeof rightVal !== 'function') {
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 | return leftVal instanceof rightVal;
|
---|
| 75 | default:
|
---|
| 76 | return undefined;
|
---|
| 77 | }
|
---|
| 78 | } |
---|