Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unsupported bound styles from Builder generator to avoid crashes #1685

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-eggs-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

Builder: unsupported bound styles are removed to avoid crashes
43 changes: 43 additions & 0 deletions packages/core/src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,49 @@ describe('Builder', () => {
`);
});

test('drop unsupported bound styles to avoid crashes', () => {
const jsx = `export default function MyComponent(props) {
return (
<div
style={{
fontSize: state.fontSize,
'&:hover': {
backgroundColor: state.foo === 1 ? "red" : "blue"
}
}}
/>
);
}`;

const mitosis = parseJsx(jsx);

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

test('map custom component bindings', () => {
const content = {
data: {
Expand Down
33 changes: 28 additions & 5 deletions packages/core/src/generators/builder/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,40 @@ const mapBoundStyles = (bindings: { [key: string]: Binding | undefined }) => {
};
}
} else {
bindings[`style.${key}`] = {
code: parsed[key],
bindingType: 'expression',
type: 'single',
};
if (isGlobalStyle(key)) {
console.warn(
`The following bound styles are not supported by Builder JSON and have been removed:
"${key}": ${parsed[key]}
`,
);
} else {
bindings[`style.${key}`] = {
code: parsed[key],
bindingType: 'expression',
type: 'single',
};
}
}
}

delete bindings['style'];
};

function isGlobalStyle(key: string) {
// These are mapped to their respective responsiveStyle and support bindings
if (/max-width: (.*?)px/gm.exec(key)) {
return false;
}

return (
// pseudo class
key.startsWith('&:') ||
key.startsWith(':') ||
// @ rules
key.startsWith('@')
);
}

export const blockToBuilder = (
json: MitosisNode,
options: ToBuilderOptions = {},
Expand Down