BindExpression.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = extractValueFromBindExpression;
  6. /**
  7. * Extractor function for a BindExpression type value node.
  8. * A bind expression looks like `::this.foo`
  9. * This will return `this.foo.bind(this)` as the value to indicate its existence,
  10. * since we can not execute the function this.foo.bind(this) in a static environment.
  11. *
  12. * @param - value - AST Value object with type `BindExpression`
  13. * @returns - The extracted value converted to correct type.
  14. */
  15. function extractValueFromBindExpression(value) {
  16. // eslint-disable-next-line global-require
  17. var getValue = require('./index.js').default;
  18. var callee = getValue(value.callee);
  19. // If value.object === null, the callee must be a MemberExpression.
  20. // https://github.com/babel/babylon/blob/master/ast/spec.md#bindexpression
  21. var object = value.object === null ? getValue(value.callee.object) : getValue(value.object);
  22. if (value.object && value.object.property) {
  23. return object + '.' + callee + '.bind(' + object + ')';
  24. }
  25. return callee + '.bind(' + object + ')';
  26. }