@builder.io/mitosis 0.5.11 → 0.5.12

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.
@@ -39,18 +39,21 @@ function emitStateMethods(file, componentState, lexicalArgs) {
39
39
  case 'method':
40
40
  case 'function':
41
41
  let code = stateValue.code;
42
- let prefixIdx = 0;
43
- if (stateValue.type === 'function') {
44
- prefixIdx += 'function '.length;
42
+ const isAsync = code.startsWith('async');
43
+ if (!isAsync) {
44
+ let prefixIdx = 0;
45
+ if (stateValue.type === 'function') {
46
+ prefixIdx += 'function '.length;
47
+ }
48
+ code = code.substring(prefixIdx);
49
+ code = (0, convert_method_to_function_1.convertMethodToFunction)(code, methodMap, lexicalArgs).replace('(', `(${lexicalArgs.join(',')},`);
45
50
  }
46
- code = code.substring(prefixIdx);
47
- code = (0, convert_method_to_function_1.convertMethodToFunction)(code, methodMap, lexicalArgs).replace('(', `(${lexicalArgs.join(',')},`);
48
51
  const functionName = code.split(/\(/)[0];
49
52
  if (!file.options.isTypeScript) {
50
53
  // Erase type information
51
54
  code = (0, babel_transform_1.convertTypeScriptToJS)(code);
52
55
  }
53
- file.exportConst(functionName, 'function ' + code, true);
56
+ file.exportConst(isAsync ? key : functionName, isAsync ? code : 'function ' + code, true);
54
57
  continue;
55
58
  case 'property':
56
59
  stateValues[key] = stateValue.code;
@@ -377,7 +377,7 @@ const _componentToReact = (json, options, isSubComponent = false) => {
377
377
  if (type === 'getter')
378
378
  return `${key} = function ${code.replace('get ', '')}`;
379
379
  if (type === 'function')
380
- return `${key} = function ${code}`;
380
+ return code.startsWith('async') ? code : `${key} = function ${code}`;
381
381
  return code;
382
382
  },
383
383
  })
@@ -128,6 +128,24 @@ const processStateObjectSlice = (item) => {
128
128
  };
129
129
  }
130
130
  else if ((0, types_1.isArrowFunctionExpression)(item.value)) {
131
+ /**
132
+ * Arrow functions are normally converted to object methods to work around
133
+ * limitations with arrow functions in state in frameworks such as Svelte.
134
+ * However, this conversion does not work for async arrow functions due to
135
+ * how we handle parsing in `handleErrorOrExpression` for parsing
136
+ * expressions. That code does not detect async functions in order to apply
137
+ * its parsing workarounds. Even if it did, it does not consider async code
138
+ * when prefixing with "function". This would result in "function async foo()"
139
+ * which is not a valid function expression definition.
140
+ */
141
+ // TODO ENG-7256 Find a way to do this without diverging code path
142
+ if (item.value.async) {
143
+ const func = (0, types_1.functionExpression)(item.key, item.value.params, item.value.body, false, true);
144
+ return {
145
+ code: (0, helpers_1.parseCode)(func).trim(),
146
+ type: 'function',
147
+ };
148
+ }
131
149
  const n = (0, types_1.objectMethod)('method', item.key, item.value.params, item.value.body);
132
150
  const code = (0, helpers_1.parseCode)(n).trim();
133
151
  return {
@@ -153,6 +171,14 @@ const processStateObjectSlice = (item) => {
153
171
  }
154
172
  }
155
173
  else if ((0, types_1.isObjectMethod)(item)) {
174
+ // TODO ENG-7256 Find a way to do this without diverging code path
175
+ if (item.async) {
176
+ const func = (0, types_1.functionExpression)(item.key, item.params, item.body, false, true);
177
+ return {
178
+ code: (0, helpers_1.parseCode)(func).trim(),
179
+ type: 'function',
180
+ };
181
+ }
156
182
  const n = (0, helpers_1.parseCode)({ ...item, returnType: null }).trim();
157
183
  const isGetter = item.kind === 'get';
158
184
  return {
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.11",
25
+ "version": "0.5.12",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {