[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.extractComputedKeys = extractComputedKeys;
|
---|
[e29cc2e] | 7 | exports.injectInitialization = injectInitialization;
|
---|
[6a3a178] | 8 |
|
---|
| 9 | var _core = require("@babel/core");
|
---|
| 10 |
|
---|
| 11 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
---|
| 12 |
|
---|
| 13 | const findBareSupers = _core.traverse.visitors.merge([{
|
---|
| 14 | Super(path) {
|
---|
| 15 | const {
|
---|
| 16 | node,
|
---|
| 17 | parentPath
|
---|
| 18 | } = path;
|
---|
| 19 |
|
---|
| 20 | if (parentPath.isCallExpression({
|
---|
| 21 | callee: node
|
---|
| 22 | })) {
|
---|
| 23 | this.push(parentPath);
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | }, _helperReplaceSupers.environmentVisitor]);
|
---|
| 28 |
|
---|
| 29 | const referenceVisitor = {
|
---|
| 30 | "TSTypeAnnotation|TypeAnnotation"(path) {
|
---|
| 31 | path.skip();
|
---|
| 32 | },
|
---|
| 33 |
|
---|
| 34 | ReferencedIdentifier(path) {
|
---|
| 35 | if (this.scope.hasOwnBinding(path.node.name)) {
|
---|
| 36 | this.scope.rename(path.node.name);
|
---|
| 37 | path.skip();
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | function handleClassTDZ(path, state) {
|
---|
| 44 | if (state.classBinding && state.classBinding === path.scope.getBinding(path.node.name)) {
|
---|
| 45 | const classNameTDZError = state.file.addHelper("classNameTDZError");
|
---|
| 46 |
|
---|
| 47 | const throwNode = _core.types.callExpression(classNameTDZError, [_core.types.stringLiteral(path.node.name)]);
|
---|
| 48 |
|
---|
| 49 | path.replaceWith(_core.types.sequenceExpression([throwNode, path.node]));
|
---|
| 50 | path.skip();
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | const classFieldDefinitionEvaluationTDZVisitor = {
|
---|
| 55 | ReferencedIdentifier: handleClassTDZ
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | function injectInitialization(path, constructor, nodes, renamer) {
|
---|
| 59 | if (!nodes.length) return;
|
---|
| 60 | const isDerived = !!path.node.superClass;
|
---|
| 61 |
|
---|
| 62 | if (!constructor) {
|
---|
| 63 | const newConstructor = _core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([]));
|
---|
| 64 |
|
---|
| 65 | if (isDerived) {
|
---|
| 66 | newConstructor.params = [_core.types.restElement(_core.types.identifier("args"))];
|
---|
| 67 | newConstructor.body.body.push(_core.template.statement.ast`super(...args)`);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | [constructor] = path.get("body").unshiftContainer("body", newConstructor);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | if (renamer) {
|
---|
| 74 | renamer(referenceVisitor, {
|
---|
| 75 | scope: constructor.scope
|
---|
| 76 | });
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (isDerived) {
|
---|
| 80 | const bareSupers = [];
|
---|
| 81 | constructor.traverse(findBareSupers, bareSupers);
|
---|
| 82 | let isFirst = true;
|
---|
| 83 |
|
---|
| 84 | for (const bareSuper of bareSupers) {
|
---|
| 85 | if (isFirst) {
|
---|
| 86 | bareSuper.insertAfter(nodes);
|
---|
| 87 | isFirst = false;
|
---|
| 88 | } else {
|
---|
| 89 | bareSuper.insertAfter(nodes.map(n => _core.types.cloneNode(n)));
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | } else {
|
---|
| 93 | constructor.get("body").unshiftContainer("body", nodes);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | function extractComputedKeys(ref, path, computedPaths, file) {
|
---|
| 98 | const declarations = [];
|
---|
| 99 | const state = {
|
---|
| 100 | classBinding: path.node.id && path.scope.getBinding(path.node.id.name),
|
---|
| 101 | file
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | for (const computedPath of computedPaths) {
|
---|
| 105 | const computedKey = computedPath.get("key");
|
---|
| 106 |
|
---|
| 107 | if (computedKey.isReferencedIdentifier()) {
|
---|
| 108 | handleClassTDZ(computedKey, state);
|
---|
| 109 | } else {
|
---|
| 110 | computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | const computedNode = computedPath.node;
|
---|
| 114 |
|
---|
| 115 | if (!computedKey.isConstantExpression()) {
|
---|
| 116 | const ident = path.scope.generateUidIdentifierBasedOnNode(computedNode.key);
|
---|
| 117 | path.scope.push({
|
---|
| 118 | id: ident,
|
---|
| 119 | kind: "let"
|
---|
| 120 | });
|
---|
| 121 | declarations.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(ident), computedNode.key)));
|
---|
| 122 | computedNode.key = _core.types.cloneNode(ident);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | return declarations;
|
---|
| 127 | } |
---|