@builder.io/mitosis 0.0.79 → 0.0.81

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.
@@ -1,2 +1,4 @@
1
1
  import { MitosisComponent } from '../../types/mitosis-component';
2
2
  export declare const hasContext: (component: MitosisComponent) => boolean;
3
+ export declare const hasSetContext: (component: MitosisComponent) => boolean;
4
+ export declare const hasGetContext: (component: MitosisComponent) => boolean;
@@ -1,7 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasContext = void 0;
3
+ exports.hasGetContext = exports.hasSetContext = exports.hasContext = void 0;
4
4
  var hasContext = function (component) {
5
- return Boolean(Object.keys(component.context.get).length || Object.keys(component.context.set).length);
5
+ return (0, exports.hasSetContext)(component) || (0, exports.hasGetContext)(component);
6
6
  };
7
7
  exports.hasContext = hasContext;
8
+ var hasSetContext = function (component) {
9
+ return Object.keys(component.context.set).length > 0;
10
+ };
11
+ exports.hasSetContext = hasSetContext;
12
+ var hasGetContext = function (component) {
13
+ return Object.keys(component.context.get).length > 0;
14
+ };
15
+ exports.hasGetContext = hasGetContext;
@@ -203,7 +203,7 @@ function emitUseStyles(file, component) {
203
203
  }
204
204
  function emitStyles(file, css) {
205
205
  if (css) {
206
- file.exportConst('STYLES', '`' + css.replace(/`/g, '\\`') + '`');
206
+ file.exportConst('STYLES', '`\n' + css.replace(/`/g, '\\`') + '`\n');
207
207
  }
208
208
  }
209
209
  /**
@@ -244,7 +244,7 @@ var getRefsString = function (json, refs, options) {
244
244
  // domRefs must have null argument
245
245
  var argument = ((_b = json['refs'][ref]) === null || _b === void 0 ? void 0 : _b.argument) || (domRefs.has(ref) ? 'null' : '');
246
246
  hasStateArgument = /state\./.test(argument);
247
- code += "\nconst ".concat(ref, " = useRef").concat(typeParameter ? "<".concat(typeParameter, ">") : '', "(").concat((0, state_2.processHookCode)({
247
+ code += "\nconst ".concat(ref, " = useRef").concat(typeParameter && options.typescript ? "<".concat(typeParameter, ">") : '', "(").concat((0, state_2.processHookCode)({
248
248
  str: argument,
249
249
  options: options,
250
250
  }), ");");
@@ -426,7 +426,8 @@ var _componentToReact = function (json, options, isSubComponent) {
426
426
  isRootSpecialNode(json);
427
427
  var _o = getRefsString(json, allRefs, options), hasStateArgument = _o[0], refsString = _o[1];
428
428
  var nativeStyles = stylesType === 'react-native' && componentHasStyles && (0, react_native_1.collectReactNativeStyles)(json);
429
- var propsArgs = "props: ".concat(json.propsTypeRef || 'any');
429
+ var propType = json.propsTypeRef || 'any';
430
+ var propsArgs = "props".concat(options.typescript ? ":".concat(propType) : '');
430
431
  var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n "])), options.preact
431
432
  ? "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n "
432
433
  : options.type !== 'native'
@@ -437,10 +438,12 @@ var _componentToReact = function (json, options, isSubComponent) {
437
438
  ? "/** @jsx jsx */\n import { jsx } from '@emotion/react'".trim()
438
439
  : '', hasState && stateType === 'valtio' ? "import { useLocalProxy } from 'valtio/utils';" : '', hasState && stateType === 'solid' ? "import { useMutable } from 'react-solid-state';" : '', stateType === 'mobx' && hasState
439
440
  ? "import { useLocalObservable, observer } from 'mobx-react-lite';"
440
- : '', json.types ? json.types.join('\n') : '', (0, render_imports_1.renderPreComponent)({
441
+ : '', json.types && options.typescript ? json.types.join('\n') : '', (0, render_imports_1.renderPreComponent)({
441
442
  component: json,
442
443
  target: options.type === 'native' ? 'reactNative' : 'react',
443
- }), stateType === 'mobx' && isForwardRef ? "const ".concat(json.name || 'MyComponent', " = ") : "", isSubComponent || stateType === 'mobx' ? '' : 'export default ', isForwardRef ? "forwardRef".concat(forwardRefType ? "<".concat(forwardRefType, ">") : '', "(") : '', json.name || 'MyComponent', propsArgs, isForwardRef ? ", ".concat(options.forwardRef) : '', options.contextType === 'prop-drill'
444
+ }), stateType === 'mobx' && isForwardRef ? "const ".concat(json.name || 'MyComponent', " = ") : "", isSubComponent || stateType === 'mobx' ? '' : 'export default ', isForwardRef
445
+ ? "forwardRef".concat(forwardRefType && options.typescript ? "<".concat(forwardRefType, ">") : '', "(")
446
+ : '', json.name || 'MyComponent', propsArgs, isForwardRef ? ", ".concat(options.forwardRef) : '', options.contextType === 'prop-drill'
444
447
  ? "const ".concat(exports.contextPropDrillingKey, " = { ...props['").concat(exports.contextPropDrillingKey, "'] };")
445
448
  : '', hasStateArgument ? '' : refsString, hasState
446
449
  ? stateType === 'mobx'
@@ -66,7 +66,6 @@ var traverse_1 = __importDefault(require("traverse"));
66
66
  var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
67
67
  var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
68
68
  var create_mitosis_node_1 = require("../../helpers/create-mitosis-node");
69
- var context_1 = require("../helpers/context");
70
69
  var babel_transform_1 = require("../../helpers/babel-transform");
71
70
  var core_1 = require("@babel/core");
72
71
  var lodash_1 = require("lodash");
@@ -80,12 +79,13 @@ var S = __importStar(require("fp-ts/string"));
80
79
  var helpers_2 = require("./state/helpers");
81
80
  var merge_options_1 = require("../../helpers/merge-options");
82
81
  var process_code_1 = require("../../helpers/plugins/process-code");
82
+ var context_1 = require("../helpers/context");
83
83
  // Transform <foo.bar key="value" /> to <component :is="foo.bar" key="value" />
84
84
  function processDynamicComponents(json, options) {
85
85
  var found = false;
86
86
  (0, traverse_1.default)(json).forEach(function (node) {
87
87
  if ((0, is_mitosis_node_1.isMitosisNode)(node)) {
88
- if (node.name.includes('.')) {
88
+ if (node.name.includes('.') && !node.name.endsWith('.Provider')) {
89
89
  node.bindings.component = { code: node.name };
90
90
  node.name = 'Dynamic';
91
91
  found = true;
@@ -317,11 +317,10 @@ var componentToSolid = function (passedOptions) {
317
317
  });
318
318
  var state = (0, state_1.getState)({ json: json, options: options });
319
319
  var componentsUsed = (0, get_components_used_1.getComponentsUsed)(json);
320
- var componentHasContext = (0, context_1.hasContext)(json);
321
320
  var hasShowComponent = componentsUsed.has('Show');
322
321
  var hasForComponent = componentsUsed.has('For');
323
322
  var solidJSImports = (0, Array_1.uniq)(S.Eq)(__spreadArray(__spreadArray([
324
- componentHasContext ? 'useContext' : undefined,
323
+ (0, context_1.hasGetContext)(json) ? 'useContext' : undefined,
325
324
  hasShowComponent ? 'Show' : undefined,
326
325
  hasForComponent ? 'For' : undefined,
327
326
  ((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? 'onMount' : undefined
@@ -106,7 +106,7 @@ var DEFAULT_OPTIONS = {
106
106
  };
107
107
  var componentToSvelte = function (userProvidedOptions) {
108
108
  return function (_a) {
109
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
109
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
110
110
  var component = _a.component;
111
111
  var options = (0, merge_options_1.mergeOptions)(DEFAULT_OPTIONS, userProvidedOptions);
112
112
  options.plugins = __spreadArray(__spreadArray([], (options.plugins || []), true), [
@@ -171,14 +171,17 @@ var componentToSvelte = function (userProvidedOptions) {
171
171
  if ((_d = (_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) === null || _d === void 0 ? void 0 : _d.length) {
172
172
  svelteImports.push('onMount');
173
173
  }
174
- if ((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length) {
174
+ if ((_f = (_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.filter(function (x) { return !x.deps; })) === null || _f === void 0 ? void 0 : _f.length) {
175
175
  svelteImports.push('afterUpdate');
176
176
  }
177
- if ((_g = (_f = json.hooks.onUnMount) === null || _f === void 0 ? void 0 : _f.code) === null || _g === void 0 ? void 0 : _g.length) {
177
+ if ((_h = (_g = json.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code) === null || _h === void 0 ? void 0 : _h.length) {
178
178
  svelteImports.push('onDestroy');
179
179
  }
180
- if ((0, context_1.hasContext)(component)) {
181
- svelteImports.push('getContext', 'setContext');
180
+ if ((0, context_1.hasGetContext)(component)) {
181
+ svelteImports.push('getContext');
182
+ }
183
+ if ((0, context_1.hasSetContext)(component)) {
184
+ svelteImports.push('setContext');
182
185
  }
183
186
  str += (0, dedent_1.default)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n <script ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n \n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n\n ", "\n "], ["\n <script ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n "
184
187
  // https://svelte.dev/repl/bd9b56891f04414982517bbd10c52c82?version=3.31.0
@@ -207,14 +210,14 @@ var componentToSvelte = function (userProvidedOptions) {
207
210
  ? dataString.length < 4
208
211
  ? ''
209
212
  : "let state = onChange(".concat(dataString, ", () => state = state)")
210
- : dataString, (_j = (_h = json.hooks.onInit) === null || _h === void 0 ? void 0 : _h.code) !== null && _j !== void 0 ? _j : '', !((_k = json.hooks.onMount) === null || _k === void 0 ? void 0 : _k.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " });"), ((_l = json.hooks.onUpdate) === null || _l === void 0 ? void 0 : _l.map(function (_a, index) {
213
+ : dataString, (_k = (_j = json.hooks.onInit) === null || _j === void 0 ? void 0 : _j.code) !== null && _k !== void 0 ? _k : '', !((_l = json.hooks.onMount) === null || _l === void 0 ? void 0 : _l.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " });"), ((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.map(function (_a, index) {
211
214
  var code = _a.code, deps = _a.deps;
212
215
  if (!deps) {
213
216
  return "afterUpdate(() => { ".concat(code, " });");
214
217
  }
215
218
  var fnName = "onUpdateFn_".concat(index);
216
219
  return "\n function ".concat(fnName, "() {\n ").concat(code, "\n }\n $: ").concat(fnName, "(...").concat(deps, ")\n ");
217
- }).join(';')) || '', !((_m = json.hooks.onUnMount) === null || _m === void 0 ? void 0 : _m.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
220
+ }).join(';')) || '', !((_o = json.hooks.onUnMount) === null || _o === void 0 ? void 0 : _o.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
218
221
  .map(function (item) {
219
222
  return (0, blocks_1.blockToSvelte)({
220
223
  json: item,
@@ -245,6 +248,7 @@ var componentToSvelte = function (userProvidedOptions) {
245
248
  console.warn({ string: str }, err);
246
249
  }
247
250
  }
251
+ str = str.replace(/<script>\n<\/script>/g, '').trim();
248
252
  str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
249
253
  return str;
250
254
  };
@@ -14,6 +14,7 @@ var lodash_1 = require("lodash");
14
14
  var get_state_object_string_1 = require("../../helpers/get-state-object-string");
15
15
  var helpers_1 = require("./helpers");
16
16
  var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
17
+ var helpers_2 = require("./helpers");
17
18
  var getCompositionPropDefinition = function (_a) {
18
19
  var options = _a.options, component = _a.component, props = _a.props;
19
20
  var str = 'const props = ';
@@ -76,7 +77,11 @@ function generateCompositionApiScript(component, options, template, props, onUpd
76
77
  var computedCode = "const ".concat(key, " = computed(").concat(getterAsFunction, ")");
77
78
  return computedCode;
78
79
  }).join('\n')) || '', (onUpdateWithoutDeps === null || onUpdateWithoutDeps === void 0 ? void 0 : onUpdateWithoutDeps.map(function (hook) { return "onUpdated(() => ".concat(hook.code, ")"); }).join('\n')) || '', (onUpdateWithDeps === null || onUpdateWithDeps === void 0 ? void 0 : onUpdateWithDeps.map(function (hook) {
79
- return "watch(() => ".concat(hook.deps, ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, " })");
80
+ return "watch(() => ".concat((0, helpers_2.processBinding)({
81
+ code: hook.deps || '',
82
+ options: options,
83
+ json: component,
84
+ }), ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, " }, {immediate: true})");
80
85
  }).join('\n')) || '', methods !== null && methods !== void 0 ? methods : '');
81
86
  return str;
82
87
  }
@@ -24,7 +24,7 @@ var helpers_1 = require("./helpers");
24
24
  function getContextInjectString(component, options) {
25
25
  var str = '{';
26
26
  for (var key in component.context.get) {
27
- str += "\n ".concat(key, ": \"").concat((0, helpers_1.encodeQuotes)(component.context.get[key].name), "\",\n ");
27
+ str += "\n ".concat(key, ": ").concat((0, helpers_1.encodeQuotes)(component.context.get[key].name), ",\n ");
28
28
  }
29
29
  str += '}';
30
30
  return str;
@@ -140,7 +140,7 @@ function generateOptionsApiScript(component, options, path, template, props, onU
140
140
  : '', "\n ").concat(onUpdateWithDeps.length
141
141
  ? "watch: {\n ".concat(onUpdateWithDeps
142
142
  .map(function (hook, index) {
143
- return "".concat((0, helpers_1.getOnUpdateHookName)(index), "() {\n ").concat(hook.code, "\n }\n ");
143
+ return "".concat((0, helpers_1.getOnUpdateHookName)(index), ": { handler() { ").concat(hook.code, " }, immediate: true }");
144
144
  })
145
145
  .join(','), "\n },")
146
146
  : '', "\n ").concat(component.hooks.onUnMount
@@ -175,7 +175,9 @@ var componentToVue = function (userOptions) {
175
175
  (0, lodash_1.size)(Object.keys(component.state).filter(function (key) { var _a; return ((_a = component.state[key]) === null || _a === void 0 ? void 0 : _a.type) === 'property'; })) && vueImports.push('ref');
176
176
  }
177
177
  var tsLangAttribute = options.typescript ? "lang='ts'" : '';
178
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n <template>\n ", "\n </template>\n\n\n <script ", " ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n "], ["\n <template>\n ", "\n </template>\n\n\n <script ", " ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n "])), template, options.api === 'composition' ? 'setup' : '', tsLangAttribute, vueImports.length ? "import { ".concat((0, lodash_1.uniq)(vueImports).sort().join(', '), " } from \"vue\"") : '', (options.typescript && ((_h = component.types) === null || _h === void 0 ? void 0 : _h.join('\n'))) || '', (0, render_imports_1.renderPreComponent)({
178
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n\n\n <script ", " ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n "], ["\n ", "\n\n\n <script ", " ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n "])), template.trim().length > 0
179
+ ? "<template>\n ".concat(template, "\n </template>")
180
+ : '', options.api === 'composition' ? 'setup' : '', tsLangAttribute, vueImports.length ? "import { ".concat((0, lodash_1.uniq)(vueImports).sort().join(', '), " } from \"vue\"") : '', (options.typescript && ((_h = component.types) === null || _h === void 0 ? void 0 : _h.join('\n'))) || '', (0, render_imports_1.renderPreComponent)({
179
181
  component: component,
180
182
  target: 'vue',
181
183
  asyncComponentImports: options.asyncComponentImports,
@@ -205,8 +207,9 @@ var componentToVue = function (userOptions) {
205
207
  str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
206
208
  for (var _i = 0, removePatterns_1 = removePatterns; _i < removePatterns_1.length; _i++) {
207
209
  var pattern = removePatterns_1[_i];
208
- str = str.replace(pattern, '');
210
+ str = str.replace(pattern, '').trim();
209
211
  }
212
+ str = str.replace(/<script(.*)>\n?<\/script>/g, '').trim();
210
213
  return str;
211
214
  };
212
215
  };
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processHttpRequests = void 0;
4
4
  function processHttpRequests(json) {
5
- var _a, _b;
6
- var httpRequests = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.httpRequests;
7
- var onMount = ((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? json.hooks.onMount : { code: '' };
5
+ var _a, _b, _c;
6
+ var httpRequests = (_b = (_a = json === null || json === void 0 ? void 0 : json.meta) === null || _a === void 0 ? void 0 : _a.useMetadata) === null || _b === void 0 ? void 0 : _b.httpRequests;
7
+ var onMount = ((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) ? json.hooks.onMount : { code: '' };
8
8
  if (httpRequests) {
9
9
  for (var key in httpRequests) {
10
10
  if (!json.state[key]) {
@@ -71,7 +71,7 @@ var classStyleMapToCss = function (map) {
71
71
  var str = '';
72
72
  for (var key in map) {
73
73
  var styles = (0, helpers_1.getStylesOnly)(map[key]);
74
- str += ".".concat(key, " { ").concat((0, helpers_1.styleMapToCss)(styles), " }");
74
+ str += ".".concat(key, " {\n").concat((0, helpers_1.styleMapToCss)(styles), "\n}");
75
75
  var nestedSelectors = (0, helpers_1.getNestedSelectors)(map[key]);
76
76
  for (var nestedSelector in nestedSelectors) {
77
77
  var value = nestedSelectors[nestedSelector];
@@ -82,7 +82,7 @@ var classStyleMapToCss = function (map) {
82
82
  var useSelector = nestedSelector.includes('&')
83
83
  ? nestedSelector.replace(/&/g, ".".concat(key))
84
84
  : ".".concat(key, " ").concat(nestedSelector);
85
- str += "".concat(useSelector, " { ").concat((0, helpers_1.styleMapToCss)(value), " }");
85
+ str += "".concat(useSelector, " {\n").concat((0, helpers_1.styleMapToCss)(value), "\n}");
86
86
  }
87
87
  }
88
88
  }
@@ -69,16 +69,15 @@ var parseCssObject = function (css) {
69
69
  };
70
70
  exports.parseCssObject = parseCssObject;
71
71
  var styleMapToCss = function (map) {
72
- var str = '';
73
- for (var key in map) {
74
- var value = map[key];
75
- if (typeof value === 'string') {
76
- str += "\n".concat((0, dash_case_1.dashCase)(key), ": ").concat(value, ";");
77
- }
78
- else {
79
- // TODO: do nothing
80
- }
81
- }
82
- return str;
72
+ return Object.entries(map)
73
+ .filter(function (_a) {
74
+ var key = _a[0], value = _a[1];
75
+ return typeof value === 'string';
76
+ })
77
+ .map(function (_a) {
78
+ var key = _a[0], value = _a[1];
79
+ return " ".concat((0, dash_case_1.dashCase)(key), ": ").concat(value, ";");
80
+ })
81
+ .join('\n');
83
82
  };
84
83
  exports.styleMapToCss = styleMapToCss;
@@ -1,9 +1,14 @@
1
+ import type { StateValue } from '../../../types/mitosis-component';
1
2
  import type { SveltosisComponent } from '../types';
2
- export declare function preventNameCollissions(json: SveltosisComponent, input: string, arguments_: string[], prepend?: string, append?: string): {
3
- code: string;
4
- arguments: string[];
5
- } | {
3
+ declare type SveltosisStateValue = StateValue & {
4
+ arguments?: string[];
5
+ type: string;
6
+ };
7
+ export declare function preventNameCollissions(json: SveltosisComponent, item: SveltosisStateValue, prepend?: string, append?: string): {
6
8
  code: string;
7
- arguments?: undefined;
9
+ type: "function" | "getter" | "method" | "property";
10
+ typeParameter?: string | undefined;
11
+ arguments?: string[] | undefined;
8
12
  };
9
13
  export declare function postProcess(json: SveltosisComponent): void;
14
+ export {};
@@ -10,6 +10,29 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
13
36
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
37
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
38
  if (ar || !(i in from)) {
@@ -19,14 +42,28 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
42
  }
20
43
  return to.concat(ar || Array.prototype.slice.call(from));
21
44
  };
45
+ var __importDefault = (this && this.__importDefault) || function (mod) {
46
+ return (mod && mod.__esModule) ? mod : { "default": mod };
47
+ };
22
48
  Object.defineProperty(exports, "__esModule", { value: true });
23
49
  exports.postProcess = exports.preventNameCollissions = void 0;
50
+ var generator_1 = __importDefault(require("@babel/generator"));
51
+ var parser = __importStar(require("@babel/parser"));
52
+ var types = __importStar(require("@babel/types"));
24
53
  var bindings_1 = require("./bindings");
25
- function preventNameCollissions(json, input, arguments_, prepend, append) {
54
+ function preventNameCollissions(json, item, prepend, append) {
26
55
  if (prepend === void 0) { prepend = ''; }
27
56
  if (append === void 0) { append = '_'; }
28
- var output = input;
29
- var argumentsOutput = arguments_;
57
+ var output = item.code;
58
+ var argumentsOutput = [];
59
+ try {
60
+ var parsed = parser.parse(item.code);
61
+ var body = parsed.program.body[0];
62
+ if (types.isFunctionDeclaration(body)) {
63
+ argumentsOutput = body.params.map(function (p) { return (0, generator_1.default)(p).code; });
64
+ }
65
+ }
66
+ catch (e) { }
30
67
  var keys = __spreadArray(__spreadArray(__spreadArray([], Object.keys(json.props), true), Object.keys(json.state), true), Object.keys(json.refs), true);
31
68
  var _loop_1 = function (key) {
32
69
  var regex = function () { return new RegExp("(?<!=(?:\\s))".concat(key, "\\b"), 'g'); };
@@ -37,9 +74,10 @@ function preventNameCollissions(json, input, arguments_, prepend, append) {
37
74
  argumentsOutput.splice(index, 1, argument.replace(regex(), "".concat(prepend).concat(key).concat(append)));
38
75
  }
39
76
  });
40
- var isInOutput = regex().test(output);
77
+ var outputRegex = function () { return new RegExp("\\b".concat(key, "\\b"), 'g'); };
78
+ var isInOutput = outputRegex().test(output);
41
79
  if (isInArguments && isInOutput) {
42
- output = output.replace(regex(), "".concat(prepend).concat(key).concat(append));
80
+ output = output.replace(outputRegex(), "".concat(prepend).concat(key).concat(append));
43
81
  }
44
82
  };
45
83
  for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
@@ -47,11 +85,7 @@ function preventNameCollissions(json, input, arguments_, prepend, append) {
47
85
  _loop_1(key);
48
86
  }
49
87
  return (argumentsOutput === null || argumentsOutput === void 0 ? void 0 : argumentsOutput.length)
50
- ? {
51
- code: output,
52
- arguments: argumentsOutput,
53
- }
54
- : { code: output };
88
+ ? __assign(__assign({}, item), { code: output, arguments: argumentsOutput }) : __assign(__assign({}, item), { code: output });
55
89
  }
56
90
  exports.preventNameCollissions = preventNameCollissions;
57
91
  function prependProperties(json, input) {
@@ -71,7 +105,7 @@ function prependState(json, input) {
71
105
  var stateKeys = Object.keys(json.state);
72
106
  for (var _i = 0, stateKeys_1 = stateKeys; _i < stateKeys_1.length; _i++) {
73
107
  var state = stateKeys_1[_i];
74
- var regex = new RegExp("(?<!(\\.|'|\"|`|function ))\\b(state\\.)?".concat(state, "\\b(?!(\\s+)?\\()"), 'g');
108
+ var regex = new RegExp("(?<!(\\.|'|\"|`|function |get ))\\b(state\\.)?".concat(state, "\\b"), 'g');
75
109
  if (regex.test(output)) {
76
110
  output = output.replace(regex, "state.".concat(state));
77
111
  }
@@ -85,13 +119,14 @@ function addPropertiesAndState(json, input) {
85
119
  return output;
86
120
  }
87
121
  function addPropertiesAndStateToNode(json, node) {
88
- var _a, _b, _c;
89
- for (var _i = 0, _d = Object.keys(node.bindings); _i < _d.length; _i++) {
90
- var key = _d[_i];
122
+ var _a, _b, _c, _d;
123
+ for (var _i = 0, _e = Object.keys(node.bindings); _i < _e.length; _i++) {
124
+ var key = _e[_i];
91
125
  if (Object.prototype.hasOwnProperty.call(node.bindings, key)) {
92
126
  node.bindings[key] = {
93
127
  code: addPropertiesAndState(json, (_b = (_a = node.bindings[key]) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : '').trim(),
94
- type: (_c = node.bindings[key]) === null || _c === void 0 ? void 0 : _c.type,
128
+ arguments: (_c = node.bindings[key]) === null || _c === void 0 ? void 0 : _c.arguments,
129
+ type: (_d = node.bindings[key]) === null || _d === void 0 ? void 0 : _d.type,
95
130
  };
96
131
  }
97
132
  }
@@ -101,7 +136,7 @@ function postProcessState(json) {
101
136
  var key = _a[_i];
102
137
  var item = json.state[key];
103
138
  if ((item === null || item === void 0 ? void 0 : item.type) !== 'property') {
104
- var output = preventNameCollissions(json, item.code, (item === null || item === void 0 ? void 0 : item.arguments) || []);
139
+ var output = preventNameCollissions(json, item);
105
140
  output.code = addPropertiesAndState(json, output.code);
106
141
  json.state[key] = __assign(__assign({}, item), output);
107
142
  }
@@ -21,7 +21,7 @@ var actions_1 = require("./actions");
21
21
  var SPECIAL_ELEMENTS = new Set(['svelte:component', 'svelte:element']);
22
22
  function parseElement(json, node) {
23
23
  var _a;
24
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
24
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
25
25
  var mitosisNode = (0, mitosis_node_1.createMitosisNode)();
26
26
  mitosisNode.name = node.name;
27
27
  var useReference = function () {
@@ -46,8 +46,8 @@ function parseElement(json, node) {
46
46
  mitosisNode.name = "".concat(prefix, ".").concat(expression);
47
47
  }
48
48
  if ((_b = node.attributes) === null || _b === void 0 ? void 0 : _b.length) {
49
- for (var _i = 0, _s = node.attributes; _i < _s.length; _i++) {
50
- var attribute = _s[_i];
49
+ for (var _i = 0, _v = node.attributes; _i < _v.length; _i++) {
50
+ var attribute = _v[_i];
51
51
  switch (attribute.type) {
52
52
  case 'Attribute': {
53
53
  switch ((_c = attribute.value[0]) === null || _c === void 0 ? void 0 : _c.type) {
@@ -102,23 +102,41 @@ function parseElement(json, node) {
102
102
  break;
103
103
  }
104
104
  case 'EventHandler': {
105
- var object = { code: '', arguments: [] };
105
+ var object = {
106
+ code: '',
107
+ arguments: [],
108
+ };
106
109
  if (((_h = attribute.expression) === null || _h === void 0 ? void 0 : _h.type) === 'ArrowTypeFunction') {
107
110
  var expression = attribute.expression;
111
+ var code = (0, astring_1.generate)(expression.body);
108
112
  object = {
109
- code: (0, astring_1.generate)(expression.body),
110
- arguments: (_k = (_j = expression.body) === null || _j === void 0 ? void 0 : _j.arguments) === null || _k === void 0 ? void 0 : _k.map(function (a) { return a.name; }),
113
+ code: code,
114
+ arguments: (_k = (_j = expression.body) === null || _j === void 0 ? void 0 : _j.arguments) === null || _k === void 0 ? void 0 : _k.map(function (a) { var _a; return (_a = a.name) !== null && _a !== void 0 ? _a : []; }),
111
115
  };
112
116
  }
113
117
  else if (attribute.expression) {
114
118
  var code = (0, astring_1.generate)(attribute.expression);
115
- if (!((_l = attribute.expression.arguments) === null || _l === void 0 ? void 0 : _l.length) &&
116
- !((_o = (_m = attribute.expression.body) === null || _m === void 0 ? void 0 : _m.arguments) === null || _o === void 0 ? void 0 : _o.length)) {
119
+ if (((_l = attribute.expression.body) === null || _l === void 0 ? void 0 : _l.type) === 'CallExpression') {
120
+ code = (0, astring_1.generate)(attribute.expression.body);
121
+ }
122
+ if (!code.startsWith(')') && !code.endsWith(')')) {
123
+ code += '()';
124
+ }
125
+ if (!((_m = attribute.expression.arguments) === null || _m === void 0 ? void 0 : _m.length) &&
126
+ !((_p = (_o = attribute.expression.body) === null || _o === void 0 ? void 0 : _o.arguments) === null || _p === void 0 ? void 0 : _p.length)) {
117
127
  code = code.replace(/\(\)/g, '(event)');
118
128
  }
129
+ var args = undefined;
130
+ if (attribute.expression.type === 'ArrowFunctionExpression') {
131
+ args = (_r = (_q = attribute.expression.params) === null || _q === void 0 ? void 0 : _q.map(function (arg) { return (0, astring_1.generate)(arg); })) !== null && _r !== void 0 ? _r : [];
132
+ }
133
+ else if (attribute.expression.type === 'CallExpression' &&
134
+ attribute.expression.arguments.length) {
135
+ args = [];
136
+ }
119
137
  object = {
120
138
  code: code,
121
- arguments: ['event'],
139
+ arguments: args,
122
140
  };
123
141
  }
124
142
  else {
@@ -172,7 +190,7 @@ function parseElement(json, node) {
172
190
  // if there are existing class declarations
173
191
  // add them here instead and remove them from properties
174
192
  // to avoid duplicate class declarations in certain frameworks
175
- if ((_q = (_p = mitosisNode.properties) === null || _p === void 0 ? void 0 : _p.class) === null || _q === void 0 ? void 0 : _q.length) {
193
+ if ((_t = (_s = mitosisNode.properties) === null || _s === void 0 ? void 0 : _s.class) === null || _t === void 0 ? void 0 : _t.length) {
176
194
  code = "".concat(mitosisNode.properties.class, " ");
177
195
  delete mitosisNode.properties.class;
178
196
  }
@@ -180,7 +198,7 @@ function parseElement(json, node) {
180
198
  // append it to the string instead of assigning it
181
199
  if (mitosisNode.bindings.class &&
182
200
  Object.prototype.hasOwnProperty.call(mitosisNode.bindings.class, 'code') &&
183
- ((_r = mitosisNode.bindings.class) === null || _r === void 0 ? void 0 : _r.code.length)) {
201
+ ((_u = mitosisNode.bindings.class) === null || _u === void 0 ? void 0 : _u.code.length)) {
184
202
  code = (0, string_1.insertAt)(mitosisNode.bindings.class.code, ' ${' + binding + '}', mitosisNode.bindings.class.code.length - 1);
185
203
  mitosisNode.bindings.class = {
186
204
  code: code,
@@ -13,12 +13,21 @@ function parseReactive(json, node) {
13
13
  type: 'getter',
14
14
  };
15
15
  }
16
- else if (expression.left) {
16
+ else if (expression.type === 'AssignmentExpression') {
17
17
  var name_2 = expression.left.name;
18
18
  json.state[name_2] = {
19
19
  code: "get ".concat(name_2, "() {\n return ").concat((0, astring_1.generate)(expression.right), "}"),
20
20
  type: 'getter',
21
21
  };
22
22
  }
23
+ else if (expression.type === 'CallExpression') {
24
+ if (node.body.type === 'ExpressionStatement') {
25
+ json.hooks.onUpdate = json.hooks.onUpdate || [];
26
+ json.hooks.onUpdate.push({
27
+ code: (0, astring_1.generate)(node.body),
28
+ deps: "[".concat(expression.arguments.map(function (arg) { return (0, astring_1.generate)(arg); }), "]"),
29
+ });
30
+ }
31
+ }
23
32
  }
24
33
  exports.parseReactive = parseReactive;
@@ -22,7 +22,7 @@ function isPropertyOrStateReference(index) {
22
22
  return (0, lodash_1.isString)(index) && (index.includes('props.') || index.includes('state.'));
23
23
  }
24
24
  function parseReferences(json, node) {
25
- var _a, _b;
25
+ var _a, _b, _c;
26
26
  var declaration = node.declarations[0];
27
27
  var code;
28
28
  var type = 'property';
@@ -57,7 +57,7 @@ function parseReferences(json, node) {
57
57
  break;
58
58
  }
59
59
  default: {
60
- code = (_b = declaration === null || declaration === void 0 ? void 0 : declaration.init) === null || _b === void 0 ? void 0 : _b.value;
60
+ code = (_c = (_b = declaration === null || declaration === void 0 ? void 0 : declaration.init) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : null;
61
61
  }
62
62
  }
63
63
  json.state[declaration.id.name] = {