Skip to content

Commit

Permalink
Null check (#1669)
Browse files Browse the repository at this point in the history
* null check fix

* changeset

* Update .changeset/itchy-chairs-collect.md

Co-authored-by: Sami Jaber <jabersami@gmail.com>

---------

Co-authored-by: Sami Jaber <jabersami@gmail.com>
  • Loading branch information
mrkoreye and samijaber authored Jan 27, 2025
1 parent b15f534 commit 0c493b2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-chairs-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Builder] Fix null check issue with localized values
23 changes: 23 additions & 0 deletions packages/core/src/__tests__/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4509,6 +4509,29 @@ exports[`Builder > nodes as props 4`] = `
}
`;
exports[`Builder > null values 1`] = `
{
"@type": "@builder.io/mitosis/node",
"bindings": {
"text2": {
"bindingType": "expression",
"code": "null",
"type": "single",
},
},
"children": [],
"meta": {},
"name": "CustomText",
"properties": {
"$tagName": undefined,
"href": null,
"text": "hello",
},
"scope": {},
"slots": {},
}
`;
exports[`Builder > preserve cssCode when converting 1`] = `
".foo {
background: green;
Expand Down
30 changes: 29 additions & 1 deletion packages/core/src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { componentToMitosis } from '@/generators/mitosis';
import { ToMitosisOptions } from '@/generators/mitosis/types';
import { componentToReact } from '@/generators/react';
import { dedent } from '@/helpers/dedent';
import { builderContentToMitosisComponent, extractStateHook } from '@/parsers/builder';
import {
builderContentToMitosisComponent,
builderElementToMitosisNode,
extractStateHook,
} from '@/parsers/builder';
import { parseJsx } from '@/parsers/jsx';
import { compileAwayBuilderComponents } from '@/plugins/compile-away-builder-components';
import { BuilderContent } from '@builder.io/sdk';
Expand Down Expand Up @@ -485,6 +489,30 @@ describe('Builder', () => {
expect(backToBuilder).toMatchSnapshot();
});

test('null values', () => {
const component = builderElementToMitosisNode(
{
'@type': '@builder.io/sdk:Element',
'@version': 2,
id: 'builder-170e19cac58e4c28998d443a9dce80b8',
linkUrl: null,
component: {
name: 'CustomText',
options: {
text: 'hello',
text2: null,
},
},
properties: {
href: null,
},
} as any,
{},
);

expect(component).toMatchSnapshot();
});

test('preserve cssCode when converting', () => {
const builderJson: BuilderContent = {
data: {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/parsers/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,11 @@ export const builderElementToMitosisNode = (

const linkUrl = (block as any).linkUrl;
if (linkUrl) {
if (typeof linkUrl === 'object' && linkUrl['@type'] === '@builder.io/core:LocalizedValue') {
if (
typeof linkUrl === 'object' &&
linkUrl !== null &&
linkUrl['@type'] === '@builder.io/core:LocalizedValue'
) {
properties.href = linkUrl.Default;
localizedValues['linkUrl'] = linkUrl;
} else {
Expand Down Expand Up @@ -804,6 +808,7 @@ export const builderElementToMitosisNode = (
properties[key] = value;
} else if (
typeof value === 'object' &&
value !== null &&
value['@type'] === '@builder.io/core:LocalizedValue'
) {
properties[key] = value.Default;
Expand Down

0 comments on commit 0c493b2

Please sign in to comment.