summaryrefslogtreecommitdiff
path: root/node_modules/@babel/plugin-transform-classes/lib/transformClass.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/plugin-transform-classes/lib/transformClass.js
parent43acceb75957c880a5ba5a31aae221394a36d5f6 (diff)
node fixes, updates
Diffstat (limited to 'node_modules/@babel/plugin-transform-classes/lib/transformClass.js')
-rw-r--r--node_modules/@babel/plugin-transform-classes/lib/transformClass.js74
1 files changed, 36 insertions, 38 deletions
diff --git a/node_modules/@babel/plugin-transform-classes/lib/transformClass.js b/node_modules/@babel/plugin-transform-classes/lib/transformClass.js
index 57811677..4a2b9fe6 100644
--- a/node_modules/@babel/plugin-transform-classes/lib/transformClass.js
+++ b/node_modules/@babel/plugin-transform-classes/lib/transformClass.js
@@ -4,10 +4,9 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = transformClass;
-var _helperFunctionName = require("@babel/helper-function-name");
var _helperReplaceSupers = require("@babel/helper-replace-supers");
-var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
var _core = require("@babel/core");
+var _traverse = require("@babel/traverse");
var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
var _inlineCallSuperHelpers = require("./inline-callSuper-helpers.js");
function buildConstructor(classRef, constructorBody, node) {
@@ -56,11 +55,11 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
const setState = newState => {
Object.assign(classState, newState);
};
- const findThisesVisitor = _core.traverse.visitors.merge([_helperEnvironmentVisitor.default, {
+ const findThisesVisitor = _traverse.visitors.environmentVisitor({
ThisExpression(path) {
classState.superThises.push(path);
}
- }]);
+ });
function createClassHelper(args) {
return _core.types.callExpression(classState.file.addHelper("createClass"), args);
}
@@ -124,17 +123,27 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
});
replaceSupers.replace();
const superReturns = [];
- path.traverse(_core.traverse.visitors.merge([_helperEnvironmentVisitor.default, {
+ path.traverse(_traverse.visitors.environmentVisitor({
ReturnStatement(path) {
if (!path.getFunctionParent().isArrowFunctionExpression()) {
superReturns.push(path);
}
}
- }]));
+ }));
if (isConstructor) {
pushConstructor(superReturns, node, path);
} else {
- pushMethod(node, path);
+ {
+ var _path$ensureFunctionN;
+ (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
+ }
+ path.ensureFunctionName(supportUnicodeId);
+ let wrapped;
+ if (node !== path.node) {
+ wrapped = path.node;
+ path.replaceWith(node);
+ }
+ pushMethod(node, wrapped);
}
}
}
@@ -227,7 +236,7 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
return _core.types.callExpression(classState.file.addHelper("assertThisInitialized"), [thisRef()]);
};
const bareSupers = [];
- path.traverse(_core.traverse.visitors.merge([_helperEnvironmentVisitor.default, {
+ path.traverse(_traverse.visitors.environmentVisitor({
Super(path) {
const {
node,
@@ -239,7 +248,7 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
bareSupers.unshift(parentPath);
}
}
- }]));
+ }));
for (const bareSuper of bareSupers) {
wrapSuperCall(bareSuper, classState.superName, thisRef, body);
if (maxGuaranteedSuperBeforeIndex >= 0) {
@@ -257,6 +266,7 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
});
}
}
+ const guaranteedCalls = new Set();
for (const thisPath of classState.superThises) {
const {
node,
@@ -275,9 +285,18 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
return true;
}
});
- if (maxGuaranteedSuperBeforeIndex !== -1 && thisIndex > maxGuaranteedSuperBeforeIndex) {
+ let exprPath = thisPath.parentPath.isSequenceExpression() ? thisPath.parentPath : thisPath;
+ if (exprPath.listKey === "arguments" && (exprPath.parentPath.isCallExpression() || exprPath.parentPath.isOptionalCallExpression())) {
+ exprPath = exprPath.parentPath;
+ } else {
+ exprPath = null;
+ }
+ if (maxGuaranteedSuperBeforeIndex !== -1 && thisIndex > maxGuaranteedSuperBeforeIndex || guaranteedCalls.has(exprPath)) {
thisPath.replaceWith(thisRef());
} else {
+ if (exprPath) {
+ guaranteedCalls.add(exprPath);
+ }
thisPath.replaceWith(buildAssertThisInitialized());
}
}
@@ -305,28 +324,16 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
returnPath.get("argument").replaceWith(wrapReturn(returnPath.node.argument));
}
}
- function pushMethod(node, path) {
- const scope = path ? path.scope : classState.scope;
+ function pushMethod(node, wrapped) {
if (node.kind === "method") {
- if (processMethod(node, scope)) return;
+ if (processMethod(node)) return;
}
const placement = node.static ? "static" : "instance";
const methods = classState.methods[placement];
const descKey = node.kind === "method" ? "value" : node.kind;
const key = _core.types.isNumericLiteral(node.key) || _core.types.isBigIntLiteral(node.key) ? _core.types.stringLiteral(String(node.key.value)) : _core.types.toComputedKey(node);
- let fn = _core.types.toExpression(node);
- if (_core.types.isStringLiteral(key)) {
- if (node.kind === "method") {
- var _nameFunction;
- fn = (_nameFunction = (0, _helperFunctionName.default)({
- id: key,
- node: node,
- scope
- }, undefined, supportUnicodeId)) != null ? _nameFunction : fn;
- }
- } else {
- methods.hasComputed = true;
- }
+ methods.hasComputed = !_core.types.isStringLiteral(key);
+ const fn = wrapped != null ? wrapped : _core.types.toExpression(node);
let descriptor;
if (!methods.hasComputed && methods.map.has(key.value)) {
descriptor = methods.map.get(key.value);
@@ -348,7 +355,7 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
}
}
}
- function processMethod(node, scope) {
+ function processMethod(node) {
if (assumptions.setClassMethods && !node.decorators) {
let {
classRef
@@ -358,17 +365,8 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
classRef = classState.protoAlias;
}
const methodName = _core.types.memberExpression(_core.types.cloneNode(classRef), node.key, node.computed || _core.types.isLiteral(node.key));
- let func = _core.types.functionExpression(null, node.params, node.body, node.generator, node.async);
+ const func = _core.types.functionExpression(node.id, node.params, node.body, node.generator, node.async);
_core.types.inherits(func, node);
- const key = _core.types.toComputedKey(node, node.key);
- if (_core.types.isStringLiteral(key)) {
- var _nameFunction2;
- func = (_nameFunction2 = (0, _helperFunctionName.default)({
- node: func,
- id: key,
- scope
- }, undefined, supportUnicodeId)) != null ? _nameFunction2 : func;
- }
const expr = _core.types.expressionStatement(_core.types.assignmentExpression("=", methodName, func));
_core.types.inheritsComments(expr, node);
classState.body.push(expr);
@@ -493,7 +491,7 @@ function transformClass(path, file, builtinClasses, isLoose, assumptions, suppor
constructorBody.body.unshift(_core.types.expressionStatement(_core.types.callExpression(classState.file.addHelper("classCallCheck"), [_core.types.thisExpression(), _core.types.cloneNode(classState.classRef)])));
}
const isStrict = path.isInStrictMode();
- let constructorOnly = classState.classId && body.length === 0;
+ let constructorOnly = body.length === 0;
if (constructorOnly && !isStrict) {
for (const param of classState.construct.params) {
if (!_core.types.isIdentifier(param)) {