@builder.io/mitosis 0.5.13 → 0.5.15

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,60 @@ 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')),
143
- },
144
- ],
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 programNode = (0, parsers_1.parseCodeToAst)(code);
155
+ if (!programNode)
156
+ continue;
157
+ (0, core_1.traverse)(programNode, {
158
+ Program(path) {
159
+ if (path.scope.hasBinding(target))
160
+ return;
161
+ const x = {
162
+ id: core_1.types.identifier(target),
163
+ init: core_1.types.identifier('PLACEHOLDER'),
164
+ };
165
+ path.scope.push(x);
166
+ path.scope.rename(target, 'state.$index');
167
+ path.traverse({
168
+ VariableDeclaration(p) {
169
+ if (p.node.declarations.length === 1 && p.node.declarations[0].id === x.id) {
170
+ p.remove();
171
+ }
172
+ },
173
+ });
174
+ },
145
175
  });
176
+ thing.bindings[key].code = (0, generator_1.default)(programNode).code;
146
177
  }
178
+ catch (error) {
179
+ console.error('Error processing function binding. Falling back to simple replacement.', error);
180
+ thing.bindings[key].code = replaceIndexNode(value.code);
181
+ }
182
+ }
183
+ else {
184
+ thing.bindings[key].code = replaceIndexNode(value.code);
147
185
  }
148
186
  }
149
187
  });
@@ -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
  });
@@ -1,4 +1,5 @@
1
1
  import * as babel from '@babel/core';
2
+ export declare function parseCodeToAst(code: string): babel.ParseResult | null;
2
3
  export declare function parseCode(code: string): babel.types.Statement[];
3
4
  export declare const isCodeBodyExpression: (body: babel.types.Statement[]) => boolean;
4
5
  /**
@@ -26,25 +26,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.isCodeBodyIdentifier = exports.isExpression = exports.isCodeBodyExpression = exports.parseCode = void 0;
29
+ exports.isCodeBodyIdentifier = exports.isExpression = exports.isCodeBodyExpression = exports.parseCode = exports.parseCodeToAst = void 0;
30
30
  const babel = __importStar(require("@babel/core"));
31
31
  const plugin_syntax_decorators_1 = __importDefault(require("@babel/plugin-syntax-decorators"));
32
32
  const plugin_syntax_typescript_1 = __importDefault(require("@babel/plugin-syntax-typescript"));
33
33
  const preset_typescript_1 = __importDefault(require("@babel/preset-typescript"));
34
- function parseCode(code) {
35
- const ast = babel.parse(code, {
34
+ function parseCodeToAst(code) {
35
+ return babel.parse(code, {
36
36
  presets: [[preset_typescript_1.default, { isTSX: true, allExtensions: true }]],
37
37
  plugins: [
38
38
  [plugin_syntax_typescript_1.default, { isTSX: true }],
39
39
  [plugin_syntax_decorators_1.default, { legacy: true }],
40
40
  ],
41
41
  });
42
- const body = babel.types.isFile(ast)
43
- ? ast.program.body
44
- : babel.types.isProgram(ast)
45
- ? ast.body
46
- : [];
47
- return body;
42
+ }
43
+ exports.parseCodeToAst = parseCodeToAst;
44
+ function parseCode(code) {
45
+ const ast = parseCodeToAst(code);
46
+ return babel.types.isFile(ast) ? ast.program.body : babel.types.isProgram(ast) ? ast.body : [];
48
47
  }
49
48
  exports.parseCode = parseCode;
50
49
  const isCodeBodyExpression = (body) => body.length == 1 &&
@@ -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.15",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {