source: imaps-frontend/node_modules/@babel/plugin-transform-computed-properties/lib/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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