source: trip-planner-front/node_modules/@babel/plugin-transform-computed-properties/lib/index.js@ 188ee53

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

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _helperPluginUtils = require("@babel/helper-plugin-utils");
9
10var _core = require("@babel/core");
11
12var _default = (0, _helperPluginUtils.declare)((api, options) => {
13 var _api$assumption;
14
15 api.assertVersion(7);
16 const setComputedProperties = (_api$assumption = api.assumption("setComputedProperties")) != null ? _api$assumption : options.loose;
17 const pushComputedProps = setComputedProperties ? pushComputedPropsLoose : pushComputedPropsSpec;
18 const buildMutatorMapAssign = (0, _core.template)(`
19 MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {};
20 MUTATOR_MAP_REF[KEY].KIND = VALUE;
21 `);
22
23 function getValue(prop) {
24 if (_core.types.isObjectProperty(prop)) {
25 return prop.value;
26 } else if (_core.types.isObjectMethod(prop)) {
27 return _core.types.functionExpression(null, prop.params, prop.body, prop.generator, prop.async);
28 }
29 }
30
31 function pushAssign(objId, prop, body) {
32 if (prop.kind === "get" && prop.kind === "set") {
33 pushMutatorDefine(objId, prop, body);
34 } else {
35 body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(objId), prop.key, prop.computed || _core.types.isLiteral(prop.key)), getValue(prop))));
36 }
37 }
38
39 function pushMutatorDefine({
40 body,
41 getMutatorId,
42 scope
43 }, prop) {
44 let key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key;
45 const maybeMemoise = scope.maybeGenerateMemoised(key);
46
47 if (maybeMemoise) {
48 body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", maybeMemoise, key)));
49 key = maybeMemoise;
50 }
51
52 body.push(...buildMutatorMapAssign({
53 MUTATOR_MAP_REF: getMutatorId(),
54 KEY: _core.types.cloneNode(key),
55 VALUE: getValue(prop),
56 KIND: _core.types.identifier(prop.kind)
57 }));
58 }
59
60 function pushComputedPropsLoose(info) {
61 for (const prop of info.computedProps) {
62 if (prop.kind === "get" || prop.kind === "set") {
63 pushMutatorDefine(info, prop);
64 } else {
65 pushAssign(_core.types.cloneNode(info.objId), prop, info.body);
66 }
67 }
68 }
69
70 function pushComputedPropsSpec(info) {
71 const {
72 objId,
73 body,
74 computedProps,
75 state
76 } = info;
77
78 for (const prop of computedProps) {
79 const key = _core.types.toComputedKey(prop);
80
81 if (prop.kind === "get" || prop.kind === "set") {
82 pushMutatorDefine(info, prop);
83 } else {
84 if (computedProps.length === 1) {
85 return _core.types.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, getValue(prop)]);
86 } else {
87 body.push(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineProperty"), [_core.types.cloneNode(objId), key, getValue(prop)])));
88 }
89 }
90 }
91 }
92
93 return {
94 name: "transform-computed-properties",
95 visitor: {
96 ObjectExpression: {
97 exit(path, state) {
98 const {
99 node,
100 parent,
101 scope
102 } = path;
103 let hasComputed = false;
104
105 for (const prop of node.properties) {
106 hasComputed = prop.computed === true;
107 if (hasComputed) break;
108 }
109
110 if (!hasComputed) return;
111 const initProps = [];
112 const computedProps = [];
113 let foundComputed = false;
114
115 for (const prop of node.properties) {
116 if (prop.computed) {
117 foundComputed = true;
118 }
119
120 if (foundComputed) {
121 computedProps.push(prop);
122 } else {
123 initProps.push(prop);
124 }
125 }
126
127 const objId = scope.generateUidIdentifierBasedOnNode(parent);
128
129 const initPropExpression = _core.types.objectExpression(initProps);
130
131 const body = [];
132 body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(objId, initPropExpression)]));
133 let mutatorRef;
134
135 const getMutatorId = function () {
136 if (!mutatorRef) {
137 mutatorRef = scope.generateUidIdentifier("mutatorMap");
138 body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(mutatorRef, _core.types.objectExpression([]))]));
139 }
140
141 return _core.types.cloneNode(mutatorRef);
142 };
143
144 const single = pushComputedProps({
145 scope,
146 objId,
147 body,
148 computedProps,
149 initPropExpression,
150 getMutatorId,
151 state
152 });
153
154 if (mutatorRef) {
155 body.push(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineEnumerableProperties"), [_core.types.cloneNode(objId), _core.types.cloneNode(mutatorRef)])));
156 }
157
158 if (single) {
159 path.replaceWith(single);
160 } else {
161 body.push(_core.types.expressionStatement(_core.types.cloneNode(objId)));
162 path.replaceWithMultiple(body);
163 }
164 }
165
166 }
167 }
168 };
169});
170
171exports.default = _default;
Note: See TracBrowser for help on using the repository browser.