@builder.io/mitosis 0.4.1 → 0.4.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.
@@ -4,6 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.componentToBuilder = exports.blockToBuilder = void 0;
7
+ const is_mitosis_node_1 = require("../helpers/is-mitosis-node");
8
+ const replace_identifiers_1 = require("../helpers/replace-identifiers");
9
+ const core_1 = require("@babel/core");
7
10
  const json5_1 = __importDefault(require("json5"));
8
11
  const lodash_1 = require("lodash");
9
12
  const legacy_1 = __importDefault(require("neotraverse/legacy"));
@@ -70,6 +73,27 @@ const componentMappers = {
70
73
  },
71
74
  For(_node, options) {
72
75
  var _a;
76
+ // rename `index` var to `state.$index`
77
+ const replaceIndex = (node) => {
78
+ (0, legacy_1.default)(node).forEach(function (thing) {
79
+ if ((0, is_mitosis_node_1.isMitosisNode)(thing)) {
80
+ for (const [key, value] of Object.entries(thing.bindings)) {
81
+ if (value === null || value === void 0 ? void 0 : value.code.includes('index')) {
82
+ thing.bindings[key].code = (0, replace_identifiers_1.replaceNodes)({
83
+ code: value.code,
84
+ nodeMaps: [
85
+ {
86
+ from: core_1.types.identifier('index'),
87
+ to: core_1.types.memberExpression(core_1.types.identifier('state'), core_1.types.identifier('$index')),
88
+ },
89
+ ],
90
+ });
91
+ }
92
+ }
93
+ }
94
+ });
95
+ return node;
96
+ };
73
97
  const node = _node;
74
98
  return el({
75
99
  component: {
@@ -81,7 +105,7 @@ const componentMappers = {
81
105
  },
82
106
  children: node.children
83
107
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
84
- .map((node) => (0, exports.blockToBuilder)(node, options)),
108
+ .map((node) => (0, exports.blockToBuilder)(replaceIndex(node), options)),
85
109
  }, options);
86
110
  },
87
111
  Show(node, options) {
@@ -43,7 +43,7 @@ const blockToMitosis = (json, toMitosisOptions = {}, component) => {
43
43
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
44
44
  const needsWrapper = json.children.length !== 1;
45
45
  return `<For each={${(_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code}}>
46
- {(${json.scope.forName}, index) =>
46
+ {(${json.scope.forName}, ${json.scope.indexName || 'index'}) =>
47
47
  ${needsWrapper ? '<>' : ''}
48
48
  ${json.children.map((child) => (0, exports.blockToMitosis)(child, options, component))}}
49
49
  ${needsWrapper ? '</>' : ''}
@@ -345,10 +345,9 @@ const componentMappers = {
345
345
  },
346
346
  scope: {
347
347
  forName: block.component.options.repeat.itemName,
348
- indexName: '$index',
349
348
  },
350
349
  meta: (0, exports.getMetaFromBlock)(block, options),
351
- children: (block.children || []).map((child) => (0, exports.builderElementToMitosisNode)(child, options)),
350
+ children: (block.children || []).map((child) => (0, exports.builderElementToMitosisNode)(updateBindings(child, 'state.$index', 'index'), options)),
352
351
  });
353
352
  },
354
353
  Text: (block, options) => {
@@ -480,10 +479,9 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
480
479
  },
481
480
  scope: {
482
481
  forName: ((_e = block.repeat) === null || _e === void 0 ? void 0 : _e.itemName) || 'item',
483
- indexName: '$index',
484
482
  },
485
483
  meta: (0, exports.getMetaFromBlock)(block, options),
486
- children: ((_f = block.children) === null || _f === void 0 ? void 0 : _f.map((child) => (0, exports.builderElementToMitosisNode)(child, options))) || [],
484
+ children: ((_f = block.children) === null || _f === void 0 ? void 0 : _f.map((child) => (0, exports.builderElementToMitosisNode)(updateBindings(child, 'state.$index', 'index'), options))) || [],
487
485
  });
488
486
  }
489
487
  else {
@@ -797,6 +795,27 @@ function convertExportDefaultToReturn(code) {
797
795
  }
798
796
  }
799
797
  exports.convertExportDefaultToReturn = convertExportDefaultToReturn;
798
+ const updateBindings = (node, from, to) => {
799
+ (0, legacy_1.default)(node).forEach(function (item) {
800
+ if ((0, exports.isBuilderElement)(item)) {
801
+ if (item.bindings) {
802
+ for (const [key, value] of Object.entries(item.bindings)) {
803
+ if (value === null || value === void 0 ? void 0 : value.includes(from)) {
804
+ item.bindings[key] = value.replaceAll(from, to);
805
+ }
806
+ }
807
+ }
808
+ if (item.actions) {
809
+ for (const [key, value] of Object.entries(item.actions)) {
810
+ if (value === null || value === void 0 ? void 0 : value.includes(from)) {
811
+ item.actions[key] = value.replaceAll(from, to);
812
+ }
813
+ }
814
+ }
815
+ }
816
+ });
817
+ return node;
818
+ };
800
819
  // TODO: maybe this should be part of the builder -> Mitosis part
801
820
  function extractSymbols(json) {
802
821
  var _a, _b, _c, _d;
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.4.1",
25
+ "version": "0.4.2",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {