source: frontend/node_modules/@babel/helper-create-class-features-plugin/lib/misc.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.extractComputedKeys = extractComputedKeys;
7exports.injectInitialization = injectInitialization;
8exports.memoiseComputedKey = memoiseComputedKey;
9var _core = require("@babel/core");
10var _traverse = require("@babel/traverse");
11const 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});
24const 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};
37function 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}
45const classFieldDefinitionEvaluationTDZVisitor = {
46 ReferencedIdentifier: handleClassTDZ,
47 "TSTypeAnnotation|TypeAnnotation"(path) {
48 path.skip();
49 }
50};
51function injectInitialization(path, constructor, nodes, renamer, lastReturnsThis) {
52 if (!nodes.length) return;
53 const isDerived = !!path.node.superClass;
54 if (!constructor) {
55 const newConstructor = _core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([]));
56 if (isDerived) {
57 newConstructor.params = [_core.types.restElement(_core.types.identifier("args"))];
58 newConstructor.body.body.push(_core.template.statement.ast`super(...args)`);
59 }
60 [constructor] = path.get("body").unshiftContainer("body", newConstructor);
61 }
62 if (renamer) {
63 renamer(referenceVisitor, {
64 scope: constructor.scope
65 });
66 }
67 if (isDerived) {
68 const bareSupers = [];
69 constructor.traverse(findBareSupers, bareSupers);
70 let isFirst = true;
71 for (const bareSuper of bareSupers) {
72 if (isFirst) {
73 isFirst = false;
74 } else {
75 nodes = nodes.map(n => _core.types.cloneNode(n));
76 }
77 if (!bareSuper.parentPath.isExpressionStatement()) {
78 const allNodes = [bareSuper.node, ...nodes.map(n => _core.types.toExpression(n))];
79 if (!lastReturnsThis) allNodes.push(_core.types.thisExpression());
80 bareSuper.replaceWith(_core.types.sequenceExpression(allNodes));
81 } else {
82 bareSuper.insertAfter(nodes);
83 }
84 }
85 } else {
86 constructor.get("body").unshiftContainer("body", nodes);
87 }
88}
89function memoiseComputedKey(keyNode, scope, hint) {
90 const isUidReference = _core.types.isIdentifier(keyNode) && scope.hasUid(keyNode.name);
91 if (isUidReference) {
92 return;
93 }
94 const isMemoiseAssignment = _core.types.isAssignmentExpression(keyNode, {
95 operator: "="
96 }) && _core.types.isIdentifier(keyNode.left) && scope.hasUid(keyNode.left.name);
97 if (isMemoiseAssignment) {
98 return _core.types.cloneNode(keyNode);
99 } else {
100 const ident = _core.types.identifier(hint);
101 scope.push({
102 id: ident,
103 kind: "let"
104 });
105 return _core.types.assignmentExpression("=", _core.types.cloneNode(ident), keyNode);
106 }
107}
108function extractComputedKeys(path, computedPaths, file) {
109 const {
110 scope
111 } = path;
112 const declarations = [];
113 const state = {
114 classBinding: path.node.id && scope.getBinding(path.node.id.name),
115 file
116 };
117 for (const computedPath of computedPaths) {
118 const computedKey = computedPath.get("key");
119 if (computedKey.isReferencedIdentifier()) {
120 handleClassTDZ(computedKey, state);
121 } else {
122 computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);
123 }
124 const computedNode = computedPath.node;
125 if (!computedKey.isConstantExpression()) {
126 const assignment = memoiseComputedKey(computedKey.node, scope, scope.generateUidBasedOnNode(computedKey.node));
127 if (assignment) {
128 declarations.push(_core.types.expressionStatement(assignment));
129 computedNode.key = _core.types.cloneNode(assignment.left);
130 }
131 }
132 }
133 return declarations;
134}
135
136//# sourceMappingURL=misc.js.map
Note: See TracBrowser for help on using the repository browser.