source: trip-planner-front/node_modules/@babel/helper-wrap-function/lib/index.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = wrapFunction;
7
8var _helperFunctionName = require("@babel/helper-function-name");
9
10var _template = require("@babel/template");
11
12var _t = require("@babel/types");
13
14const {
15 blockStatement,
16 callExpression,
17 functionExpression,
18 isAssignmentPattern,
19 isRestElement,
20 returnStatement
21} = _t;
22
23const buildAnonymousExpressionWrapper = _template.default.expression(`
24 (function () {
25 var REF = FUNCTION;
26 return function NAME(PARAMS) {
27 return REF.apply(this, arguments);
28 };
29 })()
30`);
31
32const buildNamedExpressionWrapper = _template.default.expression(`
33 (function () {
34 var REF = FUNCTION;
35 function NAME(PARAMS) {
36 return REF.apply(this, arguments);
37 }
38 return NAME;
39 })()
40`);
41
42const buildDeclarationWrapper = (0, _template.default)(`
43 function NAME(PARAMS) { return REF.apply(this, arguments); }
44 function REF() {
45 REF = FUNCTION;
46 return REF.apply(this, arguments);
47 }
48`);
49
50function classOrObjectMethod(path, callId) {
51 const node = path.node;
52 const body = node.body;
53 const container = functionExpression(null, [], blockStatement(body.body), true);
54 body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
55 node.async = false;
56 node.generator = false;
57 path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
58}
59
60function plainFunction(path, callId, noNewArrows) {
61 const node = path.node;
62 const isDeclaration = path.isFunctionDeclaration();
63 const functionId = node.id;
64 const wrapper = isDeclaration ? buildDeclarationWrapper : functionId ? buildNamedExpressionWrapper : buildAnonymousExpressionWrapper;
65
66 if (path.isArrowFunctionExpression()) {
67 path.arrowFunctionToExpression({
68 noNewArrows
69 });
70 }
71
72 node.id = null;
73
74 if (isDeclaration) {
75 node.type = "FunctionExpression";
76 }
77
78 const built = callExpression(callId, [node]);
79 const container = wrapper({
80 NAME: functionId || null,
81 REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
82 FUNCTION: built,
83 PARAMS: node.params.reduce((acc, param) => {
84 acc.done = acc.done || isAssignmentPattern(param) || isRestElement(param);
85
86 if (!acc.done) {
87 acc.params.push(path.scope.generateUidIdentifier("x"));
88 }
89
90 return acc;
91 }, {
92 params: [],
93 done: false
94 }).params
95 });
96
97 if (isDeclaration) {
98 path.replaceWith(container[0]);
99 path.insertAfter(container[1]);
100 } else {
101 const retFunction = container.callee.body.body[1].argument;
102
103 if (!functionId) {
104 (0, _helperFunctionName.default)({
105 node: retFunction,
106 parent: path.parent,
107 scope: path.scope
108 });
109 }
110
111 if (!retFunction || retFunction.id || node.params.length) {
112 path.replaceWith(container);
113 } else {
114 path.replaceWith(built);
115 }
116 }
117}
118
119function wrapFunction(path, callId, noNewArrows = true) {
120 if (path.isMethod()) {
121 classOrObjectMethod(path, callId);
122 } else {
123 plainFunction(path, callId, noNewArrows);
124 }
125}
Note: See TracBrowser for help on using the repository browser.