1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@babel/plugin-transform-parameters/lib/params.js

144 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = convertFunctionParams;
2024-01-16 21:26:16 +08:00
var _core = require("@babel/core");
var _shadowUtils = require("./shadow-utils.js");
const buildDefaultParam = _core.template.statement(`
2023-12-18 13:12:25 +08:00
let VARIABLE_NAME =
arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ?
arguments[ARGUMENT_KEY]
:
DEFAULT_VALUE;
`);
2024-01-16 21:26:16 +08:00
const buildLooseDefaultParam = _core.template.statement(`
2023-12-18 13:12:25 +08:00
if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {
ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;
}
`);
2024-01-16 21:26:16 +08:00
const buildLooseDestructuredDefaultParam = _core.template.statement(`
2023-12-18 13:12:25 +08:00
let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;
`);
2024-01-16 21:26:16 +08:00
const buildSafeArgumentsAccess = _core.template.statement(`
2023-12-18 13:12:25 +08:00
let $0 = arguments.length > $1 ? arguments[$1] : undefined;
`);
2024-01-16 21:26:16 +08:00
function convertFunctionParams(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement) {
const params = path.get("params");
const isSimpleParameterList = params.every(param => param.isIdentifier());
if (isSimpleParameterList) return false;
2023-12-18 13:12:25 +08:00
const {
node,
scope
} = path;
2024-01-16 21:26:16 +08:00
const body = [];
const shadowedParams = new Set();
for (const param of params) {
(0, _shadowUtils.collectShadowedParamsNames)(param, scope, shadowedParams);
}
2023-12-18 13:12:25 +08:00
const state = {
2024-01-16 21:26:16 +08:00
needsOuterBinding: false,
scope
2023-12-18 13:12:25 +08:00
};
2024-01-16 21:26:16 +08:00
if (shadowedParams.size === 0) {
for (const param of params) {
if (!param.isIdentifier()) param.traverse(_shadowUtils.iifeVisitor, state);
if (state.needsOuterBinding) break;
}
}
2023-12-18 13:12:25 +08:00
let firstOptionalIndex = null;
for (let i = 0; i < params.length; i++) {
const param = params[i];
2024-01-16 21:26:16 +08:00
if (shouldTransformParam && !shouldTransformParam(i)) {
continue;
}
const transformedRestNodes = [];
if (replaceRestElement) {
replaceRestElement(path, param, transformedRestNodes);
}
2023-12-18 13:12:25 +08:00
const paramIsAssignmentPattern = param.isAssignmentPattern();
2024-01-16 21:26:16 +08:00
if (paramIsAssignmentPattern && (ignoreFunctionLength || _core.types.isMethod(node, {
kind: "set"
}))) {
2023-12-18 13:12:25 +08:00
const left = param.get("left");
const right = param.get("right");
const undefinedNode = scope.buildUndefinedNode();
if (left.isIdentifier()) {
body.push(buildLooseDefaultParam({
2024-01-16 21:26:16 +08:00
ASSIGNMENT_IDENTIFIER: _core.types.cloneNode(left.node),
2023-12-18 13:12:25 +08:00
DEFAULT_VALUE: right.node,
UNDEFINED: undefinedNode
}));
param.replaceWith(left.node);
} else if (left.isObjectPattern() || left.isArrayPattern()) {
const paramName = scope.generateUidIdentifier();
body.push(buildLooseDestructuredDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
2024-01-16 21:26:16 +08:00
PARAMETER_NAME: _core.types.cloneNode(paramName),
2023-12-18 13:12:25 +08:00
UNDEFINED: undefinedNode
}));
param.replaceWith(paramName);
}
} else if (paramIsAssignmentPattern) {
if (firstOptionalIndex === null) firstOptionalIndex = i;
const left = param.get("left");
const right = param.get("right");
const defNode = buildDefaultParam({
VARIABLE_NAME: left.node,
DEFAULT_VALUE: right.node,
2024-01-16 21:26:16 +08:00
ARGUMENT_KEY: _core.types.numericLiteral(i)
2023-12-18 13:12:25 +08:00
});
body.push(defNode);
} else if (firstOptionalIndex !== null) {
2024-01-16 21:26:16 +08:00
const defNode = buildSafeArgumentsAccess([param.node, _core.types.numericLiteral(i)]);
2023-12-18 13:12:25 +08:00
body.push(defNode);
} else if (param.isObjectPattern() || param.isArrayPattern()) {
const uid = path.scope.generateUidIdentifier("ref");
2024-01-16 21:26:16 +08:00
uid.typeAnnotation = param.node.typeAnnotation;
const defNode = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(param.node, uid)]);
2023-12-18 13:12:25 +08:00
body.push(defNode);
2024-01-16 21:26:16 +08:00
param.replaceWith(_core.types.cloneNode(uid));
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
if (transformedRestNodes) {
for (const transformedNode of transformedRestNodes) {
body.push(transformedNode);
}
2023-12-18 13:12:25 +08:00
}
}
if (firstOptionalIndex !== null) {
node.params = node.params.slice(0, firstOptionalIndex);
}
path.ensureBlock();
2024-01-16 21:26:16 +08:00
const {
async,
generator
} = node;
if (generator || state.needsOuterBinding || shadowedParams.size > 0) {
body.push((0, _shadowUtils.buildScopeIIFE)(shadowedParams, path.node.body));
path.set("body", _core.types.blockStatement(body));
const bodyPath = path.get("body.body");
const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee");
arrowPath.arrowFunctionToExpression();
arrowPath.node.generator = generator;
arrowPath.node.async = async;
node.generator = false;
node.async = false;
if (async) {
path.node.body = _core.template.statement.ast`{
try {
${path.node.body.body}
} catch (e) {
return Promise.reject(e);
}
}`;
}
2023-12-18 13:12:25 +08:00
} else {
path.get("body").unshiftContainer("body", body);
}
return true;
2024-01-16 21:26:16 +08:00
}
//# sourceMappingURL=params.js.map