babel-plugin-essor 0.0.10-beta.22 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,700 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
18
+ // src/jsx/index.ts
19
+ import { types as t3 } from "@babel/core";
20
+
21
+ // ../shared/dist/shared.js
22
+ var isArray = Array.isArray;
23
+ function isString(val) {
24
+ return typeof val === "string";
25
+ }
26
+ var noop = Function.prototype;
27
+ function startsWith(str, searchString) {
28
+ if (!isString(str)) {
29
+ return false;
30
+ }
31
+ return str.indexOf(searchString) === 0;
32
+ }
33
+ var capitalizeFirstLetter = (inputString) => {
34
+ return inputString.charAt(0).toUpperCase() + inputString.slice(1);
35
+ };
36
+
37
+ // src/program.ts
38
+ import { types as t } from "@babel/core";
39
+ var imports = /* @__PURE__ */ new Set();
40
+ var defaultOption = {
41
+ ssg: false,
42
+ symbol: "$",
43
+ props: true
44
+ };
45
+ var transformProgram = {
46
+ enter(path, state) {
47
+ imports.clear();
48
+ state.opts = __spreadValues(__spreadValues({}, defaultOption), state.opts);
49
+ path.state = {
50
+ h: path.scope.generateUidIdentifier("h$"),
51
+ renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
52
+ template: path.scope.generateUidIdentifier("template$"),
53
+ useSignal: path.scope.generateUidIdentifier("signal$"),
54
+ useComputed: path.scope.generateUidIdentifier("computed$"),
55
+ useReactive: path.scope.generateUidIdentifier("reactive$"),
56
+ tmplDeclaration: t.variableDeclaration("const", []),
57
+ opts: state.opts
58
+ };
59
+ },
60
+ exit(path) {
61
+ const state = path.state;
62
+ if (state.tmplDeclaration.declarations.length > 0) {
63
+ const index = path.node.body.findIndex(
64
+ (node) => !t.isImportDeclaration(node) && !t.isExportDeclaration(node)
65
+ );
66
+ path.node.body.splice(index, 0, state.tmplDeclaration);
67
+ }
68
+ if (imports.size > 0) {
69
+ path.node.body.unshift(createImport(state, "essor"));
70
+ }
71
+ }
72
+ };
73
+ function createImport(state, from) {
74
+ const ImportSpecifier = [];
75
+ imports.forEach((name) => {
76
+ const local = t.identifier(state[name].name);
77
+ const imported = t.identifier(name);
78
+ ImportSpecifier.push(t.importSpecifier(local, imported));
79
+ });
80
+ const importSource = t.stringLiteral(from);
81
+ return t.importDeclaration(ImportSpecifier, importSource);
82
+ }
83
+
84
+ // src/shared.ts
85
+ import { types as t2 } from "@babel/core";
86
+ function hasSiblingElement(path) {
87
+ const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
88
+ const hasSibling = siblings.some(
89
+ (siblingPath) => siblingPath.isJSXElement() || siblingPath.isJSXExpressionContainer()
90
+ );
91
+ return hasSibling;
92
+ }
93
+ function getAttrName(attribute) {
94
+ if (t2.isJSXIdentifier(attribute.name)) {
95
+ return attribute.name.name;
96
+ }
97
+ if (t2.isJSXNamespacedName(attribute.name)) {
98
+ return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
99
+ }
100
+ throw new Error("Unsupported attribute type");
101
+ }
102
+ function getTagName(node) {
103
+ const tag = node.openingElement.name;
104
+ return jsxElementNameToString(tag);
105
+ }
106
+ function jsxElementNameToString(node) {
107
+ if (t2.isJSXMemberExpression(node)) {
108
+ return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
109
+ }
110
+ if (t2.isJSXIdentifier(node) || t2.isIdentifier(node)) {
111
+ return node.name;
112
+ }
113
+ return `${node.namespace.name}:${node.name.name}`;
114
+ }
115
+ function isComponent(tagName) {
116
+ return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-z]/i.test(tagName[0]);
117
+ }
118
+ function isTextChild(path) {
119
+ if (path.isJSXExpressionContainer()) {
120
+ const expression = path.get("expression");
121
+ if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
122
+ return true;
123
+ }
124
+ }
125
+ if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
126
+ return true;
127
+ }
128
+ return false;
129
+ }
130
+ function setNodeText(path, text) {
131
+ if (path.isJSXText()) {
132
+ path.node.value = text;
133
+ }
134
+ if (path.isJSXExpressionContainer()) {
135
+ const expression = path.get("expression");
136
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
137
+ expression.replaceWith(t2.stringLiteral(text));
138
+ }
139
+ }
140
+ }
141
+ function isSymbolStart(path, name) {
142
+ const state = path.state;
143
+ const { symbol } = state.opts;
144
+ return startsWith(name, symbol);
145
+ }
146
+
147
+ // src/jsx/constants.ts
148
+ var selfClosingTags = [
149
+ "area",
150
+ "base",
151
+ "br",
152
+ "col",
153
+ "embed",
154
+ "hr",
155
+ "img",
156
+ "input",
157
+ "link",
158
+ "meta",
159
+ "param",
160
+ "source",
161
+ "track",
162
+ "wbr"
163
+ ];
164
+ var svgTags = [
165
+ "circle",
166
+ "clipPath",
167
+ "defs",
168
+ "ellipse",
169
+ "filter",
170
+ "g",
171
+ "line",
172
+ "linearGradient",
173
+ "mask",
174
+ "path",
175
+ "pattern",
176
+ "polygon",
177
+ "polyline",
178
+ "radialGradient",
179
+ "rect",
180
+ "stop",
181
+ "symbol",
182
+ "text",
183
+ "use"
184
+ ];
185
+
186
+ // src/jsx/index.ts
187
+ var isSsg = false;
188
+ function addToTemplate(result, content) {
189
+ if (isSsg) {
190
+ result.template.push(content);
191
+ } else {
192
+ result.template += content;
193
+ }
194
+ }
195
+ function transformJSX(path) {
196
+ const state = path.state;
197
+ isSsg = state.opts.ssg;
198
+ const result = {
199
+ index: 1,
200
+ isLastChild: false,
201
+ parentIndex: 0,
202
+ props: {},
203
+ template: isSsg ? [] : ""
204
+ };
205
+ transformJSXElement(path, result, true);
206
+ path.replaceWith(createEssorNode(path, result));
207
+ }
208
+ function createEssorNode(path, result) {
209
+ var _a;
210
+ const state = path.state;
211
+ let tmpl;
212
+ if (path.isJSXElement() && isComponent(getTagName(path.node))) {
213
+ tmpl = t3.identifier(getTagName(path.node));
214
+ } else {
215
+ tmpl = path.scope.generateUidIdentifier("_tmpl$");
216
+ const template = isSsg ? t3.arrayExpression(result.template.map(t3.stringLiteral)) : t3.callExpression(state.template, [t3.stringLiteral(result.template)]);
217
+ const declarator = t3.variableDeclarator(tmpl, template);
218
+ state.tmplDeclaration.declarations.push(declarator);
219
+ if (!isSsg) {
220
+ imports.add("template");
221
+ }
222
+ }
223
+ const args = [tmpl, createProps(result.props)];
224
+ const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
225
+ if (key) {
226
+ args.push(key);
227
+ }
228
+ imports.add(isSsg ? "renderTemplate" : "h");
229
+ return t3.callExpression(isSsg ? state.renderTemplate : state.h, args);
230
+ }
231
+ function createProps(props) {
232
+ const toAstNode = (value) => {
233
+ if (Array.isArray(value)) {
234
+ return t3.arrayExpression(value.map(toAstNode));
235
+ }
236
+ if (value && typeof value === "object" && !t3.isNode(value)) {
237
+ return createProps(value);
238
+ }
239
+ switch (typeof value) {
240
+ case "string":
241
+ return t3.stringLiteral(value);
242
+ case "number":
243
+ return t3.numericLiteral(value);
244
+ case "boolean":
245
+ return t3.booleanLiteral(value);
246
+ case "undefined":
247
+ return t3.tsUndefinedKeyword();
248
+ case void 0:
249
+ return t3.tsUndefinedKeyword();
250
+ case null:
251
+ return t3.nullLiteral();
252
+ default:
253
+ return value;
254
+ }
255
+ };
256
+ const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
257
+ const value = toAstNode(props[prop]);
258
+ return prop === "_$spread$" ? t3.spreadElement(value) : t3.objectProperty(t3.stringLiteral(prop), value);
259
+ });
260
+ return t3.objectExpression(result);
261
+ }
262
+ function transformJSXElement(path, result, isRoot = false) {
263
+ if (path.isJSXElement()) {
264
+ const tagName = getTagName(path.node);
265
+ const tagIsComponent = isComponent(tagName);
266
+ const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
267
+ const isSvg = svgTags.includes(tagName) && result.index === 1;
268
+ const props = getAttrProps(path);
269
+ if (tagIsComponent) {
270
+ if (isRoot) {
271
+ result.props = props;
272
+ const children = getChildren(path);
273
+ if (children.length > 0) {
274
+ const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
275
+ result.props.children = childrenGenerator;
276
+ }
277
+ } else {
278
+ transformJSX(path);
279
+ replaceChild(path.node, result);
280
+ }
281
+ } else {
282
+ if (isSvg) {
283
+ result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
284
+ }
285
+ addToTemplate(result, `<${tagName}`);
286
+ handleAttributes(props, result);
287
+ addToTemplate(result, isSelfClose ? "/>" : ">");
288
+ if (!isSelfClose) {
289
+ transformChildren(path, result);
290
+ if (hasSiblingElement(path)) {
291
+ addToTemplate(result, `</${tagName}>`);
292
+ }
293
+ }
294
+ }
295
+ } else {
296
+ result.index--;
297
+ transformChildren(path, result);
298
+ }
299
+ }
300
+ function transformChildren(path, result) {
301
+ const parentIndex = isSsg ? result.template.length : result.index;
302
+ path.get("children").reduce((pre, cur) => {
303
+ if (isValidChild(cur)) {
304
+ const lastChild = pre.at(-1);
305
+ if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
306
+ setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
307
+ } else {
308
+ pre.push(cur);
309
+ }
310
+ }
311
+ return pre;
312
+ }, []).forEach((child, i, arr) => {
313
+ result.parentIndex = parentIndex;
314
+ result.isLastChild = i === arr.length - 1;
315
+ transformChild(child, result);
316
+ });
317
+ }
318
+ function transformChild(child, result) {
319
+ result.index++;
320
+ if (child.isJSXElement() || child.isJSXFragment()) {
321
+ transformJSXElement(child, result, false);
322
+ } else if (child.isJSXExpressionContainer()) {
323
+ const expression = child.get("expression");
324
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
325
+ addToTemplate(result, String(expression.node.value));
326
+ } else if (expression.isExpression()) {
327
+ replaceChild(expression.node, result);
328
+ } else if (t3.isJSXEmptyExpression(expression.node)) {
329
+ } else {
330
+ throw new Error("Unsupported child type");
331
+ }
332
+ } else if (child.isJSXText()) {
333
+ addToTemplate(result, String(child.node.value));
334
+ } else {
335
+ throw new Error("Unsupported child type");
336
+ }
337
+ }
338
+ function getNodeText(path) {
339
+ if (path.isJSXText()) {
340
+ return path.node.value;
341
+ }
342
+ if (path.isJSXExpressionContainer()) {
343
+ const expression = path.get("expression");
344
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
345
+ return String(expression.node.value);
346
+ }
347
+ }
348
+ return "";
349
+ }
350
+ function handleAttributes(props, result) {
351
+ let klass = "";
352
+ let style = "";
353
+ for (const prop in props) {
354
+ let value = props[prop];
355
+ if (prop === "class" && typeof value === "string") {
356
+ klass += ` ${value}`;
357
+ delete props[prop];
358
+ continue;
359
+ }
360
+ if (prop === "style" && typeof value === "string") {
361
+ style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
362
+ delete props[prop];
363
+ continue;
364
+ }
365
+ if (value === true) {
366
+ addToTemplate(result, ` ${prop}`);
367
+ delete props[prop];
368
+ }
369
+ if (value === false) {
370
+ delete props[prop];
371
+ }
372
+ if (typeof value === "string" || typeof value === "number") {
373
+ addToTemplate(result, ` ${prop}="${value}"`);
374
+ delete props[prop];
375
+ }
376
+ if (t3.isConditionalExpression(value)) {
377
+ const { test, consequent, alternate } = value;
378
+ value = t3.arrowFunctionExpression([], t3.conditionalExpression(test, consequent, alternate));
379
+ props[prop] = value;
380
+ }
381
+ if (t3.isObjectExpression(value)) {
382
+ let hasConditional = false;
383
+ value.properties.forEach((property) => {
384
+ if (t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)) {
385
+ hasConditional = true;
386
+ }
387
+ });
388
+ if (hasConditional) {
389
+ value = t3.arrowFunctionExpression([], value);
390
+ props[prop] = value;
391
+ } else {
392
+ if (prop === "style") {
393
+ value.properties.forEach((property) => {
394
+ if (t3.isObjectProperty(property)) {
395
+ style += `${property.key.name}:${property.value.value};`;
396
+ }
397
+ });
398
+ delete props[prop];
399
+ }
400
+ }
401
+ }
402
+ }
403
+ if (Object.keys(props).length > 0) {
404
+ result.props[result.index] = props;
405
+ }
406
+ klass = klass.trim();
407
+ style = style.trim();
408
+ if (klass) {
409
+ addToTemplate(result, ` class="${klass}"`);
410
+ }
411
+ if (style) {
412
+ addToTemplate(result, ` style="${style}"`);
413
+ }
414
+ }
415
+ function replaceChild(node, result) {
416
+ var _a, _b, _c, _d, _e;
417
+ if (result.isLastChild) {
418
+ result.index--;
419
+ } else {
420
+ addToTemplate(result, "<!>");
421
+ }
422
+ (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
423
+ (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
424
+ result.props[result.parentIndex].children.push(
425
+ t3.arrayExpression([
426
+ t3.arrowFunctionExpression([], node),
427
+ result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
428
+ ])
429
+ );
430
+ }
431
+ function getChildren(path) {
432
+ return path.get("children").filter((child) => isValidChild(child)).map((child) => {
433
+ if (child.isJSXElement() || child.isJSXFragment()) {
434
+ transformJSX(child);
435
+ } else if (child.isJSXExpressionContainer()) {
436
+ child.replaceWith(child.get("expression"));
437
+ } else if (child.isJSXText()) {
438
+ child.replaceWith(t3.stringLiteral(child.node.value));
439
+ } else {
440
+ throw new Error("Unsupported child type");
441
+ }
442
+ return child.node;
443
+ });
444
+ }
445
+ function isValidChild(path) {
446
+ const regex = /^\s*$/;
447
+ if (path.isStringLiteral() || path.isJSXText()) {
448
+ return !regex.test(path.node.value);
449
+ }
450
+ return Object.keys(path.node).length > 0;
451
+ }
452
+ function getAttrProps(path) {
453
+ const props = {};
454
+ path.get("openingElement").get("attributes").forEach((attribute) => {
455
+ if (attribute.isJSXAttribute()) {
456
+ const name = getAttrName(attribute.node);
457
+ const value = attribute.get("value");
458
+ if (!value.node) {
459
+ props[name] = true;
460
+ } else if (value.isStringLiteral()) {
461
+ props[name] = value.node.value;
462
+ } else {
463
+ if (value.isJSXExpressionContainer()) {
464
+ const expression = value.get("expression");
465
+ if (expression.isStringLiteral()) {
466
+ props[name] = expression.node.value;
467
+ } else if (expression.isNumericLiteral()) {
468
+ props[name] = expression.node.value;
469
+ } else if (expression.isJSXElement() || expression.isJSXFragment()) {
470
+ transformJSX(expression);
471
+ props[name] = expression.node;
472
+ } else if (expression.isExpression()) {
473
+ if (/^key|ref|on.+$/.test(name)) {
474
+ props[name] = expression.node;
475
+ } else if (/^bind:.+/.test(name)) {
476
+ const value2 = path.scope.generateUidIdentifier("value");
477
+ const bindName = name.slice(5).toLocaleLowerCase();
478
+ props[bindName] = expression.node;
479
+ props[`update${capitalizeFirstLetter(bindName)}`] = t3.arrowFunctionExpression(
480
+ [value2],
481
+ t3.assignmentExpression("=", expression.node, value2)
482
+ );
483
+ } else {
484
+ if (expression.isConditionalExpression()) {
485
+ props[name] = t3.arrowFunctionExpression([], expression.node);
486
+ } else {
487
+ props[name] = expression.node;
488
+ }
489
+ }
490
+ }
491
+ } else if (value.isJSXElement() || value.isJSXFragment()) {
492
+ transformJSX(value);
493
+ props[name] = value.node;
494
+ }
495
+ }
496
+ } else if (attribute.isJSXSpreadAttribute()) {
497
+ props._$spread$ = attribute.get("argument").node;
498
+ } else {
499
+ throw new Error("Unsupported attribute type");
500
+ }
501
+ });
502
+ return props;
503
+ }
504
+
505
+ // src/signal/symbol.ts
506
+ import { types as t4 } from "@babel/core";
507
+ function replaceSymbol(path) {
508
+ const init = path.node.init;
509
+ const variableName = path.node.id.name;
510
+ if (t4.isObjectPattern(path.node.id) || t4.isArrayPattern(path.node.id)) {
511
+ return;
512
+ }
513
+ if (!isSymbolStart(path, variableName)) {
514
+ return;
515
+ }
516
+ if (init && (t4.isFunctionExpression(init) || t4.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
517
+ const newInit = t4.callExpression(t4.identifier(path.state.useComputed.name), init ? [init] : []);
518
+ imports.add("useComputed");
519
+ path.node.init = newInit;
520
+ } else {
521
+ const newInit = t4.callExpression(t4.identifier(path.state.useSignal.name), init ? [init] : []);
522
+ imports.add("useSignal");
523
+ path.node.init = newInit;
524
+ }
525
+ }
526
+ function symbolIdentifier(path) {
527
+ const parentPath = path.parentPath;
528
+ if (!parentPath || t4.isVariableDeclarator(parentPath) || t4.isImportSpecifier(parentPath) || t4.isObjectProperty(parentPath) || t4.isArrayPattern(parentPath) || t4.isObjectPattern(parentPath)) {
529
+ return;
530
+ }
531
+ const { node } = path;
532
+ if (isSymbolStart(path, node.name)) {
533
+ let currentPath = path;
534
+ while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
535
+ if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
536
+ return;
537
+ }
538
+ currentPath = currentPath.parentPath;
539
+ }
540
+ const newNode = t4.memberExpression(t4.identifier(node.name), t4.identifier("value"));
541
+ path.replaceWith(newNode);
542
+ }
543
+ }
544
+ function symbolObjectPattern(path) {
545
+ path.node.properties.forEach((property) => {
546
+ if (t4.isObjectProperty(property) && t4.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
547
+ const newKey = t4.identifier(property.key.name);
548
+ property.key = newKey;
549
+ }
550
+ });
551
+ }
552
+ function symbolArrayPattern(path) {
553
+ path.node.elements.forEach((element) => {
554
+ if (t4.isIdentifier(element) && element.name.startsWith("$")) {
555
+ const newElement = t4.identifier(element.name);
556
+ element.name = newElement.name;
557
+ } else if (t4.isObjectPattern(element)) {
558
+ element.properties.forEach((property) => {
559
+ if (t4.isObjectProperty(property) && t4.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
560
+ const newKey = t4.identifier(property.key.name);
561
+ property.key = newKey;
562
+ }
563
+ });
564
+ }
565
+ });
566
+ }
567
+
568
+ // src/signal/import.ts
569
+ import { types as t5 } from "@babel/core";
570
+ function replaceImportDeclaration(path) {
571
+ const imports2 = path.node.specifiers;
572
+ imports2.forEach((specifier) => {
573
+ const variableName = specifier.local.name;
574
+ if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
575
+ path.scope.rename(variableName, `${variableName}.value`);
576
+ specifier.local.name = `${variableName}`;
577
+ }
578
+ });
579
+ }
580
+ function isVariableUsedAsObject(path, variableName) {
581
+ const binding = path.scope.getBinding(variableName);
582
+ let isUsedObject = false;
583
+ if (!binding || !binding.referencePaths) {
584
+ return isUsedObject;
585
+ }
586
+ for (const referencePath of binding.referencePaths) {
587
+ if (t5.isMemberExpression(referencePath.parent)) {
588
+ const memberExprParent = referencePath.parent;
589
+ if (t5.isIdentifier(memberExprParent.object, { name: variableName })) {
590
+ const newMemberExpr = t5.memberExpression(
591
+ t5.memberExpression(memberExprParent.object, t5.identifier("value")),
592
+ memberExprParent.property
593
+ );
594
+ referencePath.parentPath.replaceWith(newMemberExpr);
595
+ isUsedObject = true;
596
+ }
597
+ }
598
+ }
599
+ return isUsedObject;
600
+ }
601
+
602
+ // src/signal/props.ts
603
+ import { types as t6 } from "@babel/core";
604
+ function replaceProps(path) {
605
+ var _a;
606
+ const state = path.state;
607
+ const firstParam = path.node.params[0];
608
+ if (!firstParam || !t6.isObjectPattern(firstParam)) {
609
+ return;
610
+ }
611
+ const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
612
+ if (!returnStatement) {
613
+ return;
614
+ }
615
+ const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
616
+ if (!t6.isJSXElement(returnValue)) {
617
+ return;
618
+ }
619
+ function replaceProperties(properties2, parentPath) {
620
+ properties2.forEach((property) => {
621
+ if (t6.isObjectProperty(property)) {
622
+ const keyName = property.key.name;
623
+ if (t6.isIdentifier(property.value)) {
624
+ const propertyName = property.value.name;
625
+ const newName = `${parentPath}${keyName}`;
626
+ path.scope.rename(propertyName, newName);
627
+ } else if (t6.isObjectPattern(property.value)) {
628
+ replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
629
+ }
630
+ }
631
+ });
632
+ }
633
+ const properties = firstParam.properties;
634
+ replaceProperties(
635
+ properties.filter((property) => !t6.isRestElement(property)),
636
+ "__props."
637
+ );
638
+ const notRestProperties = properties.filter((property) => !t6.isRestElement(property));
639
+ const notRestNames = notRestProperties.map(
640
+ (property) => property.key.name
641
+ );
642
+ if (notRestNames.some((name) => startsWith(name, "$"))) {
643
+ console.warn("props name can not start with $");
644
+ return;
645
+ }
646
+ const restElement = properties.find((property) => t6.isRestElement(property));
647
+ path.node.params[0] = t6.identifier("__props");
648
+ if (restElement) {
649
+ const restName = restElement.argument.name;
650
+ if (notRestProperties.length === 0) {
651
+ path.node.params[0] = t6.identifier(restName);
652
+ } else {
653
+ const restVariableDeclaration = t6.variableDeclaration("const", [
654
+ t6.variableDeclarator(
655
+ t6.identifier(restName),
656
+ t6.callExpression(state.useReactive, [
657
+ t6.identifier("__props"),
658
+ t6.arrayExpression(notRestNames.map((name) => t6.stringLiteral(name)))
659
+ ])
660
+ )
661
+ ]);
662
+ imports.add("useReactive");
663
+ path.node.body.body.unshift(restVariableDeclaration);
664
+ }
665
+ }
666
+ }
667
+
668
+ // src/index.ts
669
+ function src_default() {
670
+ return {
671
+ name: "babel-plugin-essor",
672
+ manipulateOptions({ filename }, parserOpts) {
673
+ if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
674
+ parserOpts.plugins.push("typescript");
675
+ }
676
+ parserOpts.plugins.push("jsx");
677
+ },
678
+ visitor: {
679
+ Program: transformProgram,
680
+ FunctionDeclaration: replaceProps,
681
+ ArrowFunctionExpression: replaceProps,
682
+ VariableDeclarator: replaceSymbol,
683
+ ImportDeclaration: replaceImportDeclaration,
684
+ Identifier: symbolIdentifier,
685
+ ObjectPattern: symbolObjectPattern,
686
+ ArrayPattern: symbolArrayPattern,
687
+ JSXElement: transformJSX,
688
+ JSXFragment: transformJSX
689
+ }
690
+ };
691
+ }
692
+ export {
693
+ src_default as default
694
+ };
695
+ /**
696
+ * @estjs/shared v0.0.10
697
+ * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
698
+ * @license MIT
699
+ **/
700
+ //# sourceMappingURL=index.js.map