1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = convertFunctionParams;
|
---|
7 | var _core = require("@babel/core");
|
---|
8 | var _shadowUtils = require("./shadow-utils.js");
|
---|
9 | const buildDefaultParam = _core.template.statement(`
|
---|
10 | let VARIABLE_NAME =
|
---|
11 | arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ?
|
---|
12 | arguments[ARGUMENT_KEY]
|
---|
13 | :
|
---|
14 | DEFAULT_VALUE;
|
---|
15 | `);
|
---|
16 | const buildLooseDefaultParam = _core.template.statement(`
|
---|
17 | if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {
|
---|
18 | ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;
|
---|
19 | }
|
---|
20 | `);
|
---|
21 | const buildLooseDestructuredDefaultParam = _core.template.statement(`
|
---|
22 | let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;
|
---|
23 | `);
|
---|
24 | const buildSafeArgumentsAccess = _core.template.statement(`
|
---|
25 | let $0 = arguments.length > $1 ? arguments[$1] : undefined;
|
---|
26 | `);
|
---|
27 | function convertFunctionParams(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement) {
|
---|
28 | const params = path.get("params");
|
---|
29 | const isSimpleParameterList = params.every(param => param.isIdentifier());
|
---|
30 | if (isSimpleParameterList) return false;
|
---|
31 | const {
|
---|
32 | node,
|
---|
33 | scope
|
---|
34 | } = path;
|
---|
35 | const body = [];
|
---|
36 | const shadowedParams = new Set();
|
---|
37 | for (const param of params) {
|
---|
38 | (0, _shadowUtils.collectShadowedParamsNames)(param, scope, shadowedParams);
|
---|
39 | }
|
---|
40 | const state = {
|
---|
41 | needsOuterBinding: false,
|
---|
42 | scope
|
---|
43 | };
|
---|
44 | if (shadowedParams.size === 0) {
|
---|
45 | for (const param of params) {
|
---|
46 | if (!param.isIdentifier()) param.traverse(_shadowUtils.iifeVisitor, state);
|
---|
47 | if (state.needsOuterBinding) break;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | let firstOptionalIndex = null;
|
---|
51 | for (let i = 0; i < params.length; i++) {
|
---|
52 | const param = params[i];
|
---|
53 | if (shouldTransformParam && !shouldTransformParam(i)) {
|
---|
54 | continue;
|
---|
55 | }
|
---|
56 | const transformedRestNodes = [];
|
---|
57 | if (replaceRestElement) {
|
---|
58 | replaceRestElement(path, param, transformedRestNodes);
|
---|
59 | }
|
---|
60 | const paramIsAssignmentPattern = param.isAssignmentPattern();
|
---|
61 | if (paramIsAssignmentPattern && (ignoreFunctionLength || _core.types.isMethod(node, {
|
---|
62 | kind: "set"
|
---|
63 | }))) {
|
---|
64 | const left = param.get("left");
|
---|
65 | const right = param.get("right");
|
---|
66 | const undefinedNode = scope.buildUndefinedNode();
|
---|
67 | if (left.isIdentifier()) {
|
---|
68 | body.push(buildLooseDefaultParam({
|
---|
69 | ASSIGNMENT_IDENTIFIER: _core.types.cloneNode(left.node),
|
---|
70 | DEFAULT_VALUE: right.node,
|
---|
71 | UNDEFINED: undefinedNode
|
---|
72 | }));
|
---|
73 | param.replaceWith(left.node);
|
---|
74 | } else if (left.isObjectPattern() || left.isArrayPattern()) {
|
---|
75 | const paramName = scope.generateUidIdentifier();
|
---|
76 | body.push(buildLooseDestructuredDefaultParam({
|
---|
77 | ASSIGNMENT_IDENTIFIER: left.node,
|
---|
78 | DEFAULT_VALUE: right.node,
|
---|
79 | PARAMETER_NAME: _core.types.cloneNode(paramName),
|
---|
80 | UNDEFINED: undefinedNode
|
---|
81 | }));
|
---|
82 | param.replaceWith(paramName);
|
---|
83 | }
|
---|
84 | } else if (paramIsAssignmentPattern) {
|
---|
85 | if (firstOptionalIndex === null) firstOptionalIndex = i;
|
---|
86 | const left = param.get("left");
|
---|
87 | const right = param.get("right");
|
---|
88 | const defNode = buildDefaultParam({
|
---|
89 | VARIABLE_NAME: left.node,
|
---|
90 | DEFAULT_VALUE: right.node,
|
---|
91 | ARGUMENT_KEY: _core.types.numericLiteral(i)
|
---|
92 | });
|
---|
93 | body.push(defNode);
|
---|
94 | } else if (firstOptionalIndex !== null) {
|
---|
95 | const defNode = buildSafeArgumentsAccess([param.node, _core.types.numericLiteral(i)]);
|
---|
96 | body.push(defNode);
|
---|
97 | } else if (param.isObjectPattern() || param.isArrayPattern()) {
|
---|
98 | const uid = path.scope.generateUidIdentifier("ref");
|
---|
99 | uid.typeAnnotation = param.node.typeAnnotation;
|
---|
100 | const defNode = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(param.node, uid)]);
|
---|
101 | body.push(defNode);
|
---|
102 | param.replaceWith(_core.types.cloneNode(uid));
|
---|
103 | }
|
---|
104 | if (transformedRestNodes) {
|
---|
105 | for (const transformedNode of transformedRestNodes) {
|
---|
106 | body.push(transformedNode);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if (firstOptionalIndex !== null) {
|
---|
111 | node.params = node.params.slice(0, firstOptionalIndex);
|
---|
112 | }
|
---|
113 | path.ensureBlock();
|
---|
114 | const path2 = path;
|
---|
115 | const {
|
---|
116 | async,
|
---|
117 | generator
|
---|
118 | } = node;
|
---|
119 | if (generator || state.needsOuterBinding || shadowedParams.size > 0) {
|
---|
120 | body.push((0, _shadowUtils.buildScopeIIFE)(shadowedParams, path2.node.body));
|
---|
121 | path.set("body", _core.types.blockStatement(body));
|
---|
122 | const bodyPath = path2.get("body.body");
|
---|
123 | const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee");
|
---|
124 | arrowPath.arrowFunctionToExpression();
|
---|
125 | arrowPath.node.generator = generator;
|
---|
126 | arrowPath.node.async = async;
|
---|
127 | node.generator = false;
|
---|
128 | node.async = false;
|
---|
129 | if (async) {
|
---|
130 | path2.node.body = _core.template.statement.ast`{
|
---|
131 | try {
|
---|
132 | ${path2.node.body.body}
|
---|
133 | } catch (e) {
|
---|
134 | return Promise.reject(e);
|
---|
135 | }
|
---|
136 | }`;
|
---|
137 | }
|
---|
138 | } else {
|
---|
139 | path2.get("body").unshiftContainer("body", body);
|
---|
140 | }
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 |
|
---|
144 | //# sourceMappingURL=params.js.map
|
---|