index.js 1.9 KB

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