Skip to content

Commit

Permalink
fix: Builder bound style literals are not removed (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi authored Feb 12, 2025
1 parent a68bd42 commit b91dfa7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-jars-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

Builder: bound style string literals are not removed
94 changes: 50 additions & 44 deletions packages/core/src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ describe('Builder', () => {
'responsiveStyles.small.top': 'state.top',
'responsiveStyles.large.color': 'state.color',
'style.fontSize': 'state.fontSize',
'style.background': '"red"',
'responsiveStyles.large.background': '"green"',
},
},
],
Expand All @@ -768,61 +770,65 @@ describe('Builder', () => {
{
"style": {
"bindingType": "expression",
"code": "{ fontSize: state.fontSize, \\"@media (max-width: 640px)\\": { left: state.left, top: state.top }, \\"@media (max-width: 1200px)\\": { color: state.color }, }",
"code": "{ fontSize: state.fontSize, background: \\"red\\", \\"@media (max-width: 640px)\\": { left: state.left, top: state.top }, \\"@media (max-width: 1200px)\\": { color: state.color, background: \\"green\\" }, }",
"type": "single",
},
}
`);

const jsx = componentToMitosis()({ component: mitosis });
expect(jsx).toMatchInlineSnapshot(`
"export default function MyComponent(props) {
return (
<div
style={{
fontSize: state.fontSize,
\\"@media (max-width: 640px)\\": {
left: state.left,
top: state.top,
},
\\"@media (max-width: 1200px)\\": {
color: state.color,
},
}}
/>
);
}
"
`);
"export default function MyComponent(props) {
return (
<div
style={{
fontSize: state.fontSize,
background: \\"red\\",
\\"@media (max-width: 640px)\\": {
left: state.left,
top: state.top,
},
\\"@media (max-width: 1200px)\\": {
color: state.color,
background: \\"green\\",
},
}}
/>
);
}
"
`);

const json = componentToBuilder()({ component: mitosis });
expect(json).toMatchInlineSnapshot(`
{
"data": {
"blocks": [
{
"@type": "@builder.io/sdk:Element",
"actions": {},
"bindings": {
"responsiveStyles.large.color": "state.color",
"responsiveStyles.small.left": "state.left",
"responsiveStyles.small.top": "state.top",
"style.fontSize": "state.fontSize",
},
"children": [],
"code": {
"actions": {},
"bindings": {},
},
"properties": {},
"tagName": "div",
{
"data": {
"blocks": [
{
"@type": "@builder.io/sdk:Element",
"actions": {},
"bindings": {
"responsiveStyles.large.background": "\\"green\\"",
"responsiveStyles.large.color": "state.color",
"responsiveStyles.small.left": "state.left",
"responsiveStyles.small.top": "state.top",
"style.background": "\\"red\\"",
"style.fontSize": "state.fontSize",
},
"children": [],
"code": {
"actions": {},
"bindings": {},
},
"properties": {},
"tagName": "div",
},
],
"jsCode": "",
"tsCode": "",
},
],
"jsCode": "",
"tsCode": "",
},
}
`);
}
`);
});

test('map custom component bindings', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/parsers/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ export function convertExportDefaultToReturn(code: string) {
try {
const { types } = babel;
const body = parseCode(code);
if (body.length === 0) return code;
const newBody = body.slice();
for (let i = 0; i < body.length; i++) {
const statement = body[i];
Expand Down

0 comments on commit b91dfa7

Please sign in to comment.