@builder.io/mitosis 0.5.14 → 0.5.16

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.
@@ -151,46 +151,29 @@ const componentMappers = {
151
151
  if (value.type === 'single' && value.bindingType === 'function') {
152
152
  try {
153
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
- }
182
- },
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') + '}';
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
+ },
175
+ });
176
+ thing.bindings[key].code = (0, generator_1.default)(programNode).code;
194
177
  }
195
178
  catch (error) {
196
179
  console.error('Error processing function binding. Falling back to simple replacement.', error);
@@ -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 &&
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.14",
25
+ "version": "0.5.16",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {