UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7
8function _helperHoistVariables() {
9 const data = _interopRequireDefault(require("@babel/helper-hoist-variables"));
10
11 _helperHoistVariables = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function t() {
19 const data = _interopRequireWildcard(require("@babel/types"));
20
21 t = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32const visitor = {
33 enter(path, state) {
34 if (path.isThisExpression()) {
35 state.foundThis = true;
36 }
37
38 if (path.isReferencedIdentifier({
39 name: "arguments"
40 })) {
41 state.foundArguments = true;
42 }
43 },
44
45 Function(path) {
46 path.skip();
47 }
48
49};
50
51function _default(path, scope = path.scope) {
52 const {
53 node
54 } = path;
55 const container = t().functionExpression(null, [], node.body, node.generator, node.async);
56 let callee = container;
57 let args = [];
58 (0, _helperHoistVariables().default)(path, id => scope.push({
59 id
60 }));
61 const state = {
62 foundThis: false,
63 foundArguments: false
64 };
65 path.traverse(visitor, state);
66
67 if (state.foundArguments || state.foundThis) {
68 callee = t().memberExpression(container, t().identifier("apply"));
69 args = [];
70
71 if (state.foundThis) {
72 args.push(t().thisExpression());
73 }
74
75 if (state.foundArguments) {
76 if (!state.foundThis) args.push(t().nullLiteral());
77 args.push(t().identifier("arguments"));
78 }
79 }
80
81 let call = t().callExpression(callee, args);
82 if (node.generator) call = t().yieldExpression(call, true);
83 return t().returnStatement(call);
84}
\No newline at end of file