source: trip-planner-front/node_modules/@babel/helper-create-class-features-plugin/lib/misc.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.injectInitialization = injectInitialization;
7exports.extractComputedKeys = extractComputedKeys;
8
9var _core = require("@babel/core");
10
11var _helperReplaceSupers = require("@babel/helper-replace-supers");
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}, _helperReplaceSupers.environmentVisitor]);
28
29const 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
43function 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
54const classFieldDefinitionEvaluationTDZVisitor = {
55 ReferencedIdentifier: handleClassTDZ
56};
57
58function 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
97function 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}
Note: See TracBrowser for help on using the repository browser.