index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*istanbul ignore next*/"use strict";
  2. exports.__esModule = true;
  3. exports.default = function (path) {
  4. /*istanbul ignore next*/var scope = arguments.length <= 1 || arguments[1] === undefined ? path.scope : arguments[1];
  5. /*istanbul ignore next*/var node = path.node;
  6. var container = t.functionExpression(null, [], node.body, node.generator, node.async);
  7. var callee = container;
  8. var args = [];
  9. // todo: only hoist if necessary
  10. /*istanbul ignore next*/(0, _babelHelperHoistVariables2.default)(path, function (id) /*istanbul ignore next*/{
  11. return scope.push({ id: id });
  12. });
  13. var state = {
  14. foundThis: false,
  15. foundArguments: false
  16. };
  17. path.traverse(visitor, state);
  18. if (state.foundArguments) {
  19. callee = t.memberExpression(container, t.identifier("apply"));
  20. args = [];
  21. if (state.foundThis) {
  22. args.push(t.thisExpression());
  23. }
  24. if (state.foundArguments) {
  25. if (!state.foundThis) args.push(t.nullLiteral());
  26. args.push(t.identifier("arguments"));
  27. }
  28. }
  29. var call = t.callExpression(callee, args);
  30. if (node.generator) call = t.yieldExpression(call, true);
  31. return t.returnStatement(call);
  32. };
  33. var /*istanbul ignore next*/_babelHelperHoistVariables = require("babel-helper-hoist-variables");
  34. /*istanbul ignore next*/
  35. var _babelHelperHoistVariables2 = _interopRequireDefault(_babelHelperHoistVariables);
  36. var /*istanbul ignore next*/_babelTypes = require("babel-types");
  37. /*istanbul ignore next*/
  38. var t = _interopRequireWildcard(_babelTypes);
  39. /*istanbul ignore next*/
  40. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  41. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  42. var visitor = { /*istanbul ignore next*/
  43. enter: function enter(path, state) {
  44. if (path.isThisExpression()) {
  45. state.foundThis = true;
  46. }
  47. if (path.isReferencedIdentifier({ name: "arguments" })) {
  48. state.foundArguments = true;
  49. }
  50. },
  51. /*istanbul ignore next*/Function: function Function(path) {
  52. path.skip();
  53. }
  54. };
  55. /*istanbul ignore next*/module.exports = exports["default"];