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