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