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.js CHANGED
@@ -48,8 +48,8 @@ var transformProgram = {
48
48
  state.opts = __spreadValues(__spreadValues({}, defaultOption), state.opts);
49
49
  path.state = {
50
50
  h: path.scope.generateUidIdentifier("h$"),
51
- renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
52
51
  template: path.scope.generateUidIdentifier("template$"),
52
+ ssg: path.scope.generateUidIdentifier("ssg$"),
53
53
  useSignal: path.scope.generateUidIdentifier("signal$"),
54
54
  useComputed: path.scope.generateUidIdentifier("computed$"),
55
55
  useReactive: path.scope.generateUidIdentifier("reactive$"),
@@ -140,7 +140,7 @@ function setNodeText(path, text) {
140
140
  }
141
141
  function isSymbolStart(path, name) {
142
142
  const state = path.state;
143
- const { symbol } = state.opts;
143
+ const { symbol } = (state == null ? void 0 : state.opts) || "$";
144
144
  return startsWith(name, symbol);
145
145
  }
146
146
 
@@ -185,9 +185,13 @@ var svgTags = [
185
185
 
186
186
  // src/jsx/index.ts
187
187
  var isSsg = false;
188
- function addToTemplate(result, content) {
188
+ function addToTemplate(result, content, join = false) {
189
189
  if (isSsg) {
190
- result.template.push(content);
190
+ if (join && result.template.length > 0) {
191
+ result.template[result.template.length - 1] += content;
192
+ } else {
193
+ result.template.push(content);
194
+ }
191
195
  } else {
192
196
  result.template += content;
193
197
  }
@@ -208,14 +212,11 @@ function transformJSX(path) {
208
212
  function createEssorNode(path, result) {
209
213
  var _a;
210
214
  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$");
215
+ const isComponent2 = path.isJSXElement() && isComponent(getTagName(path.node));
216
+ const tmpl = isComponent2 ? t3.identifier(getTagName(path.node)) : path.scope.generateUidIdentifier("_tmpl$");
217
+ if (!isComponent2) {
216
218
  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
+ state.tmplDeclaration.declarations.push(t3.variableDeclarator(tmpl, template));
219
220
  if (!isSsg) {
220
221
  imports.add("template");
221
222
  }
@@ -223,10 +224,11 @@ function createEssorNode(path, result) {
223
224
  const args = [tmpl, createProps(result.props)];
224
225
  const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
225
226
  if (key) {
226
- args.push(key);
227
+ args.push(t3.identifier(`${key}`));
227
228
  }
228
- imports.add(isSsg ? "renderTemplate" : "h");
229
- return t3.callExpression(isSsg ? state.renderTemplate : state.h, args);
229
+ const fnName = isSsg ? "ssg" : "h";
230
+ imports.add(fnName);
231
+ return t3.callExpression(state[fnName], args);
230
232
  }
231
233
  function createProps(props) {
232
234
  const toAstNode = (value) => {
@@ -265,7 +267,7 @@ function transformJSXElement(path, result, isRoot = false) {
265
267
  const tagIsComponent = isComponent(tagName);
266
268
  const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
267
269
  const isSvg = svgTags.includes(tagName) && result.index === 1;
268
- const props = getAttrProps(path);
270
+ const { props, hasExpression } = getAttrProps(path);
269
271
  if (tagIsComponent) {
270
272
  if (isRoot) {
271
273
  result.props = props;
@@ -282,12 +284,12 @@ function transformJSXElement(path, result, isRoot = false) {
282
284
  if (isSvg) {
283
285
  result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
284
286
  }
285
- addToTemplate(result, `<${tagName}`);
287
+ addToTemplate(result, `<${tagName}`, true);
286
288
  handleAttributes(props, result);
287
- addToTemplate(result, isSelfClose ? "/>" : ">");
289
+ addToTemplate(result, isSelfClose ? "/>" : ">", !hasExpression);
288
290
  if (!isSelfClose) {
289
291
  transformChildren(path, result);
290
- if (hasSiblingElement(path)) {
292
+ if (hasSiblingElement(path) || isSsg) {
291
293
  addToTemplate(result, `</${tagName}>`);
292
294
  }
293
295
  }
@@ -350,54 +352,25 @@ function getNodeText(path) {
350
352
  function handleAttributes(props, result) {
351
353
  let klass = "";
352
354
  let style = "";
353
- for (const prop in props) {
354
- let value = props[prop];
355
+ for (const [prop, value] of Object.entries(props)) {
355
356
  if (prop === "class" && typeof value === "string") {
356
357
  klass += ` ${value}`;
357
358
  delete props[prop];
358
- continue;
359
- }
360
- if (prop === "style" && typeof value === "string") {
359
+ } else if (prop === "style" && typeof value === "string") {
361
360
  style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
362
361
  delete props[prop];
363
- continue;
364
- }
365
- if (value === true) {
362
+ } else if (value === true) {
366
363
  addToTemplate(result, ` ${prop}`);
367
364
  delete props[prop];
368
- }
369
- if (value === false) {
365
+ } else if (value === false) {
370
366
  delete props[prop];
371
- }
372
- if (typeof value === "string" || typeof value === "number") {
367
+ } else if (typeof value === "string" || typeof value === "number") {
373
368
  addToTemplate(result, ` ${prop}="${value}"`);
374
369
  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
- }
370
+ } else if (t3.isConditionalExpression(value)) {
371
+ props[prop] = t3.arrowFunctionExpression([], value);
372
+ } else if (t3.isObjectExpression(value)) {
373
+ handleObjectExpression(prop, value, props, style);
401
374
  }
402
375
  }
403
376
  if (Object.keys(props).length > 0) {
@@ -412,6 +385,21 @@ function handleAttributes(props, result) {
412
385
  addToTemplate(result, ` style="${style}"`);
413
386
  }
414
387
  }
388
+ function handleObjectExpression(prop, value, props, style) {
389
+ const hasConditional = value.properties.some(
390
+ (property) => t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)
391
+ );
392
+ if (hasConditional) {
393
+ props[prop] = t3.arrowFunctionExpression([], value);
394
+ } else if (prop === "style") {
395
+ value.properties.forEach((property) => {
396
+ if (t3.isObjectProperty(property)) {
397
+ style += `${property.key.name}:${property.value.value};`;
398
+ }
399
+ });
400
+ delete props[prop];
401
+ }
402
+ }
415
403
  function replaceChild(node, result) {
416
404
  var _a, _b, _c, _d, _e;
417
405
  if (result.isLastChild) {
@@ -451,6 +439,7 @@ function isValidChild(path) {
451
439
  }
452
440
  function getAttrProps(path) {
453
441
  const props = {};
442
+ let hasExpression = false;
454
443
  path.get("openingElement").get("attributes").forEach((attribute) => {
455
444
  if (attribute.isJSXAttribute()) {
456
445
  const name = getAttrName(attribute.node);
@@ -470,6 +459,7 @@ function getAttrProps(path) {
470
459
  transformJSX(expression);
471
460
  props[name] = expression.node;
472
461
  } else if (expression.isExpression()) {
462
+ hasExpression = true;
473
463
  if (/^key|ref|on.+$/.test(name)) {
474
464
  props[name] = expression.node;
475
465
  } else if (/^bind:.+/.test(name)) {
@@ -495,72 +485,54 @@ function getAttrProps(path) {
495
485
  }
496
486
  } else if (attribute.isJSXSpreadAttribute()) {
497
487
  props._$spread$ = attribute.get("argument").node;
488
+ hasExpression = true;
498
489
  } else {
499
490
  throw new Error("Unsupported attribute type");
500
491
  }
501
492
  });
502
- return props;
493
+ return {
494
+ props,
495
+ hasExpression
496
+ };
503
497
  }
504
498
 
505
499
  // src/signal/symbol.ts
506
500
  import { types as t4 } from "@babel/core";
507
501
  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
- }
502
+ const { init, id } = path.node;
503
+ const variableName = id.name;
504
+ if (t4.isObjectPattern(id) || t4.isArrayPattern(id) || !isSymbolStart(path, variableName)) return;
505
+ const isComputed = init && (t4.isFunctionExpression(init) || t4.isArrowFunctionExpression(init)) && path.parent.kind === "const";
506
+ const hookName = isComputed ? "useComputed" : "useSignal";
507
+ const newInit = t4.callExpression(t4.identifier(path.state[hookName].name), init ? [init] : []);
508
+ imports.add(hookName);
509
+ path.node.init = newInit;
525
510
  }
526
511
  function symbolIdentifier(path) {
527
512
  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
- }
513
+ if (!shouldProcessIdentifier(parentPath)) return;
531
514
  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);
515
+ if (!isSymbolStart(path, node.name)) return;
516
+ if (!path.findParent((p) => p.isMemberExpression() && p.node.property.name === "value")) {
517
+ path.replaceWith(t4.memberExpression(t4.identifier(node.name), t4.identifier("value")));
542
518
  }
543
519
  }
520
+ function shouldProcessIdentifier(parentPath) {
521
+ return parentPath && !t4.isVariableDeclarator(parentPath) && !t4.isImportSpecifier(parentPath) && !t4.isObjectProperty(parentPath) && !t4.isArrayPattern(parentPath) && !t4.isObjectPattern(parentPath);
522
+ }
544
523
  function symbolObjectPattern(path) {
545
524
  path.node.properties.forEach((property) => {
546
525
  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;
526
+ property.key = t4.identifier(property.key.name);
549
527
  }
550
528
  });
551
529
  }
552
530
  function symbolArrayPattern(path) {
553
531
  path.node.elements.forEach((element) => {
554
532
  if (t4.isIdentifier(element) && element.name.startsWith("$")) {
555
- const newElement = t4.identifier(element.name);
556
- element.name = newElement.name;
533
+ element.name = t4.identifier(element.name).name;
557
534
  } 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
- });
535
+ symbolObjectPattern({ node: element });
564
536
  }
565
537
  });
566
538
  }
@@ -568,35 +540,29 @@ function symbolArrayPattern(path) {
568
540
  // src/signal/import.ts
569
541
  import { types as t5 } from "@babel/core";
570
542
  function replaceImportDeclaration(path) {
571
- const imports2 = path.node.specifiers;
572
- imports2.forEach((specifier) => {
543
+ path.node.specifiers.forEach((specifier) => {
573
544
  const variableName = specifier.local.name;
574
545
  if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
575
546
  path.scope.rename(variableName, `${variableName}.value`);
576
- specifier.local.name = `${variableName}`;
547
+ specifier.local.name = variableName;
577
548
  }
578
549
  });
579
550
  }
580
551
  function isVariableUsedAsObject(path, variableName) {
552
+ var _a;
581
553
  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) {
554
+ return ((_a = binding == null ? void 0 : binding.referencePaths) == null ? void 0 : _a.some((referencePath) => {
587
555
  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
556
+ const { object, property } = referencePath.parent;
557
+ if (t5.isIdentifier(object, { name: variableName })) {
558
+ referencePath.parentPath.replaceWith(
559
+ t5.memberExpression(t5.memberExpression(object, t5.identifier("value")), property)
593
560
  );
594
- referencePath.parentPath.replaceWith(newMemberExpr);
595
- isUsedObject = true;
561
+ return true;
596
562
  }
597
563
  }
598
- }
599
- return isUsedObject;
564
+ return false;
565
+ })) || false;
600
566
  }
601
567
 
602
568
  // src/signal/props.ts
@@ -605,65 +571,56 @@ function replaceProps(path) {
605
571
  var _a;
606
572
  const state = path.state;
607
573
  const firstParam = path.node.params[0];
608
- if (!firstParam || !t6.isObjectPattern(firstParam)) {
609
- return;
610
- }
574
+ if (!firstParam || !t6.isObjectPattern(firstParam)) return;
611
575
  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) {
576
+ if (!returnStatement || !t6.isJSXElement((_a = returnStatement.node) == null ? void 0 : _a.argument)) return;
577
+ const replaceProperties = (properties2, parentPath) => {
620
578
  properties2.forEach((property) => {
621
- if (t6.isObjectProperty(property)) {
579
+ if (t6.isObjectProperty(property) && t6.isIdentifier(property.key)) {
622
580
  const keyName = property.key.name;
623
581
  if (t6.isIdentifier(property.value)) {
624
- const propertyName = property.value.name;
625
- const newName = `${parentPath}${keyName}`;
626
- path.scope.rename(propertyName, newName);
582
+ path.scope.rename(property.value.name, `${parentPath}${keyName}`);
627
583
  } else if (t6.isObjectPattern(property.value)) {
628
584
  replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
629
585
  }
630
586
  }
631
587
  });
632
- }
588
+ };
633
589
  const properties = firstParam.properties;
634
- replaceProperties(
635
- properties.filter((property) => !t6.isRestElement(property)),
636
- "__props."
637
- );
638
590
  const notRestProperties = properties.filter((property) => !t6.isRestElement(property));
639
- const notRestNames = notRestProperties.map(
640
- (property) => property.key.name
641
- );
591
+ replaceProperties(notRestProperties, "__props.");
592
+ const notRestNames = notRestProperties.map((property) => property.key.name);
642
593
  if (notRestNames.some((name) => startsWith(name, "$"))) {
643
594
  console.warn("props name can not start with $");
644
595
  return;
645
596
  }
597
+ handleRestElement(path, state, properties, notRestNames);
598
+ }
599
+ function handleRestElement(path, state, properties, notRestNames) {
646
600
  const restElement = properties.find((property) => t6.isRestElement(property));
647
601
  path.node.params[0] = t6.identifier("__props");
648
602
  if (restElement) {
649
603
  const restName = restElement.argument.name;
650
- if (notRestProperties.length === 0) {
604
+ if (notRestNames.length === 0) {
651
605
  path.node.params[0] = t6.identifier(restName);
652
606
  } 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
- ]);
607
+ const restVariableDeclaration = createRestVariableDeclaration(state, restName, notRestNames);
662
608
  imports.add("useReactive");
663
609
  path.node.body.body.unshift(restVariableDeclaration);
664
610
  }
665
611
  }
666
612
  }
613
+ function createRestVariableDeclaration(state, restName, notRestNames) {
614
+ return t6.variableDeclaration("const", [
615
+ t6.variableDeclarator(
616
+ t6.identifier(restName),
617
+ t6.callExpression(state.useReactive, [
618
+ t6.identifier("__props"),
619
+ t6.arrayExpression(notRestNames.map((name) => t6.stringLiteral(name)))
620
+ ])
621
+ )
622
+ ]);
623
+ }
667
624
 
668
625
  // src/index.ts
669
626
  function src_default() {
@@ -693,7 +650,7 @@ export {
693
650
  src_default as default
694
651
  };
695
652
  /**
696
- * @estjs/shared v0.0.11
653
+ * @estjs/shared v0.0.12
697
654
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
698
655
  * @license MIT
699
656
  **/