UNPKG

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