what-compiler 0.6.0 → 0.6.2

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.
@@ -344,6 +344,7 @@ function whatBabelPlugin({ types: t }) {
344
344
  for (const attr of el.attributes) {
345
345
  if (t.isJSXSpreadAttribute(attr)) continue;
346
346
  const name = getAttrName(attr);
347
+ if (name === "key") continue;
347
348
  if (name.startsWith("on") || name.startsWith("bind:") || name.includes("|")) continue;
348
349
  let domName = name;
349
350
  if (name === "className") domName = "class";
@@ -503,6 +504,28 @@ function whatBabelPlugin({ types: t }) {
503
504
  continue;
504
505
  }
505
506
  const attrName = getAttrName(attr);
507
+ if (attrName === "key") continue;
508
+ if (attrName === "ref") {
509
+ const refExpr = getAttributeValue(attr.value);
510
+ statements.push(
511
+ t.expressionStatement(
512
+ t.conditionalExpression(
513
+ t.binaryExpression(
514
+ "===",
515
+ t.unaryExpression("typeof", refExpr),
516
+ t.stringLiteral("function")
517
+ ),
518
+ t.callExpression(t.cloneNode(refExpr), [t.identifier(elId)]),
519
+ t.assignmentExpression(
520
+ "=",
521
+ t.memberExpression(t.cloneNode(refExpr), t.identifier("current")),
522
+ t.identifier(elId)
523
+ )
524
+ )
525
+ )
526
+ );
527
+ continue;
528
+ }
506
529
  if (attrName.startsWith("on") && !attrName.includes("|")) {
507
530
  const event = attrName.slice(2).toLowerCase();
508
531
  const handler = getAttributeValue(attr.value);
@@ -653,6 +676,7 @@ function whatBabelPlugin({ types: t }) {
653
676
  }
654
677
  }
655
678
  function applyDynamicChildren(statements, elId, children, parentNode, state) {
679
+ const entries = [];
656
680
  let childIndex = 0;
657
681
  for (const child of children) {
658
682
  if (t.isJSXText(child)) {
@@ -662,8 +686,57 @@ function whatBabelPlugin({ types: t }) {
662
686
  }
663
687
  if (t.isJSXExpressionContainer(child)) {
664
688
  if (t.isJSXEmptyExpression(child.expression)) continue;
665
- const expr = child.expression;
666
- const marker = buildChildAccess(elId, childIndex);
689
+ entries.push({ type: "expression", child, childIndex });
690
+ childIndex++;
691
+ continue;
692
+ }
693
+ if (t.isJSXElement(child)) {
694
+ const childTag = child.openingElement.name.name;
695
+ if (isComponent(childTag) || childTag === "For" || childTag === "Show") {
696
+ entries.push({ type: "component", child, childIndex });
697
+ childIndex++;
698
+ } else {
699
+ const hasAnythingDynamic = child.openingElement.attributes.some(isDynamicAttr) || child.openingElement.attributes.some((a) => !t.isJSXSpreadAttribute(a) && getAttrName(a)?.startsWith("on")) || !child.children.every(isStaticChild);
700
+ entries.push({ type: "static", child, childIndex, hasAnythingDynamic });
701
+ childIndex++;
702
+ }
703
+ continue;
704
+ }
705
+ if (t.isJSXFragment(child)) {
706
+ entries.push({ type: "fragment", child });
707
+ }
708
+ }
709
+ const entriesNeedingRef = entries.filter(
710
+ (e) => e.type === "expression" || e.type === "component" || e.type === "static" && e.hasAnythingDynamic
711
+ );
712
+ const hasDynamicInsert = entries.some((e) => e.type === "expression" || e.type === "component");
713
+ const needsPreCapture = entriesNeedingRef.length >= 2 && hasDynamicInsert;
714
+ const markerVars = /* @__PURE__ */ new Map();
715
+ if (needsPreCapture) {
716
+ for (const entry of entriesNeedingRef) {
717
+ const varName = `_m$${entry.childIndex}`;
718
+ const markerVar = state.nextVarId();
719
+ markerVars.set(entry.childIndex, markerVar);
720
+ statements.push(
721
+ t.variableDeclaration("const", [
722
+ t.variableDeclarator(
723
+ t.identifier(markerVar),
724
+ buildChildAccess(elId, entry.childIndex)
725
+ )
726
+ ])
727
+ );
728
+ }
729
+ }
730
+ function getMarker(idx) {
731
+ if (markerVars.has(idx)) {
732
+ return t.identifier(markerVars.get(idx));
733
+ }
734
+ return buildChildAccess(elId, idx);
735
+ }
736
+ for (const entry of entries) {
737
+ if (entry.type === "expression") {
738
+ const expr = entry.child.expression;
739
+ const marker = getMarker(entry.childIndex);
667
740
  state.needsInsert = true;
668
741
  if (isPotentiallyReactive(expr, state.signalNames, state.importedIdentifiers)) {
669
742
  const insertCall = t.callExpression(t.identifier("_$insert"), [
@@ -691,46 +764,44 @@ function whatBabelPlugin({ types: t }) {
691
764
  )
692
765
  );
693
766
  }
694
- childIndex++;
695
767
  continue;
696
768
  }
697
- if (t.isJSXElement(child)) {
698
- const childTag = child.openingElement.name.name;
699
- if (isComponent(childTag) || childTag === "For" || childTag === "Show") {
700
- const transformed = transformElementFineGrained({ node: child }, state);
701
- const marker = buildChildAccess(elId, childIndex);
702
- state.needsInsert = true;
769
+ if (entry.type === "component") {
770
+ const transformed = transformElementFineGrained({ node: entry.child }, state);
771
+ const marker = getMarker(entry.childIndex);
772
+ state.needsInsert = true;
773
+ statements.push(
774
+ t.expressionStatement(
775
+ t.callExpression(t.identifier("_$insert"), [
776
+ t.identifier(elId),
777
+ transformed,
778
+ marker
779
+ ])
780
+ )
781
+ );
782
+ continue;
783
+ }
784
+ if (entry.type === "static" && entry.hasAnythingDynamic) {
785
+ let childElRef;
786
+ if (markerVars.has(entry.childIndex)) {
787
+ childElRef = markerVars.get(entry.childIndex);
788
+ } else {
789
+ childElRef = state.nextVarId();
703
790
  statements.push(
704
- t.expressionStatement(
705
- t.callExpression(t.identifier("_$insert"), [
706
- t.identifier(elId),
707
- transformed,
708
- marker
709
- ])
710
- )
791
+ t.variableDeclaration("const", [
792
+ t.variableDeclarator(
793
+ t.identifier(childElRef),
794
+ buildChildAccess(elId, entry.childIndex)
795
+ )
796
+ ])
711
797
  );
712
- childIndex++;
713
- } else {
714
- const hasAnythingDynamic = child.openingElement.attributes.some(isDynamicAttr) || child.openingElement.attributes.some((a) => !t.isJSXSpreadAttribute(a) && getAttrName(a)?.startsWith("on")) || !child.children.every(isStaticChild);
715
- if (hasAnythingDynamic) {
716
- const childElId = state.nextVarId();
717
- statements.push(
718
- t.variableDeclaration("const", [
719
- t.variableDeclarator(
720
- t.identifier(childElId),
721
- buildChildAccess(elId, childIndex)
722
- )
723
- ])
724
- );
725
- applyDynamicAttrs(statements, childElId, child.openingElement.attributes, state);
726
- applyDynamicChildren(statements, childElId, child.children, child, state);
727
- }
728
- childIndex++;
729
798
  }
799
+ applyDynamicAttrs(statements, childElRef, entry.child.openingElement.attributes, state);
800
+ applyDynamicChildren(statements, childElRef, entry.child.children, entry.child, state);
730
801
  continue;
731
802
  }
732
- if (t.isJSXFragment(child)) {
733
- for (const fChild of child.children) {
803
+ if (entry.type === "fragment") {
804
+ for (const fChild of entry.child.children) {
734
805
  if (t.isJSXExpressionContainer(fChild) && !t.isJSXEmptyExpression(fChild.expression)) {
735
806
  state.needsInsert = true;
736
807
  const expr = fChild.expression;
@@ -830,6 +901,7 @@ function whatBabelPlugin({ types: t }) {
830
901
  continue;
831
902
  }
832
903
  const attrName = getAttrName(attr);
904
+ if (attrName === "key") continue;
833
905
  if (isBindingAttribute(attrName)) {
834
906
  const bindProp = getBindingProperty(attrName);
835
907
  const signalExpr = attr.value.expression;