Skip to content

Commit f442a8c

Browse files
fix: ENG-9776 AI feature not working because of custom component name which includes colon (#1780)
* fix: ENG-9776 custom component name which includes colon * chore: add changeset * fix: review comment * fix: test * fix: remove unused variable
1 parent 6d343e9 commit f442a8c

File tree

4 files changed

+107
-6
lines changed

4 files changed

+107
-6
lines changed

.changeset/kind-steaks-study.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@builder.io/mitosis': patch
3+
'@builder.io/mitosis-cli': patch
4+
---
5+
6+
fix: support for builder custom component with a colon in their name

packages/core/src/generators/builder/generator.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ const findStateWithinMitosisComponent = (
138138
const omitMetaProperties = (obj: Record<string, any>) =>
139139
omitBy(obj, (_value, key) => key.startsWith('$'));
140140

141-
const builderBlockPrefixes = ['Amp', 'Core', 'Builder', 'Raw', 'Form'];
142-
const mapComponentName = (name: string) => {
141+
export const builderBlockPrefixes = ['Amp', 'Core', 'Builder', 'Raw', 'Form'];
142+
const mapComponentName = (name: string, properties?: { [key: string]: string | undefined }) => {
143+
if (properties?.['data-builder-originalName']) {
144+
return properties['data-builder-originalName'];
145+
}
143146
if (name === 'CustomCode') {
144147
return 'Custom Code';
145148
}
@@ -792,8 +795,8 @@ export const blockToBuilder = (
792795
}),
793796
...(thisIsComponent && {
794797
component: {
795-
name: mapComponentName(json.name),
796-
options: componentOptions,
798+
name: mapComponentName(json.name, json.properties),
799+
options: omit(componentOptions, ['data-builder-originalName']),
797800
},
798801
}),
799802
code: {
@@ -905,7 +908,6 @@ export const componentToBuilder =
905908
{ ...convertMitosisStateToBuilderState(component.state) },
906909
options.stateMap,
907910
);
908-
console.log('stateData', stateData);
909911
return { state: stateData };
910912
})(),
911913
blocks: component.children

packages/core/src/parsers/builder/builder.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { componentToBuilder } from '@/generators/builder';
2+
import { componentToMitosis } from '@/generators/mitosis';
13
import { BuilderContent } from '@builder.io/sdk';
4+
import { parseJsx } from '../jsx';
25
import { builderContentToMitosisComponent } from './builder';
36

47
describe('Unpaired Surrogates', () => {
@@ -28,4 +31,84 @@ describe('Unpaired Surrogates', () => {
2831
expect(output.children[0].children[0].properties._text).not.toContain('\uD800');
2932
expect(output.children[0].children[0].properties._text).not.toContain('\uDFFF');
3033
});
34+
35+
test('should handle builder component with/without a colon in the name', () => {
36+
const builderContent: BuilderContent = {
37+
data: {
38+
blocks: [
39+
{
40+
'@type': '@builder.io/sdk:Element' as const,
41+
component: {
42+
name: 'Text:123',
43+
options: {
44+
text: 'Hello World',
45+
},
46+
},
47+
},
48+
{
49+
'@type': '@builder.io/sdk:Element' as const,
50+
component: {
51+
name: 'Text123',
52+
options: {
53+
text: 'Hello World',
54+
},
55+
},
56+
},
57+
],
58+
},
59+
};
60+
61+
const component = builderContentToMitosisComponent(builderContent);
62+
expect(component.children[0].name).toBe('Text123');
63+
expect(component.children[1].name).toBe('Text123');
64+
const backToBuilder = componentToBuilder()({ component });
65+
expect(backToBuilder?.data?.blocks?.[0]?.component?.name).toBe('Text:123');
66+
expect(backToBuilder?.data?.blocks?.[1]?.component?.name).toBe('Text123');
67+
});
68+
69+
test('should handle builder content -> mitosis json -> mitosis jsx -> mitosis json -> builder content', () => {
70+
const builderContent: BuilderContent = {
71+
data: {
72+
blocks: [
73+
{
74+
'@type': '@builder.io/sdk:Element' as const,
75+
component: {
76+
name: 'Text:123',
77+
options: {
78+
text: 'Hello World',
79+
},
80+
},
81+
},
82+
],
83+
},
84+
};
85+
86+
// Convert Builder JSON to Mitosis JSON
87+
const mitosisCmp = builderContentToMitosisComponent(builderContent);
88+
expect(mitosisCmp.children[0].name).toBe('Text123');
89+
expect(mitosisCmp.children[0].properties['data-builder-originalName']).toBe('Text:123');
90+
91+
// Convert Mitosis JSON to Mitosis JSX
92+
const mitosisJsx = componentToMitosis()({ component: mitosisCmp });
93+
expect(mitosisJsx).toMatchInlineSnapshot(`
94+
"import { Text123 } from \\"@components\\";
95+
96+
export default function MyComponent(props) {
97+
return <Text123 text=\\"Hello World\\" data-builder-originalName=\\"Text:123\\" />;
98+
}
99+
"
100+
`);
101+
102+
// Convert back Mitosis JSX to Mitosis JSON
103+
const backToMitosisCmp = parseJsx(mitosisJsx);
104+
expect(backToMitosisCmp.children[0].name).toBe('Text123');
105+
expect(backToMitosisCmp.children[0].properties['data-builder-originalName']).toBe('Text:123');
106+
107+
// Convert back Mitosis JSON to Builder JSON
108+
const backToBuilder = componentToBuilder()({ component: backToMitosisCmp });
109+
expect(backToBuilder?.data?.blocks?.[0]?.component?.name).toBe('Text:123');
110+
expect(backToBuilder?.data?.blocks?.[0]?.component?.options).not.toHaveProperty(
111+
'data-builder-originalName',
112+
);
113+
});
31114
});

packages/core/src/parsers/builder/builder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { builderBlockPrefixes } from '@/generators/builder/generator';
12
import { hashCodeAsString } from '@/symbols/symbol-processor';
23
import { MitosisComponent, MitosisState } from '@/types/mitosis-component';
34
import * as babel from '@babel/core';
@@ -934,6 +935,13 @@ export const builderElementToMitosisNode = (
934935
if (block.groupLocked !== undefined) {
935936
dataAttributes['data-builder-groupLocked'] = String(block.groupLocked);
936937
}
938+
if (
939+
block.component?.name &&
940+
/:/.test(block.component?.name) &&
941+
!builderBlockPrefixes.includes(block.component?.name.split(':')[0])
942+
) {
943+
dataAttributes['data-builder-originalName'] = block.component?.name;
944+
}
937945

938946
const node = createMitosisNode({
939947
name:
@@ -961,7 +969,9 @@ export const builderElementToMitosisNode = (
961969
...slots,
962970
},
963971
...(Object.keys(blocksSlots).length > 0 && { blocksSlots }),
964-
meta: getMetaFromBlock(block, options),
972+
meta: {
973+
...getMetaFromBlock(block, options),
974+
},
965975
...(Object.keys(localizedValues).length && { localizedValues }),
966976
});
967977

0 commit comments

Comments
 (0)