UnaryExpression.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = extractValueFromUnaryExpression;
  6. /**
  7. * Extractor function for a UnaryExpression type value node.
  8. * A unary expression is an expression with a unary operator.
  9. * For example, !"foobar" will evaluate to false, so this will return false.
  10. *
  11. * @param - value - AST Value object with type `UnaryExpression`
  12. * @returns - The extracted value converted to correct type.
  13. */
  14. function extractValueFromUnaryExpression(value) {
  15. // eslint-disable-next-line global-require
  16. var getValue = require('./index.js').default;
  17. var operator = value.operator,
  18. argument = value.argument;
  19. switch (operator) {
  20. case '-':
  21. return -getValue(argument);
  22. case '+':
  23. return +getValue(argument); // eslint-disable-line no-implicit-coercion
  24. case '!':
  25. return !getValue(argument);
  26. case '~':
  27. return ~getValue(argument); // eslint-disable-line no-bitwise
  28. case 'delete':
  29. // I believe delete statements evaluate to true.
  30. return true;
  31. case 'typeof':
  32. case 'void':
  33. default:
  34. return undefined;
  35. }
  36. }