babel-plugin-essor 0.0.11 → 0.0.12

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.cjs CHANGED
@@ -71,8 +71,8 @@ var transformProgram = {
71
71
  state.opts = __spreadValues(__spreadValues({}, defaultOption), state.opts);
72
72
  path.state = {
73
73
  h: path.scope.generateUidIdentifier("h$"),
74
- renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
75
74
  template: path.scope.generateUidIdentifier("template$"),
75
+ ssg: path.scope.generateUidIdentifier("ssg$"),
76
76
  useSignal: path.scope.generateUidIdentifier("signal$"),
77
77
  useComputed: path.scope.generateUidIdentifier("computed$"),
78
78
  useReactive: path.scope.generateUidIdentifier("reactive$"),
@@ -163,7 +163,7 @@ function setNodeText(path, text) {
163
163
  }
164
164
  function isSymbolStart(path, name) {
165
165
  const state = path.state;
166
- const { symbol } = state.opts;
166
+ const { symbol } = (state == null ? void 0 : state.opts) || "$";
167
167
  return startsWith(name, symbol);
168
168
  }
169
169
 
@@ -208,9 +208,13 @@ var svgTags = [
208
208
 
209
209
  // src/jsx/index.ts
210
210
  var isSsg = false;
211
- function addToTemplate(result, content) {
211
+ function addToTemplate(result, content, join = false) {
212
212
  if (isSsg) {
213
- result.template.push(content);
213
+ if (join && result.template.length > 0) {
214
+ result.template[result.template.length - 1] += content;
215
+ } else {
216
+ result.template.push(content);
217
+ }
214
218
  } else {
215
219
  result.template += content;
216
220
  }
@@ -231,14 +235,11 @@ function transformJSX(path) {
231
235
  function createEssorNode(path, result) {
232
236
  var _a;
233
237
  const state = path.state;
234
- let tmpl;
235
- if (path.isJSXElement() && isComponent(getTagName(path.node))) {
236
- tmpl = import_core3.types.identifier(getTagName(path.node));
237
- } else {
238
- tmpl = path.scope.generateUidIdentifier("_tmpl$");
238
+ const isComponent2 = path.isJSXElement() && isComponent(getTagName(path.node));
239
+ const tmpl = isComponent2 ? import_core3.types.identifier(getTagName(path.node)) : path.scope.generateUidIdentifier("_tmpl$");
240
+ if (!isComponent2) {
239
241
  const template = isSsg ? import_core3.types.arrayExpression(result.template.map(import_core3.types.stringLiteral)) : import_core3.types.callExpression(state.template, [import_core3.types.stringLiteral(result.template)]);
240
- const declarator = import_core3.types.variableDeclarator(tmpl, template);
241
- state.tmplDeclaration.declarations.push(declarator);
242
+ state.tmplDeclaration.declarations.push(import_core3.types.variableDeclarator(tmpl, template));
242
243
  if (!isSsg) {
243
244
  imports.add("template");
244
245
  }
@@ -246,10 +247,11 @@ function createEssorNode(path, result) {
246
247
  const args = [tmpl, createProps(result.props)];
247
248
  const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
248
249
  if (key) {
249
- args.push(key);
250
+ args.push(import_core3.types.identifier(`${key}`));
250
251
  }
251
- imports.add(isSsg ? "renderTemplate" : "h");
252
- return import_core3.types.callExpression(isSsg ? state.renderTemplate : state.h, args);
252
+ const fnName = isSsg ? "ssg" : "h";
253
+ imports.add(fnName);
254
+ return import_core3.types.callExpression(state[fnName], args);
253
255
  }
254
256
  function createProps(props) {
255
257
  const toAstNode = (value) => {
@@ -288,7 +290,7 @@ function transformJSXElement(path, result, isRoot = false) {
288
290
  const tagIsComponent = isComponent(tagName);
289
291
  const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
290
292
  const isSvg = svgTags.includes(tagName) && result.index === 1;
291
- const props = getAttrProps(path);
293
+ const { props, hasExpression } = getAttrProps(path);
292
294
  if (tagIsComponent) {
293
295
  if (isRoot) {
294
296
  result.props = props;
@@ -305,12 +307,12 @@ function transformJSXElement(path, result, isRoot = false) {
305
307
  if (isSvg) {
306
308
  result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
307
309
  }
308
- addToTemplate(result, `<${tagName}`);
310
+ addToTemplate(result, `<${tagName}`, true);
309
311
  handleAttributes(props, result);
310
- addToTemplate(result, isSelfClose ? "/>" : ">");
312
+ addToTemplate(result, isSelfClose ? "/>" : ">", !hasExpression);
311
313
  if (!isSelfClose) {
312
314
  transformChildren(path, result);
313
- if (hasSiblingElement(path)) {
315
+ if (hasSiblingElement(path) || isSsg) {
314
316
  addToTemplate(result, `</${tagName}>`);
315
317
  }
316
318
  }
@@ -373,54 +375,25 @@ function getNodeText(path) {
373
375
  function handleAttributes(props, result) {
374
376
  let klass = "";
375
377
  let style = "";
376
- for (const prop in props) {
377
- let value = props[prop];
378
+ for (const [prop, value] of Object.entries(props)) {
378
379
  if (prop === "class" && typeof value === "string") {
379
380
  klass += ` ${value}`;
380
381
  delete props[prop];
381
- continue;
382
- }
383
- if (prop === "style" && typeof value === "string") {
382
+ } else if (prop === "style" && typeof value === "string") {
384
383
  style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
385
384
  delete props[prop];
386
- continue;
387
- }
388
- if (value === true) {
385
+ } else if (value === true) {
389
386
  addToTemplate(result, ` ${prop}`);
390
387
  delete props[prop];
391
- }
392
- if (value === false) {
388
+ } else if (value === false) {
393
389
  delete props[prop];
394
- }
395
- if (typeof value === "string" || typeof value === "number") {
390
+ } else if (typeof value === "string" || typeof value === "number") {
396
391
  addToTemplate(result, ` ${prop}="${value}"`);
397
392
  delete props[prop];
398
- }
399
- if (import_core3.types.isConditionalExpression(value)) {
400
- const { test, consequent, alternate } = value;
401
- value = import_core3.types.arrowFunctionExpression([], import_core3.types.conditionalExpression(test, consequent, alternate));
402
- props[prop] = value;
403
- }
404
- if (import_core3.types.isObjectExpression(value)) {
405
- let hasConditional = false;
406
- value.properties.forEach((property) => {
407
- if (import_core3.types.isObjectProperty(property) && import_core3.types.isConditionalExpression(property.value)) {
408
- hasConditional = true;
409
- }
410
- });
411
- if (hasConditional) {
412
- value = import_core3.types.arrowFunctionExpression([], value);
413
- props[prop] = value;
414
- } else {
415
- if (prop === "style") {
416
- value.properties.forEach((property) => {
417
- if (import_core3.types.isObjectProperty(property)) {
418
- style += `${property.key.name}:${property.value.value};`;
419
- }
420
- });
421
- delete props[prop];
422
- }
423
- }
393
+ } else if (import_core3.types.isConditionalExpression(value)) {
394
+ props[prop] = import_core3.types.arrowFunctionExpression([], value);
395
+ } else if (import_core3.types.isObjectExpression(value)) {
396
+ handleObjectExpression(prop, value, props, style);
424
397
  }
425
398
  }
426
399
  if (Object.keys(props).length > 0) {
@@ -435,6 +408,21 @@ function handleAttributes(props, result) {
435
408
  addToTemplate(result, ` style="${style}"`);
436
409
  }
437
410
  }
411
+ function handleObjectExpression(prop, value, props, style) {
412
+ const hasConditional = value.properties.some(
413
+ (property) => import_core3.types.isObjectProperty(property) && import_core3.types.isConditionalExpression(property.value)
414
+ );
415
+ if (hasConditional) {
416
+ props[prop] = import_core3.types.arrowFunctionExpression([], value);
417
+ } else if (prop === "style") {
418
+ value.properties.forEach((property) => {
419
+ if (import_core3.types.isObjectProperty(property)) {
420
+ style += `${property.key.name}:${property.value.value};`;
421
+ }
422
+ });
423
+ delete props[prop];
424
+ }
425
+ }
438
426
  function replaceChild(node, result) {
439
427
  var _a, _b, _c, _d, _e;
440
428
  if (result.isLastChild) {
@@ -474,6 +462,7 @@ function isValidChild(path) {
474
462
  }
475
463
  function getAttrProps(path) {
476
464
  const props = {};
465
+ let hasExpression = false;
477
466
  path.get("openingElement").get("attributes").forEach((attribute) => {
478
467
  if (attribute.isJSXAttribute()) {
479
468
  const name = getAttrName(attribute.node);
@@ -493,6 +482,7 @@ function getAttrProps(path) {
493
482
  transformJSX(expression);
494
483
  props[name] = expression.node;
495
484
  } else if (expression.isExpression()) {
485
+ hasExpression = true;
496
486
  if (/^key|ref|on.+$/.test(name)) {
497
487
  props[name] = expression.node;
498
488
  } else if (/^bind:.+/.test(name)) {
@@ -518,72 +508,54 @@ function getAttrProps(path) {
518
508
  }
519
509
  } else if (attribute.isJSXSpreadAttribute()) {
520
510
  props._$spread$ = attribute.get("argument").node;
511
+ hasExpression = true;
521
512
  } else {
522
513
  throw new Error("Unsupported attribute type");
523
514
  }
524
515
  });
525
- return props;
516
+ return {
517
+ props,
518
+ hasExpression
519
+ };
526
520
  }
527
521
 
528
522
  // src/signal/symbol.ts
529
523
  var import_core4 = require("@babel/core");
530
524
  function replaceSymbol(path) {
531
- const init = path.node.init;
532
- const variableName = path.node.id.name;
533
- if (import_core4.types.isObjectPattern(path.node.id) || import_core4.types.isArrayPattern(path.node.id)) {
534
- return;
535
- }
536
- if (!isSymbolStart(path, variableName)) {
537
- return;
538
- }
539
- if (init && (import_core4.types.isFunctionExpression(init) || import_core4.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
540
- const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useComputed.name), init ? [init] : []);
541
- imports.add("useComputed");
542
- path.node.init = newInit;
543
- } else {
544
- const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useSignal.name), init ? [init] : []);
545
- imports.add("useSignal");
546
- path.node.init = newInit;
547
- }
525
+ const { init, id } = path.node;
526
+ const variableName = id.name;
527
+ if (import_core4.types.isObjectPattern(id) || import_core4.types.isArrayPattern(id) || !isSymbolStart(path, variableName)) return;
528
+ const isComputed = init && (import_core4.types.isFunctionExpression(init) || import_core4.types.isArrowFunctionExpression(init)) && path.parent.kind === "const";
529
+ const hookName = isComputed ? "useComputed" : "useSignal";
530
+ const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state[hookName].name), init ? [init] : []);
531
+ imports.add(hookName);
532
+ path.node.init = newInit;
548
533
  }
549
534
  function symbolIdentifier(path) {
550
535
  const parentPath = path.parentPath;
551
- if (!parentPath || import_core4.types.isVariableDeclarator(parentPath) || import_core4.types.isImportSpecifier(parentPath) || import_core4.types.isObjectProperty(parentPath) || import_core4.types.isArrayPattern(parentPath) || import_core4.types.isObjectPattern(parentPath)) {
552
- return;
553
- }
536
+ if (!shouldProcessIdentifier(parentPath)) return;
554
537
  const { node } = path;
555
- if (isSymbolStart(path, node.name)) {
556
- let currentPath = path;
557
- while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
558
- if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
559
- return;
560
- }
561
- currentPath = currentPath.parentPath;
562
- }
563
- const newNode = import_core4.types.memberExpression(import_core4.types.identifier(node.name), import_core4.types.identifier("value"));
564
- path.replaceWith(newNode);
538
+ if (!isSymbolStart(path, node.name)) return;
539
+ if (!path.findParent((p) => p.isMemberExpression() && p.node.property.name === "value")) {
540
+ path.replaceWith(import_core4.types.memberExpression(import_core4.types.identifier(node.name), import_core4.types.identifier("value")));
565
541
  }
566
542
  }
543
+ function shouldProcessIdentifier(parentPath) {
544
+ return parentPath && !import_core4.types.isVariableDeclarator(parentPath) && !import_core4.types.isImportSpecifier(parentPath) && !import_core4.types.isObjectProperty(parentPath) && !import_core4.types.isArrayPattern(parentPath) && !import_core4.types.isObjectPattern(parentPath);
545
+ }
567
546
  function symbolObjectPattern(path) {
568
547
  path.node.properties.forEach((property) => {
569
548
  if (import_core4.types.isObjectProperty(property) && import_core4.types.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
570
- const newKey = import_core4.types.identifier(property.key.name);
571
- property.key = newKey;
549
+ property.key = import_core4.types.identifier(property.key.name);
572
550
  }
573
551
  });
574
552
  }
575
553
  function symbolArrayPattern(path) {
576
554
  path.node.elements.forEach((element) => {
577
555
  if (import_core4.types.isIdentifier(element) && element.name.startsWith("$")) {
578
- const newElement = import_core4.types.identifier(element.name);
579
- element.name = newElement.name;
556
+ element.name = import_core4.types.identifier(element.name).name;
580
557
  } else if (import_core4.types.isObjectPattern(element)) {
581
- element.properties.forEach((property) => {
582
- if (import_core4.types.isObjectProperty(property) && import_core4.types.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
583
- const newKey = import_core4.types.identifier(property.key.name);
584
- property.key = newKey;
585
- }
586
- });
558
+ symbolObjectPattern({ node: element });
587
559
  }
588
560
  });
589
561
  }
@@ -591,35 +563,29 @@ function symbolArrayPattern(path) {
591
563
  // src/signal/import.ts
592
564
  var import_core5 = require("@babel/core");
593
565
  function replaceImportDeclaration(path) {
594
- const imports2 = path.node.specifiers;
595
- imports2.forEach((specifier) => {
566
+ path.node.specifiers.forEach((specifier) => {
596
567
  const variableName = specifier.local.name;
597
568
  if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
598
569
  path.scope.rename(variableName, `${variableName}.value`);
599
- specifier.local.name = `${variableName}`;
570
+ specifier.local.name = variableName;
600
571
  }
601
572
  });
602
573
  }
603
574
  function isVariableUsedAsObject(path, variableName) {
575
+ var _a;
604
576
  const binding = path.scope.getBinding(variableName);
605
- let isUsedObject = false;
606
- if (!binding || !binding.referencePaths) {
607
- return isUsedObject;
608
- }
609
- for (const referencePath of binding.referencePaths) {
577
+ return ((_a = binding == null ? void 0 : binding.referencePaths) == null ? void 0 : _a.some((referencePath) => {
610
578
  if (import_core5.types.isMemberExpression(referencePath.parent)) {
611
- const memberExprParent = referencePath.parent;
612
- if (import_core5.types.isIdentifier(memberExprParent.object, { name: variableName })) {
613
- const newMemberExpr = import_core5.types.memberExpression(
614
- import_core5.types.memberExpression(memberExprParent.object, import_core5.types.identifier("value")),
615
- memberExprParent.property
579
+ const { object, property } = referencePath.parent;
580
+ if (import_core5.types.isIdentifier(object, { name: variableName })) {
581
+ referencePath.parentPath.replaceWith(
582
+ import_core5.types.memberExpression(import_core5.types.memberExpression(object, import_core5.types.identifier("value")), property)
616
583
  );
617
- referencePath.parentPath.replaceWith(newMemberExpr);
618
- isUsedObject = true;
584
+ return true;
619
585
  }
620
586
  }
621
- }
622
- return isUsedObject;
587
+ return false;
588
+ })) || false;
623
589
  }
624
590
 
625
591
  // src/signal/props.ts
@@ -628,65 +594,56 @@ function replaceProps(path) {
628
594
  var _a;
629
595
  const state = path.state;
630
596
  const firstParam = path.node.params[0];
631
- if (!firstParam || !import_core6.types.isObjectPattern(firstParam)) {
632
- return;
633
- }
597
+ if (!firstParam || !import_core6.types.isObjectPattern(firstParam)) return;
634
598
  const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
635
- if (!returnStatement) {
636
- return;
637
- }
638
- const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
639
- if (!import_core6.types.isJSXElement(returnValue)) {
640
- return;
641
- }
642
- function replaceProperties(properties2, parentPath) {
599
+ if (!returnStatement || !import_core6.types.isJSXElement((_a = returnStatement.node) == null ? void 0 : _a.argument)) return;
600
+ const replaceProperties = (properties2, parentPath) => {
643
601
  properties2.forEach((property) => {
644
- if (import_core6.types.isObjectProperty(property)) {
602
+ if (import_core6.types.isObjectProperty(property) && import_core6.types.isIdentifier(property.key)) {
645
603
  const keyName = property.key.name;
646
604
  if (import_core6.types.isIdentifier(property.value)) {
647
- const propertyName = property.value.name;
648
- const newName = `${parentPath}${keyName}`;
649
- path.scope.rename(propertyName, newName);
605
+ path.scope.rename(property.value.name, `${parentPath}${keyName}`);
650
606
  } else if (import_core6.types.isObjectPattern(property.value)) {
651
607
  replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
652
608
  }
653
609
  }
654
610
  });
655
- }
611
+ };
656
612
  const properties = firstParam.properties;
657
- replaceProperties(
658
- properties.filter((property) => !import_core6.types.isRestElement(property)),
659
- "__props."
660
- );
661
613
  const notRestProperties = properties.filter((property) => !import_core6.types.isRestElement(property));
662
- const notRestNames = notRestProperties.map(
663
- (property) => property.key.name
664
- );
614
+ replaceProperties(notRestProperties, "__props.");
615
+ const notRestNames = notRestProperties.map((property) => property.key.name);
665
616
  if (notRestNames.some((name) => startsWith(name, "$"))) {
666
617
  console.warn("props name can not start with $");
667
618
  return;
668
619
  }
620
+ handleRestElement(path, state, properties, notRestNames);
621
+ }
622
+ function handleRestElement(path, state, properties, notRestNames) {
669
623
  const restElement = properties.find((property) => import_core6.types.isRestElement(property));
670
624
  path.node.params[0] = import_core6.types.identifier("__props");
671
625
  if (restElement) {
672
626
  const restName = restElement.argument.name;
673
- if (notRestProperties.length === 0) {
627
+ if (notRestNames.length === 0) {
674
628
  path.node.params[0] = import_core6.types.identifier(restName);
675
629
  } else {
676
- const restVariableDeclaration = import_core6.types.variableDeclaration("const", [
677
- import_core6.types.variableDeclarator(
678
- import_core6.types.identifier(restName),
679
- import_core6.types.callExpression(state.useReactive, [
680
- import_core6.types.identifier("__props"),
681
- import_core6.types.arrayExpression(notRestNames.map((name) => import_core6.types.stringLiteral(name)))
682
- ])
683
- )
684
- ]);
630
+ const restVariableDeclaration = createRestVariableDeclaration(state, restName, notRestNames);
685
631
  imports.add("useReactive");
686
632
  path.node.body.body.unshift(restVariableDeclaration);
687
633
  }
688
634
  }
689
635
  }
636
+ function createRestVariableDeclaration(state, restName, notRestNames) {
637
+ return import_core6.types.variableDeclaration("const", [
638
+ import_core6.types.variableDeclarator(
639
+ import_core6.types.identifier(restName),
640
+ import_core6.types.callExpression(state.useReactive, [
641
+ import_core6.types.identifier("__props"),
642
+ import_core6.types.arrayExpression(notRestNames.map((name) => import_core6.types.stringLiteral(name)))
643
+ ])
644
+ )
645
+ ]);
646
+ }
690
647
 
691
648
  // src/index.ts
692
649
  function src_default() {
@@ -713,7 +670,7 @@ function src_default() {
713
670
  };
714
671
  }
715
672
  /**
716
- * @estjs/shared v0.0.11
673
+ * @estjs/shared v0.0.12
717
674
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
718
675
  * @license MIT
719
676
  **/