@builder.io/mitosis 0.5.13 → 0.5.14

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.
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.componentToAngular = exports.blockToAngular = void 0;
7
7
  const html_tags_1 = require("../../constants/html_tags");
8
+ const bindings_1 = require("../../helpers/bindings");
8
9
  const dedent_1 = require("../../helpers/dedent");
9
10
  const fast_clone_1 = require("../../helpers/fast-clone");
10
11
  const get_child_components_1 = require("../../helpers/get-child-components");
@@ -364,10 +365,10 @@ const blockToAngular = ({ root, json, options = {}, blockOptions = {
364
365
  const spreadRefIndex = root.meta._spreadRefIndex || 0;
365
366
  refName = `elRef${spreadRefIndex}`;
366
367
  root.meta._spreadRefIndex = spreadRefIndex + 1;
367
- json.bindings['spreadRef'] = { code: refName, type: 'single' };
368
+ json.bindings['spreadRef'] = (0, bindings_1.createSingleBinding)({ code: refName });
368
369
  root.refs[refName] = { argument: '' };
369
370
  }
370
- json.bindings['spreadRef'] = { code: refName, type: 'single' };
371
+ json.bindings['spreadRef'] = (0, bindings_1.createSingleBinding)({ code: refName });
371
372
  root.refs[refName] = { argument: '' };
372
373
  root.meta.onViewInit = (root.meta.onViewInit || { code: '' });
373
374
  let spreadCode = '';
@@ -530,7 +531,7 @@ const handleProperties = (json, item, index) => {
530
531
  }
531
532
  const newBindingName = generateNewBindingName(index, item.name);
532
533
  json.state[newBindingName] = { code: '`' + `${item.properties[key]}` + '`', type: 'property' };
533
- item.bindings[key] = { code: `state.${newBindingName}`, type: 'single' };
534
+ item.bindings[key] = (0, bindings_1.createSingleBinding)({ code: `state.${newBindingName}` });
534
535
  delete item.properties[key];
535
536
  index++;
536
537
  }
@@ -13,12 +13,14 @@ const has_props_1 = require("../../helpers/has-props");
13
13
  const is_component_1 = require("../../helpers/is-component");
14
14
  const is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
15
15
  const is_upper_case_1 = require("../../helpers/is-upper-case");
16
+ const parsers_1 = require("../../helpers/parsers");
16
17
  const remove_surrounding_block_1 = require("../../helpers/remove-surrounding-block");
17
18
  const replace_identifiers_1 = require("../../helpers/replace-identifiers");
18
19
  const state_1 = require("../../helpers/state");
19
20
  const builder_1 = require("../../parsers/builder");
20
21
  const symbol_processor_1 = require("../../symbols/symbol-processor");
21
22
  const core_1 = require("@babel/core");
23
+ const generator_1 = __importDefault(require("@babel/generator"));
22
24
  const json5_1 = __importDefault(require("json5"));
23
25
  const lodash_1 = require("lodash");
24
26
  const legacy_1 = __importDefault(require("neotraverse/legacy"));
@@ -74,7 +76,6 @@ const componentMappers = {
74
76
  },
75
77
  PersonalizationContainer(node, options) {
76
78
  const block = (0, exports.blockToBuilder)(node, options, { skipMapper: true });
77
- // console.log('block', node);
78
79
  const variants = [];
79
80
  let defaultVariant = [];
80
81
  const validFakeNodeNames = [
@@ -127,23 +128,77 @@ const componentMappers = {
127
128
  For(_node, options) {
128
129
  var _a;
129
130
  const node = _node;
131
+ const replaceIndexNode = (str) => (0, replace_identifiers_1.replaceNodes)({
132
+ code: str,
133
+ nodeMaps: [
134
+ {
135
+ from: core_1.types.identifier(target),
136
+ to: core_1.types.memberExpression(core_1.types.identifier('state'), core_1.types.identifier('$index')),
137
+ },
138
+ ],
139
+ });
130
140
  // rename `index` var to `state.$index`
131
141
  const target = node.scope.indexName || 'index';
132
142
  const replaceIndex = (node) => {
133
143
  (0, legacy_1.default)(node).forEach(function (thing) {
134
- if ((0, is_mitosis_node_1.isMitosisNode)(thing)) {
135
- for (const [key, value] of Object.entries(thing.bindings)) {
136
- if (value === null || value === void 0 ? void 0 : value.code.includes(target)) {
137
- thing.bindings[key].code = (0, replace_identifiers_1.replaceNodes)({
138
- code: value.code,
139
- nodeMaps: [
140
- {
141
- from: core_1.types.identifier(target),
142
- to: core_1.types.memberExpression(core_1.types.identifier('state'), core_1.types.identifier('$index')),
144
+ if (!(0, is_mitosis_node_1.isMitosisNode)(thing))
145
+ return;
146
+ for (const [key, value] of Object.entries(thing.bindings)) {
147
+ if (!value)
148
+ continue;
149
+ if (!value.code.includes(target))
150
+ continue;
151
+ if (value.type === 'single' && value.bindingType === 'function') {
152
+ try {
153
+ const code = value.code;
154
+ const processedLines = [];
155
+ let stopProcessing = false;
156
+ const newLocal = (0, parsers_1.parseCode)(code);
157
+ const lines = newLocal.length === 1 && newLocal[0].type === 'BlockStatement'
158
+ ? newLocal[0].body
159
+ : newLocal;
160
+ const appendSemicolonIfNeeded = (code) => code.endsWith(';') ? code : code + ';';
161
+ for (const line of lines) {
162
+ const generatedLine = appendSemicolonIfNeeded((0, generator_1.default)(line).code);
163
+ if (stopProcessing) {
164
+ processedLines.push(generatedLine);
165
+ continue;
166
+ }
167
+ /**
168
+ * Check if this statement re-declares our `target` variable, i.e.
169
+ * if there is a variable in this function shadowing the `target` variable.
170
+ */
171
+ let hasTargetDeclaration = false;
172
+ core_1.types.traverse(line, {
173
+ enter(path) {
174
+ if (hasTargetDeclaration) {
175
+ return;
176
+ }
177
+ if (core_1.types.isVariableDeclarator(path) &&
178
+ core_1.types.isIdentifier(path.id) &&
179
+ path.id.name === target) {
180
+ hasTargetDeclaration = true;
181
+ }
143
182
  },
144
- ],
145
- });
183
+ });
184
+ if (hasTargetDeclaration) {
185
+ stopProcessing = true;
186
+ processedLines.push(generatedLine);
187
+ }
188
+ else {
189
+ // Replace identifiers in this statement
190
+ processedLines.push(appendSemicolonIfNeeded(replaceIndexNode(generatedLine)));
191
+ }
192
+ }
193
+ thing.bindings[key].code = '{' + processedLines.join('\n') + '}';
146
194
  }
195
+ catch (error) {
196
+ console.error('Error processing function binding. Falling back to simple replacement.', error);
197
+ thing.bindings[key].code = replaceIndexNode(value.code);
198
+ }
199
+ }
200
+ else {
201
+ thing.bindings[key].code = replaceIndexNode(value.code);
147
202
  }
148
203
  }
149
204
  });
@@ -168,13 +168,14 @@ const stringifyBinding = (node, options) => ([key, binding]) => {
168
168
  if (key === 'innerHTML' || !binding) {
169
169
  return '';
170
170
  }
171
- const { code, arguments: cusArgs = ['event'], type, async } = binding;
171
+ const { code, arguments: cusArgs = ['event'], type } = binding;
172
172
  const isValidHtmlTag = html_tags_1.VALID_HTML_TAGS.includes(node.name) || node.name === 'svelte:element';
173
173
  if (type === 'spread') {
174
174
  const spreadValue = key === 'props' ? '$$props' : code;
175
175
  return ` {...${spreadValue}} `;
176
176
  }
177
177
  else if (key.startsWith('on') && isValidHtmlTag) {
178
+ const { async } = binding;
178
179
  // handle html native on[event] props
179
180
  const event = key.replace('on', '').toLowerCase();
180
181
  // TODO: handle quotes in event handler values
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.blockToVue = void 0;
7
+ const bindings_1 = require("../../helpers/bindings");
7
8
  const filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
8
9
  const is_children_1 = __importDefault(require("../../helpers/is-children"));
9
10
  const is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
@@ -185,7 +186,7 @@ const blockToVue = (node, options, scope) => {
185
186
  }
186
187
  if (SPECIAL_HTML_TAGS.includes(node.name)) {
187
188
  // Vue doesn't allow style/script tags in templates, but does support them through dynamic components.
188
- node.bindings.is = { code: `'${node.name}'`, type: 'single' };
189
+ node.bindings.is = (0, bindings_1.createSingleBinding)({ code: `'${node.name}'` });
189
190
  node.name = 'component';
190
191
  }
191
192
  if (node.properties._text) {
@@ -1,2 +1,6 @@
1
1
  import { Binding } from '../types/mitosis-node';
2
- export declare const createSingleBinding: (args: Omit<Binding, 'type'>) => Binding;
2
+ type SingleBinding = Omit<Binding & {
3
+ type: 'single';
4
+ }, 'type'>;
5
+ export declare const createSingleBinding: (args: Omit<SingleBinding, 'bindingType'> & Partial<Pick<SingleBinding, 'bindingType'>>) => Binding;
6
+ export {};
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSingleBinding = void 0;
4
4
  const createSingleBinding = (args) => ({
5
5
  ...args,
6
+ bindingType: args.bindingType || 'expression',
6
7
  type: 'single',
7
8
  });
8
9
  exports.createSingleBinding = createSingleBinding;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processOnEventHooksPlugin = exports.getOnEventHooksForNode = exports.getOnEventHandlerName = void 0;
4
+ const bindings_1 = require("./bindings");
4
5
  const capitalize_1 = require("./capitalize");
5
6
  const traverse_nodes_1 = require("./traverse-nodes");
6
7
  const checkIsEventHandlerNode = (node, hook) => {
@@ -37,11 +38,11 @@ const processOnEventHooksPlugin = (args = {}) => () => ({
37
38
  type: 'method',
38
39
  };
39
40
  if (setBindings) {
40
- node.bindings[handlerName] = {
41
+ node.bindings[handlerName] = (0, bindings_1.createSingleBinding)({
41
42
  code: `state.${fnName}(${hook.eventArgName})`,
42
43
  arguments: [hook.eventArgName],
43
- type: 'single',
44
- };
44
+ bindingType: 'function',
45
+ });
45
46
  }
46
47
  });
47
48
  });
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processTargetBlocks = void 0;
4
4
  const use_target_1 = require("../../parsers/jsx/hooks/use-target");
5
+ const bindings_1 = require("../bindings");
5
6
  const process_code_1 = require("./process-code");
6
7
  const getBlockForTarget = ({ target, targetBlock, }) => {
7
8
  switch (target) {
@@ -20,10 +21,7 @@ const processTargetBlocks = (target) => {
20
21
  const property = node === null || node === void 0 ? void 0 : node.properties[key];
21
22
  if (!matches || !property)
22
23
  return code;
23
- node.bindings[key] = {
24
- code: '"' + property + '"',
25
- type: 'single',
26
- };
24
+ node.bindings[key] = (0, bindings_1.createSingleBinding)({ code: `"${property}"` });
27
25
  return () => {
28
26
  delete node.properties[key];
29
27
  };
@@ -364,16 +364,14 @@ const componentMappers = {
364
364
  });
365
365
  const queryOptions = variant.query;
366
366
  if (Array.isArray(queryOptions)) {
367
- variantNode.bindings.query = {
368
- type: 'single',
367
+ variantNode.bindings.query = (0, bindings_1.createSingleBinding)({
369
368
  code: JSON.stringify(queryOptions.map((q) => (0, lodash_1.omit)(q, '@type'))),
370
- };
369
+ });
371
370
  }
372
371
  else if (queryOptions) {
373
- variantNode.bindings.query = {
374
- type: 'single',
372
+ variantNode.bindings.query = (0, bindings_1.createSingleBinding)({
375
373
  code: JSON.stringify((0, lodash_1.omit)(queryOptions, '@type')),
376
- };
374
+ });
377
375
  }
378
376
  return variantNode;
379
377
  })) || [];
@@ -282,6 +282,7 @@ const jsxElementToJson = (node) => {
282
282
  code: (0, generator_1.default)(expression.body, { compact: true }).code,
283
283
  async: expression.async === true ? true : undefined,
284
284
  arguments: args.length ? args : undefined,
285
+ bindingType: 'function',
285
286
  });
286
287
  }
287
288
  else if (types.isJSXElement(expression)) {
@@ -1,15 +1,32 @@
1
1
  import { JSONObject } from './json';
2
2
  export type SpreadType = 'normal' | 'event-handlers';
3
+ export type BindingType = 'function' | 'expression';
3
4
  type BindingProperties = {
4
5
  type: 'spread';
5
6
  spreadType: SpreadType;
7
+ /**
8
+ * TODO: remove these once we've cleaned up the code that uses them.
9
+ * they don't need to be here since they only exist for functions
10
+ */
11
+ async?: boolean;
12
+ arguments?: string[];
13
+ } | {
14
+ type: 'single';
15
+ bindingType: Extract<BindingType, 'function'>;
16
+ async?: boolean;
17
+ arguments?: string[];
6
18
  } | {
7
19
  type: 'single';
20
+ bindingType: Extract<BindingType, 'expression'>;
21
+ /**
22
+ * TODO: remove these once we've cleaned up the code that uses them.
23
+ * they don't need to be here since they only exist for functions
24
+ */
25
+ async?: boolean;
26
+ arguments?: string[];
8
27
  };
9
28
  export type Binding = {
10
29
  code: string;
11
- async?: boolean;
12
- arguments?: string[];
13
30
  } & BindingProperties;
14
31
  export type BaseNode = {
15
32
  '@type': '@builder.io/mitosis/node';
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.5.13",
25
+ "version": "0.5.14",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {