@builder.io/mitosis 0.0.69 → 0.0.70

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.
@@ -7,12 +7,12 @@ exports.updateStateSettersInCode = exports.updateStateSetters = exports.getUseSt
7
7
  var core_1 = require("@babel/core");
8
8
  var json5_1 = __importDefault(require("json5"));
9
9
  var traverse_1 = __importDefault(require("traverse"));
10
- var babel_transform_1 = require("../../helpers/babel-transform");
11
10
  var capitalize_1 = require("../../helpers/capitalize");
12
11
  var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
13
12
  var function_1 = require("fp-ts/lib/function");
14
13
  var helpers_1 = require("./helpers");
15
14
  var patterns_1 = require("../../helpers/patterns");
15
+ var transform_state_setters_1 = require("../../helpers/transform-state-setters");
16
16
  /**
17
17
  * Removes all `this.` references.
18
18
  */
@@ -88,19 +88,13 @@ var updateStateSettersInCode = function (value, options) {
88
88
  if (options.stateType !== 'useState') {
89
89
  return value;
90
90
  }
91
- return (0, babel_transform_1.babelTransformExpression)(value, {
92
- AssignmentExpression: function (path) {
91
+ return (0, transform_state_setters_1.transformStateSetters)({
92
+ value: value,
93
+ transformer: function (_a) {
94
+ var path = _a.path, propertyName = _a.propertyName;
93
95
  var node = path.node;
94
- if (core_1.types.isMemberExpression(node.left)) {
95
- if (core_1.types.isIdentifier(node.left.object)) {
96
- // TODO: utillity to properly trace this reference to the beginning
97
- if (node.left.object.name === 'state') {
98
- // TODO: ultimately support other property access like strings
99
- var propertyName = node.left.property.name;
100
- path.replaceWith(core_1.types.callExpression(core_1.types.identifier(getSetStateFnName(propertyName)), [node.right]));
101
- }
102
- }
103
- }
96
+ var newExpression = core_1.types.callExpression(core_1.types.identifier(getSetStateFnName(propertyName)), [node.right]);
97
+ return newExpression;
104
98
  },
105
99
  });
106
100
  };
@@ -14,6 +14,29 @@ var __assign = (this && this.__assign) || function () {
14
14
  };
15
15
  return __assign.apply(this, arguments);
16
16
  };
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
17
40
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
41
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
42
  if (ar || !(i in from)) {
@@ -52,6 +75,9 @@ var nullable_1 = require("../../helpers/nullable");
52
75
  var get_state_object_string_1 = require("../../helpers/get-state-object-string");
53
76
  var collect_css_1 = require("../../helpers/styles/collect-css");
54
77
  var hash_sum_1 = __importDefault(require("hash-sum"));
78
+ var Array_1 = require("fp-ts/lib/Array");
79
+ var S = __importStar(require("fp-ts/string"));
80
+ var helpers_2 = require("./state/helpers");
55
81
  var DEFAULT_OPTIONS = {
56
82
  state: 'signals',
57
83
  stylesType: 'styled-components',
@@ -125,7 +151,7 @@ var preProcessBlockCode = function (_a) {
125
151
  for (var key in json.properties) {
126
152
  var value = json.properties[key];
127
153
  if (value) {
128
- json.properties[key] = (0, state_1.updateStateCode)({ options: options, component: component, updateSetters: false })(value);
154
+ json.properties[key] = (0, helpers_2.updateStateCode)({ options: options, component: component, updateSetters: false })(value);
129
155
  }
130
156
  }
131
157
  for (var key in json.bindings) {
@@ -133,7 +159,7 @@ var preProcessBlockCode = function (_a) {
133
159
  if (value === null || value === void 0 ? void 0 : value.code) {
134
160
  json.bindings[key] = {
135
161
  arguments: value.arguments,
136
- code: (0, state_1.updateStateCode)({ options: options, component: component, updateSetters: true })(value.code),
162
+ code: (0, helpers_2.updateStateCode)({ options: options, component: component, updateSetters: true })(value.code),
137
163
  };
138
164
  }
139
165
  }
@@ -253,7 +279,7 @@ function addProviderComponents(json, options) {
253
279
  }
254
280
  var preProcessComponentCode = function (json, options) {
255
281
  var _a;
256
- var processCode = (0, state_1.updateStateCode)({ options: options, component: json });
282
+ var processCode = (0, helpers_2.updateStateCode)({ options: options, component: json });
257
283
  if ((_a = json.hooks.onMount) === null || _a === void 0 ? void 0 : _a.code) {
258
284
  json.hooks.onMount.code = processCode(json.hooks.onMount.code);
259
285
  }
@@ -295,12 +321,12 @@ var componentToSolid = function (passedOptions) {
295
321
  var componentHasContext = (0, context_1.hasContext)(json);
296
322
  var hasShowComponent = componentsUsed.has('Show');
297
323
  var hasForComponent = componentsUsed.has('For');
298
- var solidJSImports = __spreadArray(__spreadArray([
324
+ var solidJSImports = (0, Array_1.uniq)(S.Eq)(__spreadArray(__spreadArray([
299
325
  componentHasContext ? 'useContext' : undefined,
300
326
  hasShowComponent ? 'Show' : undefined,
301
327
  hasForComponent ? 'For' : undefined,
302
328
  ((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? 'onMount' : undefined
303
- ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect'] : []), true), ((_d = state === null || state === void 0 ? void 0 : state.import.solidjs) !== null && _d !== void 0 ? _d : []), true).filter(nullable_1.checkIsDefined);
329
+ ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect'] : []), true), ((_d = state === null || state === void 0 ? void 0 : state.import.solidjs) !== null && _d !== void 0 ? _d : []), true).filter(nullable_1.checkIsDefined));
304
330
  var storeImports = (_e = state === null || state === void 0 ? void 0 : state.import.store) !== null && _e !== void 0 ? _e : [];
305
331
  var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "])), solidJSImports.length > 0 ? "import { ".concat(solidJSImports.join(', '), " } from 'solid-js';") : '', !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", storeImports.length > 0 ? "import { ".concat(storeImports.join(', '), " } from 'solid-js/store';") : '', !componentHasStyles && options.stylesType === 'styled-components'
306
332
  ? ''
@@ -0,0 +1,13 @@
1
+ import { MitosisComponent } from '../../../types/mitosis-component';
2
+ import { ToSolidOptions } from '../types';
3
+ export declare const getStateSetterName: (stateName: string) => string;
4
+ export declare const getStateTypeForValue: ({ value, component, options, }: {
5
+ value: string;
6
+ component: MitosisComponent;
7
+ options: ToSolidOptions;
8
+ }) => import("../types").SolidState;
9
+ export declare const updateStateCode: ({ options, component, updateSetters, }: {
10
+ options: ToSolidOptions;
11
+ component: MitosisComponent;
12
+ updateSetters?: boolean | undefined;
13
+ }) => (value: string) => string;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateStateCode = exports.getStateTypeForValue = exports.getStateSetterName = void 0;
4
+ var core_1 = require("@babel/core");
5
+ var strip_state_and_props_refs_1 = require("../../../helpers/strip-state-and-props-refs");
6
+ var function_1 = require("fp-ts/lib/function");
7
+ var transform_state_setters_1 = require("../../../helpers/transform-state-setters");
8
+ var capitalize_1 = require("../../../helpers/capitalize");
9
+ var getStateSetterName = function (stateName) { return "set".concat((0, capitalize_1.capitalize)(stateName)); };
10
+ exports.getStateSetterName = getStateSetterName;
11
+ var getStateTypeForValue = function (_a) {
12
+ var _b, _c, _d, _e;
13
+ var value = _a.value, component = _a.component, options = _a.options;
14
+ // e.g. state.useContent?.blocks[0].id => useContent
15
+ var extractStateSliceName = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value).split('.')[0].split('?')[0];
16
+ var stateOverrideForValue = (_e = (_d = (_c = (_b = component.meta) === null || _b === void 0 ? void 0 : _b.useMetadata) === null || _c === void 0 ? void 0 : _c.solid) === null || _d === void 0 ? void 0 : _d.state) === null || _e === void 0 ? void 0 : _e[extractStateSliceName];
17
+ var stateType = stateOverrideForValue || options.state;
18
+ return stateType;
19
+ };
20
+ exports.getStateTypeForValue = getStateTypeForValue;
21
+ var getNewStateSetterExpression = function (stateType) {
22
+ return function (_a) {
23
+ var path = _a.path, propertyName = _a.propertyName;
24
+ /**
25
+ * passes the value to the setter function
26
+ * ```ts
27
+ * // BEFORE
28
+ * state.count = newCount
29
+ * // AFTER
30
+ * setCount(newCount)
31
+ * ```
32
+ */
33
+ var callValueSetter = function (args) {
34
+ return core_1.types.callExpression(core_1.types.identifier((0, exports.getStateSetterName)(propertyName)), [args]);
35
+ };
36
+ switch (stateType) {
37
+ case 'signals':
38
+ return callValueSetter(path.node.right);
39
+ case 'store':
40
+ /**
41
+ * Wrap value in a reconcile() call for Stores updates
42
+ * ```ts
43
+ * // BEFORE
44
+ * state.count = newCount
45
+ * // AFTER
46
+ * setCount(reconcile(newCount))
47
+ * ```
48
+ */
49
+ return callValueSetter(core_1.types.callExpression(core_1.types.identifier('reconcile'), [path.node.right]));
50
+ }
51
+ };
52
+ };
53
+ var updateStateSettersInCode = function (_a) {
54
+ var options = _a.options, component = _a.component;
55
+ return function (value) {
56
+ var stateType = (0, exports.getStateTypeForValue)({ value: value, component: component, options: options });
57
+ switch (stateType) {
58
+ case 'mutable':
59
+ return value;
60
+ case 'store':
61
+ case 'signals':
62
+ try {
63
+ return (0, transform_state_setters_1.transformStateSetters)({
64
+ value: value,
65
+ transformer: getNewStateSetterExpression(stateType),
66
+ });
67
+ }
68
+ catch (error) {
69
+ console.error("[Solid.js]: could not update state setters in ".concat(stateType, " code"), value);
70
+ throw error;
71
+ }
72
+ }
73
+ };
74
+ };
75
+ var updateStateGettersInCode = function (options, component) {
76
+ return function (value) {
77
+ return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value, {
78
+ includeState: true,
79
+ includeProps: false,
80
+ replaceWith: function (name) {
81
+ var stateType = (0, exports.getStateTypeForValue)({ value: name, component: component, options: options });
82
+ var state = component.state[name];
83
+ switch (stateType) {
84
+ case 'signals':
85
+ if (
86
+ // signal accessors are lazy, so we need to add a function call to property calls
87
+ (state === null || state === void 0 ? void 0 : state.type) === 'property' ||
88
+ // getters become plain functions, requiring a function call to access their value
89
+ (state === null || state === void 0 ? void 0 : state.type) === 'getter') {
90
+ return "".concat(name, "()");
91
+ }
92
+ else {
93
+ return name;
94
+ }
95
+ case 'store':
96
+ case 'mutable':
97
+ return name;
98
+ }
99
+ },
100
+ });
101
+ };
102
+ };
103
+ var updateStateCode = function (_a) {
104
+ var options = _a.options, component = _a.component, _b = _a.updateSetters, updateSetters = _b === void 0 ? true : _b;
105
+ return (0, function_1.flow)(updateSetters ? updateStateSettersInCode({ options: options, component: component }) : function_1.identity, updateStateGettersInCode(options, component), function (x) { return x.trim(); });
106
+ };
107
+ exports.updateStateCode = updateStateCode;
@@ -0,0 +1 @@
1
+ export { getState } from './state';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getState = void 0;
4
+ var state_1 = require("./state");
5
+ Object.defineProperty(exports, "getState", { enumerable: true, get: function () { return state_1.getState; } });
@@ -0,0 +1,7 @@
1
+ import { MitosisComponent, MitosisState } from '../../../types/mitosis-component';
2
+ import { ToSolidOptions } from '../types';
3
+ export declare const getSignalsCode: ({ json, options, state, }: {
4
+ json: MitosisComponent;
5
+ options: ToSolidOptions;
6
+ state: MitosisState;
7
+ }) => string;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getSignalsCode = void 0;
7
+ var json5_1 = __importDefault(require("json5"));
8
+ var function_1 = require("fp-ts/lib/function");
9
+ var patterns_1 = require("../../../helpers/patterns");
10
+ var helpers_1 = require("./helpers");
11
+ var processSignalStateValue = function (_a) {
12
+ var options = _a.options, component = _a.component;
13
+ var mapValue = (0, helpers_1.updateStateCode)({ options: options, component: component });
14
+ return function (_a) {
15
+ var key = _a[0], stateVal = _a[1];
16
+ var getDefaultCase = function () {
17
+ return (0, function_1.pipe)(value, json5_1.default.stringify, mapValue, function (x) { return "const [".concat(key, ", ").concat((0, helpers_1.getStateSetterName)(key), "] = createSignal(").concat(x, ")"); });
18
+ };
19
+ var value = stateVal === null || stateVal === void 0 ? void 0 : stateVal.code;
20
+ var type = stateVal === null || stateVal === void 0 ? void 0 : stateVal.type;
21
+ if (typeof value === 'string') {
22
+ switch (type) {
23
+ case 'getter':
24
+ return (0, function_1.pipe)(value, patterns_1.replaceGetterWithFunction, mapValue);
25
+ case 'function':
26
+ return mapValue(value);
27
+ case 'method':
28
+ return (0, function_1.pipe)(value, patterns_1.prefixWithFunction, mapValue);
29
+ default:
30
+ return getDefaultCase();
31
+ }
32
+ }
33
+ else {
34
+ return getDefaultCase();
35
+ }
36
+ };
37
+ };
38
+ var LINE_ITEM_DELIMITER = '\n\n\n';
39
+ var getSignalsCode = function (_a) {
40
+ var json = _a.json, options = _a.options, state = _a.state;
41
+ return Object.entries(state)
42
+ .map(processSignalStateValue({ options: options, component: json }))
43
+ /**
44
+ * We need to sort state so that signals are at the top.
45
+ */
46
+ .sort(function (a, b) {
47
+ var aHasSignal = a.includes('createSignal(');
48
+ var bHasSignal = b.includes('createSignal(');
49
+ if (aHasSignal && !bHasSignal) {
50
+ return -1;
51
+ }
52
+ else if (!aHasSignal && bHasSignal) {
53
+ return 1;
54
+ }
55
+ else {
56
+ return 0;
57
+ }
58
+ })
59
+ .join(LINE_ITEM_DELIMITER);
60
+ };
61
+ exports.getSignalsCode = getSignalsCode;
@@ -0,0 +1,14 @@
1
+ import { MitosisComponent } from '../../../types/mitosis-component';
2
+ import { ToSolidOptions } from '../types';
3
+ declare type State = {
4
+ str: string;
5
+ import: {
6
+ store?: string[];
7
+ solidjs?: string[];
8
+ };
9
+ };
10
+ export declare const getState: ({ json, options, }: {
11
+ json: MitosisComponent;
12
+ options: ToSolidOptions;
13
+ }) => State | undefined;
14
+ export {};
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.getState = void 0;
24
+ var get_state_object_string_1 = require("../../../helpers/get-state-object-string");
25
+ var function_1 = require("fp-ts/lib/function");
26
+ var state_1 = require("../../../helpers/state");
27
+ var helpers_1 = require("./helpers");
28
+ var signals_1 = require("./signals");
29
+ var store_1 = require("./store");
30
+ var getState = function (_a) {
31
+ var json = _a.json, options = _a.options;
32
+ var hasState = (0, state_1.checkHasState)(json);
33
+ if (!hasState) {
34
+ return undefined;
35
+ }
36
+ // unbundle state in case the user provides a type override of one of the state values
37
+ var _b = Object.entries(json.state).reduce(function (acc, _a) {
38
+ var _b, _c, _d;
39
+ var key = _a[0], value = _a[1];
40
+ var stateType = (0, helpers_1.getStateTypeForValue)({ value: key, component: json, options: options });
41
+ switch (stateType) {
42
+ case 'mutable':
43
+ return __assign(__assign({}, acc), { mutable: __assign(__assign({}, acc.mutable), (_b = {}, _b[key] = value, _b)) });
44
+ case 'signals':
45
+ return __assign(__assign({}, acc), { signal: __assign(__assign({}, acc.signal), (_c = {}, _c[key] = value, _c)) });
46
+ case 'store':
47
+ return __assign(__assign({}, acc), { store: __assign(__assign({}, acc.store), (_d = {}, _d[key] = value, _d)) });
48
+ }
49
+ }, { mutable: {}, signal: {}, store: {} }), mutable = _b.mutable, signal = _b.signal, store = _b.store;
50
+ var hasMutableState = Object.keys(mutable).length > 0;
51
+ var hasSignalState = Object.keys(signal).length > 0;
52
+ var hasStoreState = Object.keys(store).length > 0;
53
+ var mutableStateStr = hasMutableState
54
+ ? (0, function_1.pipe)(mutable, get_state_object_string_1.getMemberObjectString, function (str) { return "const state = createMutable(".concat(str, ");"); })
55
+ : '';
56
+ var signalStateStr = hasSignalState ? (0, signals_1.getSignalsCode)({ json: json, options: options, state: signal }) : '';
57
+ var storeStateStr = hasStoreState ? (0, store_1.getStoreCode)({ json: json, options: options, state: store }) : '';
58
+ var stateStr = "\n ".concat(mutableStateStr, "\n ").concat(signalStateStr, "\n ").concat(storeStateStr, "\n ");
59
+ var importObj = {
60
+ store: __spreadArray(__spreadArray([], (hasMutableState ? ['createMutable'] : []), true), (hasStoreState ? ['createStore', 'reconcile'] : []), true),
61
+ solidjs: __spreadArray(__spreadArray([], (hasSignalState ? ['createSignal'] : []), true), (hasStoreState ? ['createEffect', 'on'] : []), true),
62
+ };
63
+ return {
64
+ str: stateStr,
65
+ import: importObj,
66
+ };
67
+ };
68
+ exports.getState = getState;
@@ -0,0 +1,7 @@
1
+ import { MitosisComponent, MitosisState } from '../../../types/mitosis-component';
2
+ import { ToSolidOptions } from '../types';
3
+ export declare const getStoreCode: ({ json: component, options, state, }: {
4
+ json: MitosisComponent;
5
+ options: ToSolidOptions;
6
+ state: MitosisState;
7
+ }) => string;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getStoreCode = void 0;
16
+ var core_1 = require("@babel/core");
17
+ var json5_1 = __importDefault(require("json5"));
18
+ var babel_transform_1 = require("../../../helpers/babel-transform");
19
+ var capitalize_1 = require("../../../helpers/capitalize");
20
+ var function_1 = require("fp-ts/lib/function");
21
+ var patterns_1 = require("../../../helpers/patterns");
22
+ var helpers_1 = require("./helpers");
23
+ var collectUsedStateAndPropsInFunction = function (fnValue) {
24
+ var stateUsed = new Set();
25
+ var propsUsed = new Set();
26
+ (0, babel_transform_1.babelTransformExpression)(fnValue, {
27
+ MemberExpression: function (path) {
28
+ var node = path.node;
29
+ if (core_1.types.isIdentifier(node.object)) {
30
+ if (core_1.types.isIdentifier(node.property)) {
31
+ if (node.object.name === 'state') {
32
+ stateUsed.add("state.".concat(node.property.name));
33
+ }
34
+ else if (node.object.name === 'props') {
35
+ propsUsed.add("props.".concat(node.property.name));
36
+ }
37
+ }
38
+ }
39
+ },
40
+ });
41
+ return { stateUsed: stateUsed, propsUsed: propsUsed };
42
+ };
43
+ var getStoreCode = function (_a) {
44
+ var component = _a.json, options = _a.options, state = _a.state;
45
+ var mapValue = (0, helpers_1.updateStateCode)({ options: options, component: component });
46
+ var stateUpdater = function (_a) {
47
+ var key = _a[0], stateVal = _a[1];
48
+ var getCreateStoreStr = function (initialValue) {
49
+ return "const [".concat(key, ", ").concat((0, helpers_1.getStateSetterName)(key), "] = createStore(").concat(initialValue, ")");
50
+ };
51
+ var getDefaultCase = function () { return (0, function_1.pipe)(value, json5_1.default.stringify, mapValue, getCreateStoreStr); };
52
+ var value = stateVal === null || stateVal === void 0 ? void 0 : stateVal.code;
53
+ var type = stateVal === null || stateVal === void 0 ? void 0 : stateVal.type;
54
+ if (typeof value === 'string') {
55
+ switch (type) {
56
+ case 'getter':
57
+ var getterValueAsFunction = (0, patterns_1.replaceGetterWithFunction)(value);
58
+ var _b = collectUsedStateAndPropsInFunction(getterValueAsFunction), stateUsed = _b.stateUsed, propsUsed = _b.propsUsed;
59
+ var fnValueWithMappedRefs = mapValue(getterValueAsFunction);
60
+ var FUNCTION_NAME = "update".concat((0, capitalize_1.capitalize)(key));
61
+ var deps = __spreadArray(__spreadArray([], Array.from(stateUsed).map((0, helpers_1.updateStateCode)({
62
+ options: options,
63
+ component: component,
64
+ // there are no setters in deps
65
+ updateSetters: false,
66
+ })), true), Array.from(propsUsed), true).join(', ');
67
+ return "\n const ".concat(FUNCTION_NAME, " = ").concat(fnValueWithMappedRefs, "\n ").concat(getCreateStoreStr("".concat(FUNCTION_NAME, "()")), "\n createEffect(on(() => [").concat(deps, "], () => ").concat((0, helpers_1.getStateSetterName)(key), "(reconcile(").concat(FUNCTION_NAME, "()))))\n ");
68
+ case 'function':
69
+ return mapValue(value);
70
+ case 'method':
71
+ return (0, function_1.pipe)(value, patterns_1.prefixWithFunction, mapValue);
72
+ default:
73
+ return getDefaultCase();
74
+ }
75
+ }
76
+ else {
77
+ return getDefaultCase();
78
+ }
79
+ };
80
+ return Object.entries(state).map(stateUpdater).join(LINE_ITEM_DELIMITER);
81
+ };
82
+ exports.getStoreCode = getStoreCode;
83
+ var LINE_ITEM_DELIMITER = '\n\n\n';
@@ -1,5 +1,5 @@
1
1
  import { BaseTranspilerOptions } from '../../types/transpiler';
2
- export declare type SolidState = 'mutable' | 'signals';
2
+ export declare type SolidState = 'mutable' | 'signals' | 'store';
3
3
  export interface ToSolidOptions extends BaseTranspilerOptions {
4
4
  state: SolidState;
5
5
  stylesType?: 'styled-components' | 'style-tag';
@@ -638,7 +638,14 @@ function generateCompositionApiScript(component, options, template, props, onUpd
638
638
  methods += " function _classStringToObject(str) {\n const obj = {};\n if (typeof str !== 'string') { return obj }\n const classNames = str.trim().split(/\\s+/);\n for (const name of classNames) {\n obj[name] = true;\n }\n return obj;\n } ";
639
639
  }
640
640
  var getterKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'getter'; }));
641
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\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 ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.keys(component.context.get)) === null || _a === void 0 ? void 0 : _a.map(function (key) { return "const ".concat(key, " = inject(").concat(component.context.get[key].name, ")"); }).join('\n'), (_b = Object.keys(component.context.set)) === null || _b === void 0 ? void 0 : _b.map(function (key) { return "provide(".concat(component.context.set[key].name, ", ").concat(component.context.set[key].ref, ")"); }).join('\n'), (_c = Object.keys(component.refs)) === null || _c === void 0 ? void 0 : _c.map(function (key) { return "const ".concat(key, " = ref<").concat(component.refs[key].typeParameter, ">()"); }).join('\n'), appendValueToRefs((_e = (_d = component.hooks.onInit) === null || _d === void 0 ? void 0 : _d.code) !== null && _e !== void 0 ? _e : '', component, options), !((_f = component.hooks.onMount) === null || _f === void 0 ? void 0 : _f.code)
641
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\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 ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.keys(component.context.get)) === null || _a === void 0 ? void 0 : _a.map(function (key) { return "const ".concat(key, " = inject(").concat(component.context.get[key].name, ")"); }).join('\n'), (_b = Object.keys(component.context.set)) === null || _b === void 0 ? void 0 : _b.map(function (key) { return "provide(".concat(component.context.set[key].name, ", ").concat(component.context.set[key].ref, ")"); }).join('\n'), (_c = Object.keys(component.refs)) === null || _c === void 0 ? void 0 : _c.map(function (key) {
642
+ if (options.typescript) {
643
+ return "const ".concat(key, " = ref<").concat(component.refs[key].typeParameter, ">()");
644
+ }
645
+ else {
646
+ return "const ".concat(key, " = ref(null)");
647
+ }
648
+ }).join('\n'), appendValueToRefs((_e = (_d = component.hooks.onInit) === null || _d === void 0 ? void 0 : _d.code) !== null && _e !== void 0 ? _e : '', component, options), !((_f = component.hooks.onMount) === null || _f === void 0 ? void 0 : _f.code)
642
649
  ? ''
643
650
  : "onMounted(() => { ".concat(appendValueToRefs(component.hooks.onMount.code, component, options), "})"), !((_g = component.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
644
651
  ? ''
@@ -0,0 +1,14 @@
1
+ import { types } from '@babel/core';
2
+ declare type StateSetterTransformer = ({ path, propertyName, }: {
3
+ path: babel.NodePath<types.AssignmentExpression>;
4
+ propertyName: string;
5
+ }) => types.CallExpression;
6
+ /**
7
+ * Finds instances of state being set in `value`, and transforms them using the
8
+ * provided `transformer`.
9
+ */
10
+ export declare const transformStateSetters: ({ value, transformer, }: {
11
+ value: string;
12
+ transformer: StateSetterTransformer;
13
+ }) => string;
14
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformStateSetters = void 0;
4
+ var core_1 = require("@babel/core");
5
+ var babel_transform_1 = require("./babel-transform");
6
+ /**
7
+ * Finds instances of state being set in `value`, and transforms them using the
8
+ * provided `transformer`.
9
+ */
10
+ var transformStateSetters = function (_a) {
11
+ var value = _a.value, transformer = _a.transformer;
12
+ return (0, babel_transform_1.babelTransformExpression)(value, {
13
+ AssignmentExpression: function (path) {
14
+ var node = path.node;
15
+ if (core_1.types.isMemberExpression(node.left) &&
16
+ core_1.types.isIdentifier(node.left.object) &&
17
+ // TODO: utillity to properly trace this reference to the beginning
18
+ node.left.object.name === 'state') {
19
+ // TODO: ultimately support other property access like strings
20
+ var propertyName = node.left.property.name;
21
+ var newExpression = transformer({ path: path, propertyName: propertyName });
22
+ path.replaceWith(newExpression);
23
+ }
24
+ },
25
+ });
26
+ };
27
+ exports.transformStateSetters = transformStateSetters;