summaryrefslogtreecommitdiff
path: root/node_modules/@babel/traverse/lib/path/conversion.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
commitefbc2ffe6a1ee5ec38a4396305561d6515d0943e (patch)
tree2b4c94f18f2c792dfda813050755d5c0273fae22 /node_modules/@babel/traverse/lib/path/conversion.js
parent43acceb75957c880a5ba5a31aae221394a36d5f6 (diff)
node fixes, updates
Diffstat (limited to 'node_modules/@babel/traverse/lib/path/conversion.js')
-rw-r--r--node_modules/@babel/traverse/lib/path/conversion.js165
1 files changed, 153 insertions, 12 deletions
diff --git a/node_modules/@babel/traverse/lib/path/conversion.js b/node_modules/@babel/traverse/lib/path/conversion.js
index a7884c7c..6fac4522 100644
--- a/node_modules/@babel/traverse/lib/path/conversion.js
+++ b/node_modules/@babel/traverse/lib/path/conversion.js
@@ -5,12 +5,14 @@ Object.defineProperty(exports, "__esModule", {
});
exports.arrowFunctionToExpression = arrowFunctionToExpression;
exports.ensureBlock = ensureBlock;
+exports.ensureFunctionName = ensureFunctionName;
+exports.splitExportDeclaration = splitExportDeclaration;
exports.toComputedKey = toComputedKey;
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
var _t = require("@babel/types");
-var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
-var _helperFunctionName = require("@babel/helper-function-name");
+var _template = require("@babel/template");
var _visitors = require("../visitors.js");
+var _context = require("./context.js");
const {
arrowFunctionExpression,
assignmentExpression,
@@ -36,7 +38,18 @@ const {
super: _super,
thisExpression,
toExpression,
- unaryExpression
+ unaryExpression,
+ toBindingIdentifierName,
+ isFunction,
+ isAssignmentPattern,
+ isRestElement,
+ getFunctionName,
+ cloneNode,
+ variableDeclaration,
+ variableDeclarator,
+ exportNamedDeclaration,
+ exportSpecifier,
+ inherits
} = _t;
function toComputedKey() {
let key;
@@ -84,7 +97,7 @@ function ensureBlock() {
}
this.node.body = blockStatement(statements);
const parentPath = this.get(stringPath);
- body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
+ _context.setup.call(body, parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
return this.node;
}
{
@@ -110,10 +123,15 @@ function arrowFunctionToExpression({
if (!this.isArrowFunctionExpression()) {
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
}
+ let self = this;
+ if (!noNewArrows) {
+ var _self$ensureFunctionN;
+ self = (_self$ensureFunctionN = self.ensureFunctionName(false)) != null ? _self$ensureFunctionN : self;
+ }
const {
thisBinding,
fnPath: fn
- } = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow, allowInsertArrowWithRest);
+ } = hoistFunctionEnvironment(self, noNewArrows, allowInsertArrow, allowInsertArrowWithRest);
fn.ensureBlock();
setType(fn, "FunctionExpression");
if (!noNewArrows) {
@@ -125,19 +143,19 @@ function arrowFunctionToExpression({
});
}
fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
- fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
+ fn.replaceWith(callExpression(memberExpression(fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
return fn.get("callee.object");
}
return fn;
}
-const getSuperCallsVisitor = (0, _visitors.merge)([{
+const getSuperCallsVisitor = (0, _visitors.environmentVisitor)({
CallExpression(child, {
allSuperCalls
}) {
if (!child.get("callee").isSuper()) return;
allSuperCalls.push(child);
}
-}, _helperEnvironmentVisitor.default]);
+});
function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true, allowInsertArrowWithRest = true) {
let arrowParent;
let thisEnvFn = fnPath.findParent(p => {
@@ -324,7 +342,7 @@ function standardizeSuperProperty(superProp) {
function hasSuperClass(thisEnvFn) {
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
}
-const assignSuperThisVisitor = (0, _visitors.merge)([{
+const assignSuperThisVisitor = (0, _visitors.environmentVisitor)({
CallExpression(child, {
supers,
thisBinding
@@ -334,7 +352,7 @@ const assignSuperThisVisitor = (0, _visitors.merge)([{
supers.add(child.node);
child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
}
-}, _helperEnvironmentVisitor.default]);
+});
function getThisBinding(thisEnvFn, inConstructor) {
return getBinding(thisEnvFn, "this", thisBinding => {
if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
@@ -384,7 +402,7 @@ function getBinding(thisEnvFn, key, init) {
}
return data;
}
-const getScopeInformationVisitor = (0, _visitors.merge)([{
+const getScopeInformationVisitor = (0, _visitors.environmentVisitor)({
ThisExpression(child, {
thisPaths
}) {
@@ -442,7 +460,7 @@ const getScopeInformationVisitor = (0, _visitors.merge)([{
})) return;
newTargetPaths.push(child);
}
-}, _helperEnvironmentVisitor.default]);
+});
function getScopeInformation(fnPath) {
const thisPaths = [];
const argumentsPaths = [];
@@ -464,5 +482,128 @@ function getScopeInformation(fnPath) {
superCalls
};
}
+function splitExportDeclaration() {
+ if (!this.isExportDeclaration() || this.isExportAllDeclaration()) {
+ throw new Error("Only default and named export declarations can be split.");
+ }
+ if (this.isExportNamedDeclaration() && this.get("specifiers").length > 0) {
+ throw new Error("It doesn't make sense to split exported specifiers.");
+ }
+ const declaration = this.get("declaration");
+ if (this.isExportDefaultDeclaration()) {
+ const standaloneDeclaration = declaration.isFunctionDeclaration() || declaration.isClassDeclaration();
+ const exportExpr = declaration.isFunctionExpression() || declaration.isClassExpression();
+ const scope = declaration.isScope() ? declaration.scope.parent : declaration.scope;
+ let id = declaration.node.id;
+ let needBindingRegistration = false;
+ if (!id) {
+ needBindingRegistration = true;
+ id = scope.generateUidIdentifier("default");
+ if (standaloneDeclaration || exportExpr) {
+ declaration.node.id = cloneNode(id);
+ }
+ } else if (exportExpr && scope.hasBinding(id.name)) {
+ needBindingRegistration = true;
+ id = scope.generateUidIdentifier(id.name);
+ }
+ const updatedDeclaration = standaloneDeclaration ? declaration.node : variableDeclaration("var", [variableDeclarator(cloneNode(id), declaration.node)]);
+ const updatedExportDeclaration = exportNamedDeclaration(null, [exportSpecifier(cloneNode(id), identifier("default"))]);
+ this.insertAfter(updatedExportDeclaration);
+ this.replaceWith(updatedDeclaration);
+ if (needBindingRegistration) {
+ scope.registerDeclaration(this);
+ }
+ return this;
+ } else if (this.get("specifiers").length > 0) {
+ throw new Error("It doesn't make sense to split exported specifiers.");
+ }
+ const bindingIdentifiers = declaration.getOuterBindingIdentifiers();
+ const specifiers = Object.keys(bindingIdentifiers).map(name => {
+ return exportSpecifier(identifier(name), identifier(name));
+ });
+ const aliasDeclar = exportNamedDeclaration(null, specifiers);
+ this.insertAfter(aliasDeclar);
+ this.replaceWith(declaration.node);
+ return this;
+}
+const refersOuterBindingVisitor = {
+ "ReferencedIdentifier|BindingIdentifier"(path, state) {
+ if (path.node.name !== state.name) return;
+ state.needsRename = true;
+ path.stop();
+ },
+ Scope(path, state) {
+ if (path.scope.hasOwnBinding(state.name)) {
+ path.skip();
+ }
+ }
+};
+function ensureFunctionName(supportUnicodeId) {
+ if (this.node.id) return this;
+ const res = getFunctionName(this.node, this.parent);
+ if (res == null) return this;
+ let {
+ name
+ } = res;
+ if (!supportUnicodeId && /[\uD800-\uDFFF]/.test(name)) {
+ return null;
+ }
+ if (name.startsWith("get ") || name.startsWith("set ")) {
+ return null;
+ }
+ name = toBindingIdentifierName(name.replace(/[/ ]/g, "_"));
+ const id = identifier(name);
+ inherits(id, res.originalNode);
+ const state = {
+ needsRename: false,
+ name
+ };
+ const {
+ scope
+ } = this;
+ const binding = scope.getOwnBinding(name);
+ if (binding) {
+ if (binding.kind === "param") {
+ state.needsRename = true;
+ } else {}
+ } else if (scope.parent.hasBinding(name) || scope.hasGlobal(name)) {
+ this.traverse(refersOuterBindingVisitor, state);
+ }
+ if (!state.needsRename) {
+ this.node.id = id;
+ scope.getProgramParent().references[id.name] = true;
+ return this;
+ }
+ if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
+ scope.rename(id.name);
+ this.node.id = id;
+ scope.getProgramParent().references[id.name] = true;
+ return this;
+ }
+ if (!isFunction(this.node)) return null;
+ const key = scope.generateUidIdentifier(id.name);
+ const params = [];
+ for (let i = 0, len = getFunctionArity(this.node); i < len; i++) {
+ params.push(scope.generateUidIdentifier("x"));
+ }
+ const call = _template.default.expression.ast`
+ (function (${key}) {
+ function ${id}(${params}) {
+ return ${cloneNode(key)}.apply(this, arguments);
+ }
+
+ ${cloneNode(id)}.toString = function () {
+ return ${cloneNode(key)}.toString();
+ }
+
+ return ${cloneNode(id)};
+ })(${toExpression(this.node)})
+ `;
+ return this.replaceWith(call)[0].get("arguments.0");
+}
+function getFunctionArity(node) {
+ const count = node.params.findIndex(param => isAssignmentPattern(param) || isRestElement(param));
+ return count === -1 ? node.params.length : count;
+}
//# sourceMappingURL=conversion.js.map