UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _helperPluginUtils() {
9 const data = require("@babel/helper-plugin-utils");
10
11 _helperPluginUtils = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _pluginSyntaxPartialApplication() {
19 const data = _interopRequireDefault(require("@babel/plugin-syntax-partial-application"));
20
21 _pluginSyntaxPartialApplication = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _core() {
29 const data = require("@babel/core");
30
31 _core = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
40var _default = (0, _helperPluginUtils().declare)(api => {
41 api.assertVersion(7);
42
43 function hasArgumentPlaceholder(node) {
44 return node.arguments.some(arg => _core().types.isArgumentPlaceholder(arg));
45 }
46
47 function unwrapArguments(node, scope) {
48 const init = [];
49
50 for (let i = 0; i < node.arguments.length; i++) {
51 if (!_core().types.isArgumentPlaceholder(node.arguments[i]) && !_core().types.isImmutable(node.arguments[i])) {
52 const id = scope.generateUidIdentifierBasedOnNode(node.arguments[i], "param");
53 scope.push({
54 id
55 });
56
57 if (_core().types.isSpreadElement(node.arguments[i])) {
58 init.push(_core().types.assignmentExpression("=", _core().types.cloneNode(id), _core().types.arrayExpression([_core().types.spreadElement(node.arguments[i].argument)])));
59 node.arguments[i].argument = _core().types.cloneNode(id);
60 } else {
61 init.push(_core().types.assignmentExpression("=", _core().types.cloneNode(id), node.arguments[i]));
62 node.arguments[i] = _core().types.cloneNode(id);
63 }
64 }
65 }
66
67 return init;
68 }
69
70 function replacePlaceholders(node, scope) {
71 const placeholders = [];
72 const args = [];
73 node.arguments.forEach(arg => {
74 if (_core().types.isArgumentPlaceholder(arg)) {
75 const id = scope.generateUid("_argPlaceholder");
76 placeholders.push(_core().types.identifier(id));
77 args.push(_core().types.identifier(id));
78 } else {
79 args.push(arg);
80 }
81 });
82 return [placeholders, args];
83 }
84
85 return {
86 name: "proposal-partial-application",
87 inherits: _pluginSyntaxPartialApplication().default,
88 visitor: {
89 CallExpression(path) {
90 if (!hasArgumentPlaceholder(path.node)) {
91 return;
92 }
93
94 const {
95 node,
96 scope
97 } = path;
98 const functionLVal = path.scope.generateUidIdentifierBasedOnNode(node.callee);
99 const sequenceParts = [];
100 const argsInitializers = unwrapArguments(node, scope);
101 const [placeholdersParams, args] = replacePlaceholders(node, scope);
102 scope.push({
103 id: functionLVal
104 });
105
106 if (node.callee.type === "MemberExpression") {
107 const receiverLVal = path.scope.generateUidIdentifierBasedOnNode(node.callee.object);
108 scope.push({
109 id: receiverLVal
110 });
111 sequenceParts.push(_core().types.assignmentExpression("=", _core().types.cloneNode(receiverLVal), node.callee.object), _core().types.assignmentExpression("=", _core().types.cloneNode(functionLVal), _core().types.memberExpression(receiverLVal, node.callee.property, false, false)), ...argsInitializers, _core().types.functionExpression(node.callee.property, placeholdersParams, _core().types.blockStatement([_core().types.returnStatement(_core().types.callExpression(_core().types.memberExpression(functionLVal, _core().types.identifier("call"), false, false), [receiverLVal, ...args]))], []), false, false));
112 } else {
113 sequenceParts.push(_core().types.assignmentExpression("=", _core().types.cloneNode(functionLVal), node.callee), ...argsInitializers, _core().types.functionExpression(node.callee, placeholdersParams, _core().types.blockStatement([_core().types.returnStatement(_core().types.callExpression(functionLVal, args))], []), false, false));
114 }
115
116 path.replaceWith(_core().types.sequenceExpression(sequenceParts));
117 }
118
119 }
120 };
121});
122
123exports.default = _default;
\No newline at end of file