LogicalExpression.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = extractValueFromLogicalExpression;
  6. /**
  7. * Extractor function for a LogicalExpression type value node.
  8. * A logical expression is `a && b` or `a || b`, so we evaluate both sides
  9. * and return the extracted value of the expression.
  10. *
  11. * @param - value - AST Value object with type `LogicalExpression`
  12. * @returns - The extracted value converted to correct type.
  13. */
  14. function extractValueFromLogicalExpression(value) {
  15. // eslint-disable-next-line global-require
  16. var getValue = require('./index.js').default;
  17. var operator = value.operator,
  18. left = value.left,
  19. right = value.right;
  20. var leftVal = getValue(left);
  21. var rightVal = getValue(right);
  22. if (operator === '&&') {
  23. return leftVal && rightVal;
  24. }
  25. if (operator === '??') {
  26. // return leftVal ?? rightVal; // TODO: update to babel 7
  27. return leftVal === null || typeof leftVal === 'undefined' ? rightVal : leftVal;
  28. }
  29. return leftVal || rightVal;
  30. }