| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = transformClass;
|
|---|
| 7 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
|---|
| 8 | var _core = require("@babel/core");
|
|---|
| 9 | var _traverse = require("@babel/traverse");
|
|---|
| 10 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
|---|
| 11 | var _inlineCallSuperHelpers = require("./inline-callSuper-helpers.js");
|
|---|
| 12 | function buildConstructor(classRef, constructorBody, node) {
|
|---|
| 13 | const func = _core.types.functionDeclaration(_core.types.cloneNode(classRef), [], constructorBody);
|
|---|
| 14 | _core.types.inherits(func, node);
|
|---|
| 15 | return func;
|
|---|
| 16 | }
|
|---|
| 17 | function transformClass(path, file, builtinClasses, isLoose, assumptions, supportUnicodeId) {
|
|---|
| 18 | const classState = {
|
|---|
| 19 | parent: undefined,
|
|---|
| 20 | scope: undefined,
|
|---|
| 21 | node: undefined,
|
|---|
| 22 | path: undefined,
|
|---|
| 23 | file: undefined,
|
|---|
| 24 | classId: undefined,
|
|---|
| 25 | classRef: undefined,
|
|---|
| 26 | superName: null,
|
|---|
| 27 | superReturns: [],
|
|---|
| 28 | isDerived: false,
|
|---|
| 29 | extendsNative: false,
|
|---|
| 30 | construct: undefined,
|
|---|
| 31 | constructorBody: undefined,
|
|---|
| 32 | userConstructor: undefined,
|
|---|
| 33 | userConstructorPath: undefined,
|
|---|
| 34 | hasConstructor: false,
|
|---|
| 35 | body: [],
|
|---|
| 36 | superThises: [],
|
|---|
| 37 | pushedInherits: false,
|
|---|
| 38 | pushedCreateClass: false,
|
|---|
| 39 | protoAlias: null,
|
|---|
| 40 | isLoose: false,
|
|---|
| 41 | dynamicKeys: new Map(),
|
|---|
| 42 | methods: {
|
|---|
| 43 | instance: {
|
|---|
| 44 | hasComputed: false,
|
|---|
| 45 | list: [],
|
|---|
| 46 | map: new Map()
|
|---|
| 47 | },
|
|---|
| 48 | static: {
|
|---|
| 49 | hasComputed: false,
|
|---|
| 50 | list: [],
|
|---|
| 51 | map: new Map()
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | };
|
|---|
| 55 | const setState = newState => {
|
|---|
| 56 | Object.assign(classState, newState);
|
|---|
| 57 | };
|
|---|
| 58 | const findThisesVisitor = _traverse.visitors.environmentVisitor({
|
|---|
| 59 | ThisExpression(path) {
|
|---|
| 60 | classState.superThises.push(path);
|
|---|
| 61 | }
|
|---|
| 62 | });
|
|---|
| 63 | function createClassHelper(args) {
|
|---|
| 64 | return _core.types.callExpression(classState.file.addHelper("createClass"), args);
|
|---|
| 65 | }
|
|---|
| 66 | function maybeCreateConstructor() {
|
|---|
| 67 | const classBodyPath = classState.path.get("body");
|
|---|
| 68 | for (const path of classBodyPath.get("body")) {
|
|---|
| 69 | if (path.isClassMethod({
|
|---|
| 70 | kind: "constructor"
|
|---|
| 71 | })) return;
|
|---|
| 72 | }
|
|---|
| 73 | const params = [];
|
|---|
| 74 | let body;
|
|---|
| 75 | if (classState.isDerived) {
|
|---|
| 76 | body = _core.template.statement.ast`{
|
|---|
| 77 | super(...arguments);
|
|---|
| 78 | }`;
|
|---|
| 79 | } else {
|
|---|
| 80 | body = _core.types.blockStatement([]);
|
|---|
| 81 | }
|
|---|
| 82 | classBodyPath.unshiftContainer("body", _core.types.classMethod("constructor", _core.types.identifier("constructor"), params, body));
|
|---|
| 83 | }
|
|---|
| 84 | function buildBody() {
|
|---|
| 85 | maybeCreateConstructor();
|
|---|
| 86 | pushBody();
|
|---|
| 87 | verifyConstructor();
|
|---|
| 88 | if (classState.userConstructor) {
|
|---|
| 89 | const {
|
|---|
| 90 | constructorBody,
|
|---|
| 91 | userConstructor,
|
|---|
| 92 | construct
|
|---|
| 93 | } = classState;
|
|---|
| 94 | constructorBody.body.push(...userConstructor.body.body);
|
|---|
| 95 | _core.types.inherits(construct, userConstructor);
|
|---|
| 96 | _core.types.inherits(constructorBody, userConstructor.body);
|
|---|
| 97 | }
|
|---|
| 98 | pushDescriptors();
|
|---|
| 99 | }
|
|---|
| 100 | function pushBody() {
|
|---|
| 101 | const classBodyPaths = classState.path.get("body.body");
|
|---|
| 102 | for (const path of classBodyPaths) {
|
|---|
| 103 | const node = path.node;
|
|---|
| 104 | if (path.isClassProperty() || path.isClassPrivateProperty()) {
|
|---|
| 105 | throw path.buildCodeFrameError("Missing class properties transform.");
|
|---|
| 106 | }
|
|---|
| 107 | if (node.decorators) {
|
|---|
| 108 | throw path.buildCodeFrameError("Method has decorators, put the decorator plugin before the classes one.");
|
|---|
| 109 | }
|
|---|
| 110 | if (_core.types.isClassMethod(node)) {
|
|---|
| 111 | const isConstructor = node.kind === "constructor";
|
|---|
| 112 | const replaceSupers = new _helperReplaceSupers.default({
|
|---|
| 113 | methodPath: path,
|
|---|
| 114 | objectRef: classState.classRef,
|
|---|
| 115 | superRef: classState.superName,
|
|---|
| 116 | constantSuper: assumptions.constantSuper,
|
|---|
| 117 | file: classState.file,
|
|---|
| 118 | refToPreserve: classState.classRef
|
|---|
| 119 | });
|
|---|
| 120 | replaceSupers.replace();
|
|---|
| 121 | const superReturns = [];
|
|---|
| 122 | path.traverse(_traverse.visitors.environmentVisitor({
|
|---|
| 123 | ReturnStatement(path) {
|
|---|
| 124 | if (!path.getFunctionParent().isArrowFunctionExpression()) {
|
|---|
| 125 | superReturns.push(path);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | }));
|
|---|
| 129 | if (isConstructor) {
|
|---|
| 130 | pushConstructor(superReturns, node, path);
|
|---|
| 131 | } else {
|
|---|
| 132 | var _path$ensureFunctionN;
|
|---|
| 133 | (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
|
|---|
| 134 | path.ensureFunctionName(supportUnicodeId);
|
|---|
| 135 | let wrapped;
|
|---|
| 136 | if (node !== path.node) {
|
|---|
| 137 | wrapped = path.node;
|
|---|
| 138 | path.replaceWith(node);
|
|---|
| 139 | }
|
|---|
| 140 | pushMethod(node, wrapped);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | function pushDescriptors() {
|
|---|
| 146 | pushInheritsToBody();
|
|---|
| 147 | const {
|
|---|
| 148 | body
|
|---|
| 149 | } = classState;
|
|---|
| 150 | const props = {
|
|---|
| 151 | instance: null,
|
|---|
| 152 | static: null
|
|---|
| 153 | };
|
|---|
| 154 | for (const placement of ["static", "instance"]) {
|
|---|
| 155 | if (classState.methods[placement].list.length) {
|
|---|
| 156 | props[placement] = classState.methods[placement].list.map(desc => {
|
|---|
| 157 | const obj = _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("key"), desc.key)]);
|
|---|
| 158 | for (const kind of ["get", "set", "value"]) {
|
|---|
| 159 | if (desc[kind] != null) {
|
|---|
| 160 | obj.properties.push(_core.types.objectProperty(_core.types.identifier(kind), desc[kind]));
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | return obj;
|
|---|
| 164 | });
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 | if (props.instance || props.static) {
|
|---|
| 168 | let args = [_core.types.cloneNode(classState.classRef), props.instance ? _core.types.arrayExpression(props.instance) : _core.types.nullLiteral(), props.static ? _core.types.arrayExpression(props.static) : _core.types.nullLiteral()];
|
|---|
| 169 | let lastNonNullIndex = 0;
|
|---|
| 170 | for (let i = 0; i < args.length; i++) {
|
|---|
| 171 | if (!_core.types.isNullLiteral(args[i])) lastNonNullIndex = i;
|
|---|
| 172 | }
|
|---|
| 173 | args = args.slice(0, lastNonNullIndex + 1);
|
|---|
| 174 | body.push(_core.types.returnStatement(createClassHelper(args)));
|
|---|
| 175 | classState.pushedCreateClass = true;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | function wrapSuperCall(bareSuper, superRef, thisRef, body) {
|
|---|
| 179 | const bareSuperNode = bareSuper.node;
|
|---|
| 180 | let call;
|
|---|
| 181 | if (assumptions.superIsCallableConstructor) {
|
|---|
| 182 | bareSuperNode.arguments.unshift(_core.types.thisExpression());
|
|---|
| 183 | if (bareSuperNode.arguments.length === 2 && _core.types.isSpreadElement(bareSuperNode.arguments[1]) && _core.types.isIdentifier(bareSuperNode.arguments[1].argument, {
|
|---|
| 184 | name: "arguments"
|
|---|
| 185 | })) {
|
|---|
| 186 | bareSuperNode.arguments[1] = bareSuperNode.arguments[1].argument;
|
|---|
| 187 | bareSuperNode.callee = _core.types.memberExpression(_core.types.cloneNode(superRef), _core.types.identifier("apply"));
|
|---|
| 188 | } else {
|
|---|
| 189 | bareSuperNode.callee = _core.types.memberExpression(_core.types.cloneNode(superRef), _core.types.identifier("call"));
|
|---|
| 190 | }
|
|---|
| 191 | call = _core.types.logicalExpression("||", bareSuperNode, _core.types.thisExpression());
|
|---|
| 192 | } else {
|
|---|
| 193 | var _bareSuperNode$argume;
|
|---|
| 194 | const args = [_core.types.thisExpression(), _core.types.cloneNode(classState.classRef)];
|
|---|
| 195 | if ((_bareSuperNode$argume = bareSuperNode.arguments) != null && _bareSuperNode$argume.length) {
|
|---|
| 196 | const bareSuperNodeArguments = bareSuperNode.arguments;
|
|---|
| 197 | if (bareSuperNodeArguments.length === 1 && _core.types.isSpreadElement(bareSuperNodeArguments[0]) && _core.types.isIdentifier(bareSuperNodeArguments[0].argument, {
|
|---|
| 198 | name: "arguments"
|
|---|
| 199 | })) {
|
|---|
| 200 | args.push(bareSuperNodeArguments[0].argument);
|
|---|
| 201 | } else {
|
|---|
| 202 | args.push(_core.types.arrayExpression(bareSuperNodeArguments));
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | call = _core.types.callExpression((0, _inlineCallSuperHelpers.default)(classState.file), args);
|
|---|
| 206 | }
|
|---|
| 207 | if (bareSuper.parentPath.isExpressionStatement() && bareSuper.parentPath.container === body.node.body && body.node.body.length - 1 === bareSuper.parentPath.key) {
|
|---|
| 208 | if (classState.superThises.length) {
|
|---|
| 209 | call = _core.types.assignmentExpression("=", thisRef(), call);
|
|---|
| 210 | }
|
|---|
| 211 | bareSuper.parentPath.replaceWith(_core.types.returnStatement(call));
|
|---|
| 212 | } else {
|
|---|
| 213 | bareSuper.replaceWith(_core.types.assignmentExpression("=", thisRef(), call));
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | function verifyConstructor() {
|
|---|
| 217 | if (!classState.isDerived) return;
|
|---|
| 218 | const path = classState.userConstructorPath;
|
|---|
| 219 | const body = path.get("body");
|
|---|
| 220 | const constructorBody = path.get("body");
|
|---|
| 221 | let maxGuaranteedSuperBeforeIndex = constructorBody.node.body.length;
|
|---|
| 222 | path.traverse(findThisesVisitor);
|
|---|
| 223 | let thisRef = function () {
|
|---|
| 224 | const ref = path.scope.generateDeclaredUidIdentifier("this");
|
|---|
| 225 | maxGuaranteedSuperBeforeIndex++;
|
|---|
| 226 | thisRef = () => _core.types.cloneNode(ref);
|
|---|
| 227 | return ref;
|
|---|
| 228 | };
|
|---|
| 229 | const buildAssertThisInitialized = function () {
|
|---|
| 230 | return _core.types.callExpression(classState.file.addHelper("assertThisInitialized"), [thisRef()]);
|
|---|
| 231 | };
|
|---|
| 232 | const bareSupers = [];
|
|---|
| 233 | path.traverse(_traverse.visitors.environmentVisitor({
|
|---|
| 234 | Super(path) {
|
|---|
| 235 | const {
|
|---|
| 236 | node,
|
|---|
| 237 | parentPath
|
|---|
| 238 | } = path;
|
|---|
| 239 | if (parentPath.isCallExpression({
|
|---|
| 240 | callee: node
|
|---|
| 241 | })) {
|
|---|
| 242 | bareSupers.unshift(parentPath);
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 | }));
|
|---|
| 246 | for (const bareSuper of bareSupers) {
|
|---|
| 247 | wrapSuperCall(bareSuper, classState.superName, thisRef, body);
|
|---|
| 248 | if (maxGuaranteedSuperBeforeIndex >= 0) {
|
|---|
| 249 | let lastParentPath;
|
|---|
| 250 | bareSuper.find(function (parentPath) {
|
|---|
| 251 | if (parentPath === constructorBody) {
|
|---|
| 252 | maxGuaranteedSuperBeforeIndex = Math.min(maxGuaranteedSuperBeforeIndex, lastParentPath.key);
|
|---|
| 253 | return true;
|
|---|
| 254 | }
|
|---|
| 255 | const {
|
|---|
| 256 | type
|
|---|
| 257 | } = parentPath;
|
|---|
| 258 | switch (type) {
|
|---|
| 259 | case "ExpressionStatement":
|
|---|
| 260 | case "SequenceExpression":
|
|---|
| 261 | case "AssignmentExpression":
|
|---|
| 262 | case "BinaryExpression":
|
|---|
| 263 | case "MemberExpression":
|
|---|
| 264 | case "CallExpression":
|
|---|
| 265 | case "NewExpression":
|
|---|
| 266 | case "VariableDeclarator":
|
|---|
| 267 | case "VariableDeclaration":
|
|---|
| 268 | case "BlockStatement":
|
|---|
| 269 | case "ArrayExpression":
|
|---|
| 270 | case "ObjectExpression":
|
|---|
| 271 | case "ObjectProperty":
|
|---|
| 272 | case "TemplateLiteral":
|
|---|
| 273 | lastParentPath = parentPath;
|
|---|
| 274 | return false;
|
|---|
| 275 | default:
|
|---|
| 276 | if (type === "LogicalExpression" && parentPath.node.left === lastParentPath.node || parentPath.isConditional() && parentPath.node.test === lastParentPath.node || type === "OptionalCallExpression" && parentPath.node.callee === lastParentPath.node || type === "OptionalMemberExpression" && parentPath.node.object === lastParentPath.node) {
|
|---|
| 277 | lastParentPath = parentPath;
|
|---|
| 278 | return false;
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | maxGuaranteedSuperBeforeIndex = -1;
|
|---|
| 282 | return true;
|
|---|
| 283 | });
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 | const guaranteedCalls = new Set();
|
|---|
| 287 | for (const thisPath of classState.superThises) {
|
|---|
| 288 | const {
|
|---|
| 289 | node,
|
|---|
| 290 | parentPath
|
|---|
| 291 | } = thisPath;
|
|---|
| 292 | if (parentPath.isMemberExpression({
|
|---|
| 293 | object: node
|
|---|
| 294 | })) {
|
|---|
| 295 | thisPath.replaceWith(thisRef());
|
|---|
| 296 | continue;
|
|---|
| 297 | }
|
|---|
| 298 | let thisIndex;
|
|---|
| 299 | thisPath.find(function (parentPath) {
|
|---|
| 300 | if (parentPath.parentPath === constructorBody) {
|
|---|
| 301 | thisIndex = parentPath.key;
|
|---|
| 302 | return true;
|
|---|
| 303 | }
|
|---|
| 304 | });
|
|---|
| 305 | let exprPath = thisPath.parentPath.isSequenceExpression() ? thisPath.parentPath : thisPath;
|
|---|
| 306 | if (exprPath.listKey === "arguments" && (exprPath.parentPath.isCallExpression() || exprPath.parentPath.isOptionalCallExpression())) {
|
|---|
| 307 | exprPath = exprPath.parentPath;
|
|---|
| 308 | } else {
|
|---|
| 309 | exprPath = null;
|
|---|
| 310 | }
|
|---|
| 311 | if (maxGuaranteedSuperBeforeIndex !== -1 && thisIndex > maxGuaranteedSuperBeforeIndex || guaranteedCalls.has(exprPath)) {
|
|---|
| 312 | thisPath.replaceWith(thisRef());
|
|---|
| 313 | } else {
|
|---|
| 314 | if (exprPath) {
|
|---|
| 315 | guaranteedCalls.add(exprPath);
|
|---|
| 316 | }
|
|---|
| 317 | thisPath.replaceWith(buildAssertThisInitialized());
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 | let wrapReturn;
|
|---|
| 321 | if (classState.isLoose) {
|
|---|
| 322 | wrapReturn = returnArg => {
|
|---|
| 323 | const thisExpr = buildAssertThisInitialized();
|
|---|
| 324 | return returnArg ? _core.types.logicalExpression("||", returnArg, thisExpr) : thisExpr;
|
|---|
| 325 | };
|
|---|
| 326 | } else {
|
|---|
| 327 | wrapReturn = returnArg => {
|
|---|
| 328 | const returnParams = [thisRef()];
|
|---|
| 329 | if (returnArg != null) {
|
|---|
| 330 | returnParams.push(returnArg);
|
|---|
| 331 | }
|
|---|
| 332 | return _core.types.callExpression(classState.file.addHelper("possibleConstructorReturn"), returnParams);
|
|---|
| 333 | };
|
|---|
| 334 | }
|
|---|
| 335 | const bodyPaths = body.get("body");
|
|---|
| 336 | const guaranteedSuperBeforeFinish = maxGuaranteedSuperBeforeIndex !== -1 && maxGuaranteedSuperBeforeIndex < bodyPaths.length;
|
|---|
| 337 | if (!bodyPaths.length || !bodyPaths.pop().isReturnStatement()) {
|
|---|
| 338 | body.pushContainer("body", _core.types.returnStatement(guaranteedSuperBeforeFinish ? thisRef() : buildAssertThisInitialized()));
|
|---|
| 339 | }
|
|---|
| 340 | for (const returnPath of classState.superReturns) {
|
|---|
| 341 | returnPath.get("argument").replaceWith(wrapReturn(returnPath.node.argument));
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 | function pushMethod(node, wrapped) {
|
|---|
| 345 | if (node.kind === "method") {
|
|---|
| 346 | if (processMethod(node)) return;
|
|---|
| 347 | }
|
|---|
| 348 | const placement = node.static ? "static" : "instance";
|
|---|
| 349 | const methods = classState.methods[placement];
|
|---|
| 350 | const descKey = node.kind === "method" ? "value" : node.kind;
|
|---|
| 351 | const key = _core.types.isNumericLiteral(node.key) || _core.types.isBigIntLiteral(node.key) ? _core.types.stringLiteral(String(node.key.value)) : _core.types.toComputedKey(node);
|
|---|
| 352 | methods.hasComputed = !_core.types.isStringLiteral(key);
|
|---|
| 353 | const fn = wrapped != null ? wrapped : _core.types.toExpression(node);
|
|---|
| 354 | let descriptor;
|
|---|
| 355 | if (!methods.hasComputed && methods.map.has(key.value)) {
|
|---|
| 356 | descriptor = methods.map.get(key.value);
|
|---|
| 357 | descriptor[descKey] = fn;
|
|---|
| 358 | if (descKey === "value") {
|
|---|
| 359 | descriptor.get = null;
|
|---|
| 360 | descriptor.set = null;
|
|---|
| 361 | } else {
|
|---|
| 362 | descriptor.value = null;
|
|---|
| 363 | }
|
|---|
| 364 | } else {
|
|---|
| 365 | descriptor = {
|
|---|
| 366 | key: key,
|
|---|
| 367 | [descKey]: fn
|
|---|
| 368 | };
|
|---|
| 369 | methods.list.push(descriptor);
|
|---|
| 370 | if (!methods.hasComputed) {
|
|---|
| 371 | methods.map.set(key.value, descriptor);
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | function processMethod(node) {
|
|---|
| 376 | if (assumptions.setClassMethods && !node.decorators) {
|
|---|
| 377 | let {
|
|---|
| 378 | classRef
|
|---|
| 379 | } = classState;
|
|---|
| 380 | if (!node.static) {
|
|---|
| 381 | insertProtoAliasOnce();
|
|---|
| 382 | classRef = classState.protoAlias;
|
|---|
| 383 | }
|
|---|
| 384 | const methodName = _core.types.memberExpression(_core.types.cloneNode(classRef), node.key, node.computed || _core.types.isLiteral(node.key));
|
|---|
| 385 | const func = _core.types.functionExpression(node.id, node.params, node.body, node.generator, node.async);
|
|---|
| 386 | _core.types.inherits(func, node);
|
|---|
| 387 | const expr = _core.types.expressionStatement(_core.types.assignmentExpression("=", methodName, func));
|
|---|
| 388 | _core.types.inheritsComments(expr, node);
|
|---|
| 389 | classState.body.push(expr);
|
|---|
| 390 | return true;
|
|---|
| 391 | }
|
|---|
| 392 | return false;
|
|---|
| 393 | }
|
|---|
| 394 | function insertProtoAliasOnce() {
|
|---|
| 395 | if (classState.protoAlias === null) {
|
|---|
| 396 | setState({
|
|---|
| 397 | protoAlias: classState.scope.generateUidIdentifier("proto")
|
|---|
| 398 | });
|
|---|
| 399 | const classProto = _core.types.memberExpression(classState.classRef, _core.types.identifier("prototype"));
|
|---|
| 400 | const protoDeclaration = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(classState.protoAlias, classProto)]);
|
|---|
| 401 | classState.body.push(protoDeclaration);
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 | function pushConstructor(superReturns, method, path) {
|
|---|
| 405 | setState({
|
|---|
| 406 | userConstructorPath: path,
|
|---|
| 407 | userConstructor: method,
|
|---|
| 408 | hasConstructor: true,
|
|---|
| 409 | superReturns
|
|---|
| 410 | });
|
|---|
| 411 | const {
|
|---|
| 412 | construct
|
|---|
| 413 | } = classState;
|
|---|
| 414 | _core.types.inheritsComments(construct, method);
|
|---|
| 415 | construct.params = method.params;
|
|---|
| 416 | _core.types.inherits(construct.body, method.body);
|
|---|
| 417 | construct.body.directives = method.body.directives;
|
|---|
| 418 | if (classState.hasInstanceDescriptors || classState.hasStaticDescriptors) {
|
|---|
| 419 | pushDescriptors();
|
|---|
| 420 | }
|
|---|
| 421 | pushInheritsToBody();
|
|---|
| 422 | }
|
|---|
| 423 | function pushInheritsToBody() {
|
|---|
| 424 | if (!classState.isDerived || classState.pushedInherits) return;
|
|---|
| 425 | classState.pushedInherits = true;
|
|---|
| 426 | classState.body.unshift(_core.types.expressionStatement(_core.types.callExpression(classState.file.addHelper(classState.isLoose ? "inheritsLoose" : "inherits"), [_core.types.cloneNode(classState.classRef), _core.types.cloneNode(classState.superName)])));
|
|---|
| 427 | }
|
|---|
| 428 | function extractDynamicKeys() {
|
|---|
| 429 | const {
|
|---|
| 430 | dynamicKeys,
|
|---|
| 431 | node,
|
|---|
| 432 | scope
|
|---|
| 433 | } = classState;
|
|---|
| 434 | for (const elem of node.body.body) {
|
|---|
| 435 | if (!_core.types.isClassMethod(elem) || !elem.computed) continue;
|
|---|
| 436 | if (scope.isPure(elem.key, true)) continue;
|
|---|
| 437 | const id = scope.generateUidIdentifierBasedOnNode(elem.key);
|
|---|
| 438 | dynamicKeys.set(id.name, elem.key);
|
|---|
| 439 | elem.key = id;
|
|---|
| 440 | }
|
|---|
| 441 | }
|
|---|
| 442 | function setupClosureParamsArgs() {
|
|---|
| 443 | const {
|
|---|
| 444 | superName,
|
|---|
| 445 | dynamicKeys
|
|---|
| 446 | } = classState;
|
|---|
| 447 | const closureParams = [];
|
|---|
| 448 | const closureArgs = [];
|
|---|
| 449 | if (classState.isDerived) {
|
|---|
| 450 | let arg = _core.types.cloneNode(superName);
|
|---|
| 451 | if (classState.extendsNative) {
|
|---|
| 452 | arg = _core.types.callExpression(classState.file.addHelper("wrapNativeSuper"), [arg]);
|
|---|
| 453 | (0, _helperAnnotateAsPure.default)(arg);
|
|---|
| 454 | }
|
|---|
| 455 | const param = classState.scope.generateUidIdentifierBasedOnNode(superName);
|
|---|
| 456 | closureParams.push(param);
|
|---|
| 457 | closureArgs.push(arg);
|
|---|
| 458 | setState({
|
|---|
| 459 | superName: _core.types.cloneNode(param)
|
|---|
| 460 | });
|
|---|
| 461 | }
|
|---|
| 462 | for (const [name, value] of dynamicKeys) {
|
|---|
| 463 | closureParams.push(_core.types.identifier(name));
|
|---|
| 464 | closureArgs.push(value);
|
|---|
| 465 | }
|
|---|
| 466 | return {
|
|---|
| 467 | closureParams,
|
|---|
| 468 | closureArgs
|
|---|
| 469 | };
|
|---|
| 470 | }
|
|---|
| 471 | function classTransformer(path, file, builtinClasses, isLoose) {
|
|---|
| 472 | setState({
|
|---|
| 473 | parent: path.parent,
|
|---|
| 474 | scope: path.scope,
|
|---|
| 475 | node: path.node,
|
|---|
| 476 | path,
|
|---|
| 477 | file,
|
|---|
| 478 | isLoose
|
|---|
| 479 | });
|
|---|
| 480 | setState({
|
|---|
| 481 | classId: classState.node.id,
|
|---|
| 482 | classRef: classState.node.id ? _core.types.identifier(classState.node.id.name) : classState.scope.generateUidIdentifier("class"),
|
|---|
| 483 | superName: classState.node.superClass,
|
|---|
| 484 | isDerived: !!classState.node.superClass,
|
|---|
| 485 | constructorBody: _core.types.blockStatement([])
|
|---|
| 486 | });
|
|---|
| 487 | setState({
|
|---|
| 488 | extendsNative: _core.types.isIdentifier(classState.superName) && builtinClasses.has(classState.superName.name) && !classState.scope.hasBinding(classState.superName.name, true)
|
|---|
| 489 | });
|
|---|
| 490 | const {
|
|---|
| 491 | classRef,
|
|---|
| 492 | node,
|
|---|
| 493 | constructorBody
|
|---|
| 494 | } = classState;
|
|---|
| 495 | setState({
|
|---|
| 496 | construct: buildConstructor(classRef, constructorBody, node)
|
|---|
| 497 | });
|
|---|
| 498 | extractDynamicKeys();
|
|---|
| 499 | const {
|
|---|
| 500 | body
|
|---|
| 501 | } = classState;
|
|---|
| 502 | const {
|
|---|
| 503 | closureParams,
|
|---|
| 504 | closureArgs
|
|---|
| 505 | } = setupClosureParamsArgs();
|
|---|
| 506 | buildBody();
|
|---|
| 507 | if (!assumptions.noClassCalls) {
|
|---|
| 508 | constructorBody.body.unshift(_core.types.expressionStatement(_core.types.callExpression(classState.file.addHelper("classCallCheck"), [_core.types.thisExpression(), _core.types.cloneNode(classState.classRef)])));
|
|---|
| 509 | }
|
|---|
| 510 | const isStrict = path.isInStrictMode();
|
|---|
| 511 | let constructorOnly = body.length === 0;
|
|---|
| 512 | if (constructorOnly && !isStrict) {
|
|---|
| 513 | for (const param of classState.construct.params) {
|
|---|
| 514 | if (!_core.types.isIdentifier(param)) {
|
|---|
| 515 | constructorOnly = false;
|
|---|
| 516 | break;
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 | }
|
|---|
| 520 | const directives = constructorOnly ? classState.construct.body.directives : [];
|
|---|
| 521 | if (!isStrict) {
|
|---|
| 522 | directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
|
|---|
| 523 | }
|
|---|
| 524 | if (constructorOnly) {
|
|---|
| 525 | const expr = _core.types.toExpression(classState.construct);
|
|---|
| 526 | return classState.isLoose ? expr : createClassHelper([expr]);
|
|---|
| 527 | }
|
|---|
| 528 | if (!classState.pushedCreateClass) {
|
|---|
| 529 | body.push(_core.types.returnStatement(classState.isLoose ? _core.types.cloneNode(classState.classRef) : createClassHelper([_core.types.cloneNode(classState.classRef)])));
|
|---|
| 530 | }
|
|---|
| 531 | body.unshift(classState.construct);
|
|---|
| 532 | const container = _core.types.arrowFunctionExpression(closureParams, _core.types.blockStatement(body, directives));
|
|---|
| 533 | return _core.types.callExpression(container, closureArgs);
|
|---|
| 534 | }
|
|---|
| 535 | return classTransformer(path, file, builtinClasses, isLoose);
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | //# sourceMappingURL=transformClass.js.map
|
|---|