1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@babel/traverse/lib/path/conversion.js

472 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toComputedKey = toComputedKey;
exports.ensureBlock = ensureBlock;
exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
exports.arrowFunctionToExpression = arrowFunctionToExpression;
function t() {
2024-01-16 21:26:16 +08:00
var data = _interopRequireWildcard(require("@babel/types"));
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
t = function t() {
2023-12-18 13:12:25 +08:00
return data;
};
return data;
}
function _helperFunctionName() {
2024-01-16 21:26:16 +08:00
var data = _interopRequireDefault(require("@babel/helper-function-name"));
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
_helperFunctionName = function _helperFunctionName() {
2023-12-18 13:12:25 +08:00
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _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; } }
function toComputedKey() {
2024-01-16 21:26:16 +08:00
var node = this.node;
var key;
2023-12-18 13:12:25 +08:00
if (this.isMemberExpression()) {
key = node.property;
} else if (this.isProperty() || this.isMethod()) {
key = node.key;
} else {
throw new ReferenceError("todo");
}
if (!node.computed) {
if (t().isIdentifier(key)) key = t().stringLiteral(key.name);
}
return key;
}
function ensureBlock() {
2024-01-16 21:26:16 +08:00
var body = this.get("body");
var bodyNode = body.node;
2023-12-18 13:12:25 +08:00
if (Array.isArray(body)) {
throw new Error("Can't convert array path to a block statement");
}
if (!bodyNode) {
throw new Error("Can't convert node without a body");
}
if (body.isBlockStatement()) {
return bodyNode;
}
2024-01-16 21:26:16 +08:00
var statements = [];
var stringPath = "body";
var key;
var listKey;
2023-12-18 13:12:25 +08:00
if (body.isStatement()) {
listKey = "body";
key = 0;
statements.push(body.node);
} else {
stringPath += ".body.0";
if (this.isFunction()) {
key = "argument";
statements.push(t().returnStatement(body.node));
} else {
key = "expression";
statements.push(t().expressionStatement(body.node));
}
}
this.node.body = t().blockStatement(statements);
2024-01-16 21:26:16 +08:00
var parentPath = this.get(stringPath);
2023-12-18 13:12:25 +08:00
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
return this.node;
}
function arrowFunctionToShadowed() {
if (!this.isArrowFunctionExpression()) return;
this.arrowFunctionToExpression();
}
function unwrapFunctionEnvironment() {
if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
}
hoistFunctionEnvironment(this);
}
2024-01-16 21:26:16 +08:00
function arrowFunctionToExpression(_temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$allowInsertArrow = _ref.allowInsertArrow,
allowInsertArrow = _ref$allowInsertArrow === void 0 ? true : _ref$allowInsertArrow,
_ref$specCompliant = _ref.specCompliant,
specCompliant = _ref$specCompliant === void 0 ? false : _ref$specCompliant;
2023-12-18 13:12:25 +08:00
if (!this.isArrowFunctionExpression()) {
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
}
2024-01-16 21:26:16 +08:00
var thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow);
2023-12-18 13:12:25 +08:00
this.ensureBlock();
this.node.type = "FunctionExpression";
if (specCompliant) {
2024-01-16 21:26:16 +08:00
var checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
2023-12-18 13:12:25 +08:00
if (checkBinding) {
this.parentPath.scope.push({
id: checkBinding,
init: t().objectExpression([])
});
}
2024-01-16 21:26:16 +08:00
this.get("body").unshiftContainer("body", t().expressionStatement(t().callExpression(this.hub.file.addHelper("newArrowCheck"), [t().thisExpression(), checkBinding ? t().identifier(checkBinding.name) : t().identifier(thisBinding)])));
2023-12-18 13:12:25 +08:00
this.replaceWith(t().callExpression(t().memberExpression((0, _helperFunctionName().default)(this, true) || this.node, t().identifier("bind")), [checkBinding ? t().identifier(checkBinding.name) : t().thisExpression()]));
}
}
2024-01-16 21:26:16 +08:00
function hoistFunctionEnvironment(fnPath, specCompliant, allowInsertArrow) {
if (specCompliant === void 0) {
specCompliant = false;
}
if (allowInsertArrow === void 0) {
allowInsertArrow = true;
}
var thisEnvFn = fnPath.findParent(function (p) {
2023-12-18 13:12:25 +08:00
return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
static: false
});
});
2024-01-16 21:26:16 +08:00
var inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor";
2023-12-18 13:12:25 +08:00
if (thisEnvFn.isClassProperty()) {
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
}
2024-01-16 21:26:16 +08:00
var _getScopeInformation = getScopeInformation(fnPath),
thisPaths = _getScopeInformation.thisPaths,
argumentsPaths = _getScopeInformation.argumentsPaths,
newTargetPaths = _getScopeInformation.newTargetPaths,
superProps = _getScopeInformation.superProps,
superCalls = _getScopeInformation.superCalls;
2023-12-18 13:12:25 +08:00
if (inConstructor && superCalls.length > 0) {
if (!allowInsertArrow) {
throw superCalls[0].buildCodeFrameError("Unable to handle nested super() usage in arrow");
}
2024-01-16 21:26:16 +08:00
var allSuperCalls = [];
2023-12-18 13:12:25 +08:00
thisEnvFn.traverse({
2024-01-16 21:26:16 +08:00
Function: function Function(child) {
2023-12-18 13:12:25 +08:00
if (child.isArrowFunctionExpression()) return;
child.skip();
},
2024-01-16 21:26:16 +08:00
ClassProperty: function ClassProperty(child) {
if (child.node.static) return;
2023-12-18 13:12:25 +08:00
child.skip();
},
2024-01-16 21:26:16 +08:00
CallExpression: function CallExpression(child) {
2023-12-18 13:12:25 +08:00
if (!child.get("callee").isSuper()) return;
allSuperCalls.push(child);
}
});
2024-01-16 21:26:16 +08:00
var superBinding = getSuperBinding(thisEnvFn);
allSuperCalls.forEach(function (superCall) {
var callee = t().identifier(superBinding);
2023-12-18 13:12:25 +08:00
callee.loc = superCall.node.callee.loc;
superCall.get("callee").replaceWith(callee);
});
}
2024-01-16 21:26:16 +08:00
var thisBinding;
2023-12-18 13:12:25 +08:00
if (thisPaths.length > 0 || specCompliant) {
thisBinding = getThisBinding(thisEnvFn, inConstructor);
if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) {
2024-01-16 21:26:16 +08:00
thisPaths.forEach(function (thisChild) {
var thisRef = thisChild.isJSX() ? t().jsxIdentifier(thisBinding) : t().identifier(thisBinding);
2023-12-18 13:12:25 +08:00
thisRef.loc = thisChild.node.loc;
thisChild.replaceWith(thisRef);
});
if (specCompliant) thisBinding = null;
}
}
if (argumentsPaths.length > 0) {
2024-01-16 21:26:16 +08:00
var argumentsBinding = getBinding(thisEnvFn, "arguments", function () {
return t().identifier("arguments");
});
argumentsPaths.forEach(function (argumentsChild) {
var argsRef = t().identifier(argumentsBinding);
2023-12-18 13:12:25 +08:00
argsRef.loc = argumentsChild.node.loc;
argumentsChild.replaceWith(argsRef);
});
}
if (newTargetPaths.length > 0) {
2024-01-16 21:26:16 +08:00
var newTargetBinding = getBinding(thisEnvFn, "newtarget", function () {
return t().metaProperty(t().identifier("new"), t().identifier("target"));
});
newTargetPaths.forEach(function (targetChild) {
var targetRef = t().identifier(newTargetBinding);
2023-12-18 13:12:25 +08:00
targetRef.loc = targetChild.node.loc;
targetChild.replaceWith(targetRef);
});
}
if (superProps.length > 0) {
if (!allowInsertArrow) {
throw superProps[0].buildCodeFrameError("Unable to handle nested super.prop usage");
}
2024-01-16 21:26:16 +08:00
var flatSuperProps = superProps.reduce(function (acc, superProp) {
return acc.concat(standardizeSuperProperty(superProp));
}, []);
flatSuperProps.forEach(function (superProp) {
var key = superProp.node.computed ? "" : superProp.get("property").node.name;
2023-12-18 13:12:25 +08:00
if (superProp.parentPath.isCallExpression({
callee: superProp.node
})) {
2024-01-16 21:26:16 +08:00
var _superBinding = getSuperPropCallBinding(thisEnvFn, key);
2023-12-18 13:12:25 +08:00
if (superProp.node.computed) {
2024-01-16 21:26:16 +08:00
var prop = superProp.get("property").node;
superProp.replaceWith(t().identifier(_superBinding));
2023-12-18 13:12:25 +08:00
superProp.parentPath.node.arguments.unshift(prop);
} else {
2024-01-16 21:26:16 +08:00
superProp.replaceWith(t().identifier(_superBinding));
2023-12-18 13:12:25 +08:00
}
} else {
2024-01-16 21:26:16 +08:00
var isAssignment = superProp.parentPath.isAssignmentExpression({
2023-12-18 13:12:25 +08:00
left: superProp.node
});
2024-01-16 21:26:16 +08:00
var _superBinding2 = getSuperPropBinding(thisEnvFn, isAssignment, key);
var args = [];
2023-12-18 13:12:25 +08:00
if (superProp.node.computed) {
args.push(superProp.get("property").node);
}
if (isAssignment) {
2024-01-16 21:26:16 +08:00
var value = superProp.parentPath.node.right;
2023-12-18 13:12:25 +08:00
args.push(value);
2024-01-16 21:26:16 +08:00
superProp.parentPath.replaceWith(t().callExpression(t().identifier(_superBinding2), args));
2023-12-18 13:12:25 +08:00
} else {
2024-01-16 21:26:16 +08:00
superProp.replaceWith(t().callExpression(t().identifier(_superBinding2), args));
2023-12-18 13:12:25 +08:00
}
}
});
}
return thisBinding;
}
function standardizeSuperProperty(superProp) {
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
2024-01-16 21:26:16 +08:00
var assignmentPath = superProp.parentPath;
var op = assignmentPath.node.operator.slice(0, -1);
var value = assignmentPath.node.right;
2023-12-18 13:12:25 +08:00
assignmentPath.node.operator = "=";
if (superProp.node.computed) {
2024-01-16 21:26:16 +08:00
var tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
2023-12-18 13:12:25 +08:00
assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, t().assignmentExpression("=", tmp, superProp.node.property), true));
assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(tmp.name), true), value));
} else {
assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, superProp.node.property));
assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(superProp.node.property.name)), value));
}
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
} else if (superProp.parentPath.isUpdateExpression()) {
2024-01-16 21:26:16 +08:00
var updateExpr = superProp.parentPath;
var _tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
var computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
var parts = [t().assignmentExpression("=", _tmp, t().memberExpression(superProp.node.object, computedKey ? t().assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t().assignmentExpression("=", t().memberExpression(superProp.node.object, computedKey ? t().identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t().binaryExpression("+", t().identifier(_tmp.name), t().numericLiteral(1)))];
2023-12-18 13:12:25 +08:00
if (!superProp.parentPath.node.prefix) {
2024-01-16 21:26:16 +08:00
parts.push(t().identifier(_tmp.name));
2023-12-18 13:12:25 +08:00
}
updateExpr.replaceWith(t().sequenceExpression(parts));
2024-01-16 21:26:16 +08:00
var left = updateExpr.get("expressions.0.right");
var right = updateExpr.get("expressions.1.left");
2023-12-18 13:12:25 +08:00
return [left, right];
}
return [superProp];
}
function hasSuperClass(thisEnvFn) {
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
}
function getThisBinding(thisEnvFn, inConstructor) {
2024-01-16 21:26:16 +08:00
return getBinding(thisEnvFn, "this", function (thisBinding) {
2023-12-18 13:12:25 +08:00
if (!inConstructor || !hasSuperClass(thisEnvFn)) return t().thisExpression();
2024-01-16 21:26:16 +08:00
var supers = new WeakSet();
2023-12-18 13:12:25 +08:00
thisEnvFn.traverse({
2024-01-16 21:26:16 +08:00
Function: function Function(child) {
2023-12-18 13:12:25 +08:00
if (child.isArrowFunctionExpression()) return;
child.skip();
},
2024-01-16 21:26:16 +08:00
ClassProperty: function ClassProperty(child) {
if (child.node.static) return;
2023-12-18 13:12:25 +08:00
child.skip();
},
2024-01-16 21:26:16 +08:00
CallExpression: function CallExpression(child) {
2023-12-18 13:12:25 +08:00
if (!child.get("callee").isSuper()) return;
if (supers.has(child.node)) return;
supers.add(child.node);
2024-01-16 21:26:16 +08:00
child.replaceWith(t().assignmentExpression("=", t().identifier(thisBinding), child.node));
2023-12-18 13:12:25 +08:00
}
});
});
}
function getSuperBinding(thisEnvFn) {
2024-01-16 21:26:16 +08:00
return getBinding(thisEnvFn, "supercall", function () {
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
2023-12-18 13:12:25 +08:00
return t().arrowFunctionExpression([t().restElement(argsBinding)], t().callExpression(t().super(), [t().spreadElement(t().identifier(argsBinding.name))]));
});
}
function getSuperPropCallBinding(thisEnvFn, propName) {
2024-01-16 21:26:16 +08:00
return getBinding(thisEnvFn, "superprop_call:" + (propName || ""), function () {
var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
var argsList = [t().restElement(argsBinding)];
var fnBody;
2023-12-18 13:12:25 +08:00
if (propName) {
fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(propName)), [t().spreadElement(t().identifier(argsBinding.name))]);
} else {
2024-01-16 21:26:16 +08:00
var method = thisEnvFn.scope.generateUidIdentifier("prop");
2023-12-18 13:12:25 +08:00
argsList.unshift(method);
fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(method.name), true), [t().spreadElement(t().identifier(argsBinding.name))]);
}
return t().arrowFunctionExpression(argsList, fnBody);
});
}
function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
2024-01-16 21:26:16 +08:00
var op = isAssignment ? "set" : "get";
return getBinding(thisEnvFn, "superprop_" + op + ":" + (propName || ""), function () {
var argsList = [];
var fnBody;
2023-12-18 13:12:25 +08:00
if (propName) {
fnBody = t().memberExpression(t().super(), t().identifier(propName));
} else {
2024-01-16 21:26:16 +08:00
var method = thisEnvFn.scope.generateUidIdentifier("prop");
2023-12-18 13:12:25 +08:00
argsList.unshift(method);
fnBody = t().memberExpression(t().super(), t().identifier(method.name), true);
}
if (isAssignment) {
2024-01-16 21:26:16 +08:00
var valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
2023-12-18 13:12:25 +08:00
argsList.push(valueIdent);
fnBody = t().assignmentExpression("=", fnBody, t().identifier(valueIdent.name));
}
return t().arrowFunctionExpression(argsList, fnBody);
});
}
function getBinding(thisEnvFn, key, init) {
2024-01-16 21:26:16 +08:00
var cacheKey = "binding:" + key;
var data = thisEnvFn.getData(cacheKey);
2023-12-18 13:12:25 +08:00
if (!data) {
2024-01-16 21:26:16 +08:00
var id = thisEnvFn.scope.generateUidIdentifier(key);
2023-12-18 13:12:25 +08:00
data = id.name;
thisEnvFn.setData(cacheKey, data);
thisEnvFn.scope.push({
id: id,
init: init(data)
});
}
return data;
}
function getScopeInformation(fnPath) {
2024-01-16 21:26:16 +08:00
var thisPaths = [];
var argumentsPaths = [];
var newTargetPaths = [];
var superProps = [];
var superCalls = [];
2023-12-18 13:12:25 +08:00
fnPath.traverse({
2024-01-16 21:26:16 +08:00
ClassProperty: function ClassProperty(child) {
if (child.node.static) return;
2023-12-18 13:12:25 +08:00
child.skip();
},
2024-01-16 21:26:16 +08:00
Function: function Function(child) {
2023-12-18 13:12:25 +08:00
if (child.isArrowFunctionExpression()) return;
child.skip();
},
2024-01-16 21:26:16 +08:00
ThisExpression: function ThisExpression(child) {
2023-12-18 13:12:25 +08:00
thisPaths.push(child);
},
2024-01-16 21:26:16 +08:00
JSXIdentifier: function JSXIdentifier(child) {
2023-12-18 13:12:25 +08:00
if (child.node.name !== "this") return;
if (!child.parentPath.isJSXMemberExpression({
object: child.node
}) && !child.parentPath.isJSXOpeningElement({
name: child.node
})) {
return;
}
thisPaths.push(child);
},
2024-01-16 21:26:16 +08:00
CallExpression: function CallExpression(child) {
2023-12-18 13:12:25 +08:00
if (child.get("callee").isSuper()) superCalls.push(child);
},
2024-01-16 21:26:16 +08:00
MemberExpression: function MemberExpression(child) {
2023-12-18 13:12:25 +08:00
if (child.get("object").isSuper()) superProps.push(child);
},
2024-01-16 21:26:16 +08:00
ReferencedIdentifier: function ReferencedIdentifier(child) {
2023-12-18 13:12:25 +08:00
if (child.node.name !== "arguments") return;
argumentsPaths.push(child);
},
2024-01-16 21:26:16 +08:00
MetaProperty: function MetaProperty(child) {
2023-12-18 13:12:25 +08:00
if (!child.get("meta").isIdentifier({
name: "new"
})) return;
if (!child.get("property").isIdentifier({
name: "target"
})) return;
newTargetPaths.push(child);
}
});
return {
2024-01-16 21:26:16 +08:00
thisPaths: thisPaths,
argumentsPaths: argumentsPaths,
newTargetPaths: newTargetPaths,
superProps: superProps,
superCalls: superCalls
2023-12-18 13:12:25 +08:00
};
}