From 08601cc9fcfeceb402816fbeb3b28d0465d9ef04 Mon Sep 17 00:00:00 2001 From: Nicolas Merget Date: Wed, 15 Jan 2025 11:01:09 +0100 Subject: [PATCH 01/20] refactor: change defaultProps generation for react --- .changeset/fluffy-eyes-add.md | 5 + .../__snapshots__/mitosis.test.ts.snap | 100 +- .../__snapshots__/preact.test.ts.snap | 104 +- .../__snapshots__/react-native.test.ts.snap | 104 +- .../react-state-builder.test.ts.snap | 104 +- .../react-state-mobx.test.ts.snap | 104 +- .../react-state-solid.test.ts.snap | 104 +- .../react-state-valtio.test.ts.snap | 104 +- .../react-state-variables.test.ts.snap | 2447 +++++++---------- .../__snapshots__/react.test.ts.snap | 104 +- .../__tests__/__snapshots__/rsc.test.ts.snap | 506 +--- .../__tests__/__snapshots__/taro.test.ts.snap | 104 +- packages/core/src/__tests__/react.test.ts | 12 + packages/core/src/generators/react/blocks.ts | 2 +- .../core/src/generators/react/generator.ts | 203 +- .../generators/react/helpers/default-props.ts | 14 + .../src/generators/react/helpers/hooks.ts | 95 + .../react/{helpers.ts => helpers/index.ts} | 5 +- .../generators/react/{ => helpers}/state.ts | 77 +- .../core/src/helpers/get-function-string.ts | 2 + 20 files changed, 1726 insertions(+), 2574 deletions(-) create mode 100644 .changeset/fluffy-eyes-add.md create mode 100644 packages/core/src/generators/react/helpers/default-props.ts create mode 100644 packages/core/src/generators/react/helpers/hooks.ts rename packages/core/src/generators/react/{helpers.ts => helpers/index.ts} (93%) rename packages/core/src/generators/react/{ => helpers}/state.ts (57%) create mode 100644 packages/core/src/helpers/get-function-string.ts diff --git a/.changeset/fluffy-eyes-add.md b/.changeset/fluffy-eyes-add.md new file mode 100644 index 0000000000..b8f02f1215 --- /dev/null +++ b/.changeset/fluffy-eyes-add.md @@ -0,0 +1,5 @@ +--- +'@builder.io/mitosis': patch +--- + +[react]: Changed `defaultProps` generation for react, because defaultProps for function components is deprecated diff --git a/packages/core/src/__tests__/__snapshots__/mitosis.test.ts.snap b/packages/core/src/__tests__/__snapshots__/mitosis.test.ts.snap index 8b2e56b818..6aade9ad5b 100644 --- a/packages/core/src/__tests__/__snapshots__/mitosis.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/mitosis.test.ts.snap @@ -15416,7 +15416,9 @@ exports[`Mitosis, format: react > jsx > Javascript Test > ContentSlotJSX 1`] = ` import * as React from \\"react\\"; import { useState } from \\"react\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -15454,12 +15456,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -16937,7 +16933,16 @@ exports[`Mitosis, format: react > jsx > Javascript Test > defaultProps 1`] = ` "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -16962,15 +16967,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -16979,7 +16975,14 @@ exports[`Mitosis, format: react > jsx > Javascript Test > defaultPropsOutsideCom "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -17004,13 +17007,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -19124,7 +19120,9 @@ import * as React from \\"react\\"; import { useState } from \\"react\\"; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -19162,12 +19160,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -20653,7 +20645,16 @@ exports[`Mitosis, format: react > jsx > Typescript Test > defaultProps 1`] = ` "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -20678,15 +20679,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -20695,7 +20687,14 @@ exports[`Mitosis, format: react > jsx > Typescript Test > defaultPropsOutsideCom "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -20720,13 +20719,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -22303,7 +22295,7 @@ exports[`Mitosis, format: react > svelte > Javascript Test > classDirective 1`] import * as React from \\"react\\"; import { useState } from \\"react\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const [focus, setFocus] = useState(() => true); return ( @@ -22315,8 +22307,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -22706,7 +22696,7 @@ exports[`Mitosis, format: react > svelte > Typescript Test > classDirective 1`] import * as React from \\"react\\"; import { useState } from \\"react\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const [focus, setFocus] = useState(() => true); return ( @@ -22718,8 +22708,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/preact.test.ts.snap b/packages/core/src/__tests__/__snapshots__/preact.test.ts.snap index 3e5fc770be..1c48c8a76a 100644 --- a/packages/core/src/__tests__/__snapshots__/preact.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/preact.test.ts.snap @@ -560,7 +560,9 @@ exports[`Preact > jsx > Javascript Test > ContentSlotJSX 1`] = ` import { h, Fragment } from \\"preact\\"; import { useState } from \\"preact/hooks\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -598,12 +600,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -2103,7 +2099,16 @@ exports[`Preact > jsx > Javascript Test > defaultProps 1`] = ` "/** @jsx h */ import { h, Fragment } from \\"preact\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -2128,15 +2133,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -2145,7 +2141,14 @@ exports[`Preact > jsx > Javascript Test > defaultPropsOutsideComponent 1`] = ` "/** @jsx h */ import { h, Fragment } from \\"preact\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -2170,13 +2173,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -4384,7 +4380,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -4422,12 +4424,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -6161,7 +6157,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -6186,15 +6191,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -6211,7 +6207,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -6236,13 +6239,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -8055,7 +8051,7 @@ exports[`Preact > svelte > Javascript Test > classDirective 1`] = ` import { h, Fragment } from \\"preact\\"; import { useState } from \\"preact/hooks\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const [focus, setFocus] = useState(() => true); return ( @@ -8067,8 +8063,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -8459,7 +8453,7 @@ exports[`Preact > svelte > Typescript Test > classDirective 1`] = ` import { h, Fragment } from \\"preact\\"; import { useState } from \\"preact/hooks\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const [focus, setFocus] = useState(() => true); return ( @@ -8471,8 +8465,6 @@ function MyComponent(props: any) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/react-native.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-native.test.ts.snap index 7cd91475be..18b0eb23fc 100644 --- a/packages/core/src/__tests__/__snapshots__/react-native.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-native.test.ts.snap @@ -823,7 +823,9 @@ import { } from \\"react-native\\"; import { useState } from \\"react\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -860,12 +862,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -2842,7 +2838,16 @@ import { Linking, } from \\"react-native\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return ( {props.link ? ( @@ -2866,15 +2871,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -2895,7 +2891,14 @@ import { Linking, } from \\"react-native\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return ( {props.link ? ( @@ -2919,13 +2922,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -6180,7 +6176,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const [name, setName] = useState(() => \\"king\\"); const [showContent, setShowContent] = useState(() => false); @@ -6217,12 +6219,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -8433,7 +8429,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return ( {props.link ? ( @@ -8457,15 +8462,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -8494,7 +8490,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return ( {props.link ? ( @@ -8518,13 +8521,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -11263,14 +11259,12 @@ import { } from \\"react-native\\"; import { useState } from \\"react\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const [focus, setFocus] = useState(() => true); return ; } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -11858,14 +11852,12 @@ import { } from \\"react-native\\"; import { useState } from \\"react\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const [focus, setFocus] = useState(() => true); return ; } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/react-state-builder.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-state-builder.test.ts.snap index bbea432570..c2c3dbe167 100644 --- a/packages/core/src/__tests__/__snapshots__/react-state-builder.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-state-builder.test.ts.snap @@ -564,7 +564,9 @@ exports[`React - stateType: builder > jsx > Javascript Test > ContentSlotJSX 1`] "\\"use client\\"; import * as React from \\"react\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const state = useBuilderState({ name: \\"king\\", showContent: false, @@ -601,12 +603,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -2122,7 +2118,16 @@ exports[`React - stateType: builder > jsx > Javascript Test > defaultProps 1`] = "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { const state = useBuilderState({}); return ( @@ -2149,15 +2154,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -2166,7 +2162,14 @@ exports[`React - stateType: builder > jsx > Javascript Test > defaultPropsOutsid "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { const state = useBuilderState({}); return ( @@ -2193,13 +2196,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -4373,7 +4369,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const state = useBuilderState({ name: \\"king\\", showContent: false, @@ -4410,12 +4412,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -6163,7 +6159,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { const state = useBuilderState({}); return ( @@ -6190,15 +6195,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -6215,7 +6211,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { const state = useBuilderState({}); return ( @@ -6242,13 +6245,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -8019,7 +8015,7 @@ exports[`React - stateType: builder > svelte > Javascript Test > classDirective "\\"use client\\"; import * as React from \\"react\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const state = useBuilderState({ focus: true }); return ( @@ -8031,8 +8027,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -8419,7 +8413,7 @@ exports[`React - stateType: builder > svelte > Typescript Test > classDirective "\\"use client\\"; import * as React from \\"react\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const state = useBuilderState({ focus: true }); return ( @@ -8431,8 +8425,6 @@ function MyComponent(props: any) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/react-state-mobx.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-state-mobx.test.ts.snap index e7ddbc2ad6..3e028e0ddb 100644 --- a/packages/core/src/__tests__/__snapshots__/react-state-mobx.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-state-mobx.test.ts.snap @@ -589,7 +589,9 @@ exports[`React - stateType: mobx > jsx > Javascript Test > ContentSlotJSX 1`] = import * as React from \\"react\\"; import { useLocalObservable, observer } from \\"mobx-react-lite\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const state = useLocalObservable(() => ({ name: \\"king\\", showContent: false, @@ -626,12 +628,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - const observedContentSlotJsxCode = observer(ContentSlotJsxCode); export default observedContentSlotJsxCode; " @@ -2175,7 +2171,16 @@ exports[`React - stateType: mobx > jsx > Javascript Test > defaultProps 1`] = ` "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -2200,15 +2205,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - const observedButton = observer(Button); export default observedButton; " @@ -2218,7 +2214,14 @@ exports[`React - stateType: mobx > jsx > Javascript Test > defaultPropsOutsideCo "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -2243,13 +2246,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - const observedButton = observer(Button); export default observedButton; " @@ -4443,7 +4439,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const state = useLocalObservable(() => ({ name: \\"king\\", showContent: false, @@ -4480,12 +4482,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - const observedContentSlotJsxCode = observer(ContentSlotJsxCode); export default observedContentSlotJsxCode; " @@ -6250,7 +6246,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -6275,15 +6280,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - const observedButton = observer(Button); export default observedButton; " @@ -6301,7 +6297,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -6326,13 +6329,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - const observedButton = observer(Button); export default observedButton; " @@ -8104,7 +8100,7 @@ exports[`React - stateType: mobx > svelte > Javascript Test > classDirective 1`] import * as React from \\"react\\"; import { useLocalObservable, observer } from \\"mobx-react-lite\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const state = useLocalObservable(() => ({ focus: true })); return ( @@ -8116,8 +8112,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - const observedMyComponent = observer(MyComponent); export default observedMyComponent; " @@ -8527,7 +8521,7 @@ exports[`React - stateType: mobx > svelte > Typescript Test > classDirective 1`] import * as React from \\"react\\"; import { useLocalObservable, observer } from \\"mobx-react-lite\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const state = useLocalObservable(() => ({ focus: true })); return ( @@ -8539,8 +8533,6 @@ function MyComponent(props: any) { ); } -MyComponent.defaultProps = {}; - const observedMyComponent = observer(MyComponent); export default observedMyComponent; " diff --git a/packages/core/src/__tests__/__snapshots__/react-state-solid.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-state-solid.test.ts.snap index c183b05b58..c72f2be890 100644 --- a/packages/core/src/__tests__/__snapshots__/react-state-solid.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-state-solid.test.ts.snap @@ -569,7 +569,9 @@ exports[`React - stateType: solid > jsx > Javascript Test > ContentSlotJSX 1`] = import * as React from \\"react\\"; import { useMutable } from \\"react-solid-state\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const state = useMutable({ name: \\"king\\", showContent: false, @@ -606,12 +608,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -2107,7 +2103,16 @@ exports[`React - stateType: solid > jsx > Javascript Test > defaultProps 1`] = ` "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -2132,15 +2137,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -2149,7 +2145,14 @@ exports[`React - stateType: solid > jsx > Javascript Test > defaultPropsOutsideC "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -2174,13 +2177,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -4298,7 +4294,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const state = useMutable({ name: \\"king\\", showContent: false, @@ -4335,12 +4337,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -6057,7 +6053,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -6082,15 +6087,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -6107,7 +6103,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -6132,13 +6135,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -7852,7 +7848,7 @@ exports[`React - stateType: solid > svelte > Javascript Test > classDirective 1` import * as React from \\"react\\"; import { useMutable } from \\"react-solid-state\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const state = useMutable({ focus: true }); return ( @@ -7864,8 +7860,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -8258,7 +8252,7 @@ exports[`React - stateType: solid > svelte > Typescript Test > classDirective 1` import * as React from \\"react\\"; import { useMutable } from \\"react-solid-state\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const state = useMutable({ focus: true }); return ( @@ -8270,8 +8264,6 @@ function MyComponent(props: any) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/react-state-valtio.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-state-valtio.test.ts.snap index e7c9ed47cf..51e426fe1a 100644 --- a/packages/core/src/__tests__/__snapshots__/react-state-valtio.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-state-valtio.test.ts.snap @@ -569,7 +569,9 @@ exports[`React - stateType: valtio > jsx > Javascript Test > ContentSlotJSX 1`] import * as React from \\"react\\"; import { useLocalProxy } from \\"valtio/utils\\"; -function ContentSlotJsxCode(props) { +function ContentSlotJsxCode( + props = { content: \\"\\", slotReference: undefined, slotContent: undefined } +) { const state = useLocalProxy({ name: \\"king\\", showContent: false, @@ -606,12 +608,6 @@ function ContentSlotJsxCode(props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -2110,7 +2106,16 @@ exports[`React - stateType: valtio > jsx > Javascript Test > defaultProps 1`] = "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -2135,15 +2140,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -2152,7 +2148,14 @@ exports[`React - stateType: valtio > jsx > Javascript Test > defaultPropsOutside "\\"use client\\"; import * as React from \\"react\\"; -function Button(props) { +function Button( + props = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -2177,13 +2180,6 @@ function Button(props) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -4301,7 +4297,13 @@ type Props = { }; import type { JSX } from \\"../../../../jsx-runtime\\"; -function ContentSlotJsxCode(props: Props) { +function ContentSlotJsxCode( + props: Props = { + content: \\"\\", + slotReference: undefined, + slotContent: undefined, + } +) { const state = useLocalProxy({ name: \\"king\\", showContent: false, @@ -4338,12 +4340,6 @@ function ContentSlotJsxCode(props: Props) { ); } -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - export default ContentSlotJsxCode; " `; @@ -6063,7 +6059,16 @@ export interface ButtonProps { onClick?: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => { + console.log(\\"hi\\"); + }, + } +) { return (
{props.link ? ( @@ -6088,15 +6093,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => { - console.log(\\"hi\\"); - }, -}; - export default Button; " `; @@ -6113,7 +6109,14 @@ export interface ButtonProps { onClick: () => void; } -function Button(props: ButtonProps) { +function Button( + props: ButtonProps = { + text: \\"default text\\", + link: \\"https://builder.io/\\", + openLinkInNewTab: false, + onClick: () => {}, + } +) { return (
{props.link ? ( @@ -6138,13 +6141,6 @@ function Button(props: ButtonProps) { ); } -Button.defaultProps = { - text: \\"default text\\", - link: \\"https://builder.io/\\", - openLinkInNewTab: false, - onClick: () => {}, -}; - export default Button; " `; @@ -7858,7 +7854,7 @@ exports[`React - stateType: valtio > svelte > Javascript Test > classDirective 1 import * as React from \\"react\\"; import { useLocalProxy } from \\"valtio/utils\\"; -function MyComponent(props) { +function MyComponent(props = {}) { const state = useLocalProxy({ focus: true }); return ( @@ -7870,8 +7866,6 @@ function MyComponent(props) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; @@ -8264,7 +8258,7 @@ exports[`React - stateType: valtio > svelte > Typescript Test > classDirective 1 import * as React from \\"react\\"; import { useLocalProxy } from \\"valtio/utils\\"; -function MyComponent(props: any) { +function MyComponent(props: any = {}) { const state = useLocalProxy({ focus: true }); return ( @@ -8276,8 +8270,6 @@ function MyComponent(props: any) { ); } -MyComponent.defaultProps = {}; - export default MyComponent; " `; diff --git a/packages/core/src/__tests__/__snapshots__/react-state-variables.test.ts.snap b/packages/core/src/__tests__/__snapshots__/react-state-variables.test.ts.snap index b3f4293bd6..68fd1b9a29 100644 --- a/packages/core/src/__tests__/__snapshots__/react-state-variables.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/react-state-variables.test.ts.snap @@ -1,63 +1,115 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`React - stateType: variables > jsx > Javascript Test > AdvancedRef 1`] = ` -"'(' expected. (18:34) - 16 | const inputNoArgRef = useRef(null); - 17 | const name= 'PatrickJS' -> 18 | const onBlur = onBlur = function function onBlur() { - | ^ - 19 | // Maintain focus - 20 | inputRef.current.focus(); - 21 | }" -`; - -exports[`React - stateType: variables > jsx > Javascript Test > Basic 1`] = ` "\\"use client\\"; import * as React from \\"react\\"; +import { useRef, useEffect } from \\"react\\"; -export const DEFAULT_VALUES = { - name: \\"Steve\\", -}; - -function MyBasicComponent(props) { - const name = \\"Steve\\"; - const underscore_fn_name = function underscore_fn_name() { - return \\"bar\\"; +function MyBasicRefComponent(props) { + const inputRef = useRef(null); + const inputNoArgRef = useRef(null); + const name = \\"PatrickJS\\"; + const onBlur = function onBlur() { + // Maintain focus + inputRef.current.focus(); + }; + const lowerCaseName = function lowerCaseName() { + return name.toLowerCase(); }; - const age = 1; - const sports = [\\"\\"]; + + useEffect(() => { + console.log(\\"Received an update\\"); + }, [inputRef.current, inputNoArgRef.current]); return ( <> -
- (name = myEvent.target.value)} - /> - Hello! I can run in React, Vue, Solid, or Liquid! > +
+ {props.showInput ? ( + <> + onBlur()} + onChange={(event) => (name = event.target.value)} + /> + + + + ) : null} + Hello + {lowerCaseName()}! I can run in React, Qwik, Vue, Solid, or Web + Component!
); } -export default MyBasicComponent; +export default MyBasicRefComponent; " `; +exports[`React - stateType: variables > jsx > Javascript Test > Basic 1`] = ` +"'function' is not allowed as a variable declaration name. (18:7) + 16 | function MyBasicComponent(props) { + 17 | const name= 'Steve' +> 18 | const function underscore_fn_name() { + | ^ + 19 | return 'bar'; + 20 | } + 21 | const age= 1" +`; + exports[`React - stateType: variables > jsx > Javascript Test > Basic Context 1`] = ` -"'(' expected. (16:38) - 14 | function MyBasicComponent(props) { - 15 | const name= 'PatrickJS' -> 16 | const onChange = onChange = function function onChange() { - | ^ - 17 | const change = myService.method('change'); - 18 | console.log(change); - 19 | }" +"\\"use client\\"; +import * as React from \\"react\\"; +import { useContext, useRef, useEffect } from \\"react\\"; +import { Injector, MyService, createInjector } from \\"@dummy/injection-js\\"; + +function MyBasicComponent(props) { + const name = \\"PatrickJS\\"; + const onChange = function onChange() { + const change = myService.method(\\"change\\"); + console.log(change); + }; + + const myService = useContext(MyService); + + const hasInitialized = useRef(false); + if (!hasInitialized.current) { + const hi = myService.method(\\"hi\\"); + console.log(hi); + hasInitialized.current = true; + } + + useEffect(() => { + const bye = myService.method(\\"hi\\"); + console.log(bye); + }, []); + + return ( + +
+ {myService.method(\\"hello\\") + name} + Hello! I can run in React, Vue, Solid, or Liquid! + onChange} /> +
+
+ ); +} + +export default MyBasicComponent; +" `; exports[`React - stateType: variables > jsx > Javascript Test > Basic OnMount Update 1`] = ` @@ -227,25 +279,82 @@ export default MyBasicForComponent; `; exports[`React - stateType: variables > jsx > Javascript Test > BasicRef 1`] = ` -"'(' expected. (18:34) - 16 | const inputNoArgRef = useRef(null); - 17 | const name= 'PatrickJS' -> 18 | const onBlur = onBlur = function function onBlur() { - | ^ - 19 | // Maintain focus - 20 | inputRef.current?.focus(); - 21 | }" +"\\"use client\\"; +import * as React from \\"react\\"; +import { useRef } from \\"react\\"; + +function MyBasicRefComponent(props) { + const inputRef = useRef(null); + const inputNoArgRef = useRef(null); + const name = \\"PatrickJS\\"; + const onBlur = function onBlur() { + // Maintain focus + inputRef.current?.focus(); + }; + const lowerCaseName = function lowerCaseName() { + return name.toLowerCase(); + }; + + return ( + <> +
+ {props.showInput ? ( + <> + onBlur()} + onChange={(event) => (name = event.target.value)} + /> + + + + ) : null} + Hello + {lowerCaseName()}! I can run in React, Qwik, Vue, Solid, or Web + Component! +
+ + + ); +} + +export default MyBasicRefComponent; +" `; exports[`React - stateType: variables > jsx > Javascript Test > BasicRefAssignment 1`] = ` -"'(' expected. (16:46) - 14 | function MyBasicRefAssignmentComponent(props) { - 15 | const holdValueRef = useRef('Patrick'); -> 16 | const handlerClick = handlerClick = function function handlerClick(event) { - | ^ - 17 | event.preventDefault(); - 18 | console.log('current value', holdValueRef.current); - 19 | holdValueRef.current = holdValueRef.current + 'JS';" +"\\"use client\\"; +import * as React from \\"react\\"; +import { useRef } from \\"react\\"; + +function MyBasicRefAssignmentComponent(props) { + const holdValueRef = useRef(\\"Patrick\\"); + const handlerClick = function handlerClick(event) { + event.preventDefault(); + console.log(\\"current value\\", holdValueRef.current); + holdValueRef.current = holdValueRef.current + \\"JS\\"; + }; + + return ( +
+ +
+ ); +} + +export default MyBasicRefAssignmentComponent; +" `; exports[`React - stateType: variables > jsx > Javascript Test > BasicRefPrevious 1`] = ` @@ -318,63 +427,14 @@ export default Button; `; exports[`React - stateType: variables > jsx > Javascript Test > Columns 1`] = ` -"import * as React from \\"react\\"; - -function Column(props) { - const getColumns = function getColumns() { - return props.columns || []; - }; - const getGutterSize = function getGutterSize() { - return typeof props.space === \\"number\\" ? props.space || 0 : 20; - }; - const getWidth = function getWidth(index) { - const columns = getColumns(); - return (columns[index] && columns[index].width) || 100 / columns.length; - }; - const getColumnCssWidth = function getColumnCssWidth(index) { - const columns = getColumns(); - const gutterSize = getGutterSize(); - const subtractWidth = (gutterSize * (columns.length - 1)) / columns.length; - return \`calc(\${getWidth(index)}% - \${subtractWidth}px)\`; - }; - - return ( - <> -
- {props.columns?.map((column, index) => ( -
- {column.content} - {index} -
- ))} -
- - - ); -} - -export default Column; -" +"'function' is not allowed as a variable declaration name. (14:9) + 12 | + 13 | function Column(props) { +> 14 | const function getColumns() { + | ^ + 15 | return props.columns || []; + 16 | } + 17 | const function getGutterSize() {" `; exports[`React - stateType: variables > jsx > Javascript Test > ContentSlotHtml 1`] = ` @@ -399,214 +459,58 @@ export default ContentSlotCode; `; exports[`React - stateType: variables > jsx > Javascript Test > ContentSlotJSX 1`] = ` -"\\"use client\\"; -import * as React from \\"react\\"; - -function ContentSlotJsxCode(props) { - const name = \\"king\\"; - const showContent = false; - const cls = function cls() { - return props.slotContent && props.children ? \`\${name}-content\` : \\"\\"; - }; - const show = function show() { - props.slotContent ? 1 : \\"\\"; - }; - - return ( - <> - {props.slotReference ? ( - <> -
show()} - className={cls()} - > - {showContent && props.slotContent ? ( - <>{props.content || \\"{props.content}\\"} - ) : null} -
-
-
-
{props.children}
-
- - ) : null} - - ); -} - -ContentSlotJsxCode.defaultProps = { - content: \\"\\", - slotReference: undefined, - slotContent: undefined, -}; - -export default ContentSlotJsxCode; -" +"'function' is not allowed as a variable declaration name. (17:7) + 15 | const name= 'king' + 16 | const showContent= false +> 17 | const function cls() { + | ^ + 18 | return props.slotContent && props.children ? \`\${name}-content\` : ''; + 19 | } + 20 | const function show() {" `; exports[`React - stateType: variables > jsx > Javascript Test > CustomCode 1`] = ` -"\\"use client\\"; -import * as React from \\"react\\"; -import { useRef, useEffect } from \\"react\\"; - -function CustomCode(props) { - const elem = useRef(null); - const scriptsInserted = []; - const scriptsRun = []; - const findAndRunScripts = function findAndRunScripts() { - // TODO: Move this function to standalone one in '@builder.io/utils' - if (elem.current && typeof window !== \\"undefined\\") { - /** @type {HTMLScriptElement[]} */ - const scripts = elem.current.getElementsByTagName(\\"script\\"); - - for (let i = 0; i < scripts.length; i++) { - const script = scripts[i]; - - if (script.src) { - if (scriptsInserted.includes(script.src)) { - continue; - } - - scriptsInserted.push(script.src); - const newScript = document.createElement(\\"script\\"); - newScript.async = true; - newScript.src = script.src; - document.head.appendChild(newScript); - } else if ( - !script.type || - [ - \\"text/javascript\\", - \\"application/javascript\\", - \\"application/ecmascript\\", - ].includes(script.type) - ) { - if (scriptsRun.includes(script.innerText)) { - continue; - } - - try { - scriptsRun.push(script.innerText); - new Function(script.innerText)(); - } catch (error) { - console.warn(\\"\`CustomCode\`: Error running script:\\", error); - } - } - } - } - }; - - useEffect(() => { - findAndRunScripts(); - }, []); - - return ( -
- ); -} - -export default CustomCode; -" +"'function' is not allowed as a variable declaration name. (18:7) + 16 | const scriptsInserted= [] + 17 | const scriptsRun= [] +> 18 | const function findAndRunScripts() { + | ^ + 19 | // TODO: Move this function to standalone one in '@builder.io/utils' + 20 | if (elem.current && typeof window !== 'undefined') { + 21 | /** @type {HTMLScriptElement[]} */" `; exports[`React - stateType: variables > jsx > Javascript Test > Embed 1`] = ` -"\\"use client\\"; -import * as React from \\"react\\"; -import { useRef, useEffect } from \\"react\\"; - -function CustomCode(props) { - const elem = useRef(null); - const scriptsInserted = []; - const scriptsRun = []; - const findAndRunScripts = function findAndRunScripts() { - // TODO: Move this function to standalone one in '@builder.io/utils' - if (elem.current && typeof window !== \\"undefined\\") { - /** @type {HTMLScriptElement[]} */ - const scripts = elem.current.getElementsByTagName(\\"script\\"); - - for (let i = 0; i < scripts.length; i++) { - const script = scripts[i]; - - if (script.src) { - if (scriptsInserted.includes(script.src)) { - continue; - } - - scriptsInserted.push(script.src); - const newScript = document.createElement(\\"script\\"); - newScript.async = true; - newScript.src = script.src; - document.head.appendChild(newScript); - } else if ( - !script.type || - [ - \\"text/javascript\\", - \\"application/javascript\\", - \\"application/ecmascript\\", - ].includes(script.type) - ) { - if (scriptsRun.includes(script.innerText)) { - continue; - } - - try { - scriptsRun.push(script.innerText); - new Function(script.innerText)(); - } catch (error) { - console.warn(\\"\`CustomCode\`: Error running script:\\", error); - } - } - } - } - }; - - useEffect(() => { - findAndRunScripts(); - }, []); - - return ( -
- ); -} - -export default CustomCode; -" +"'function' is not allowed as a variable declaration name. (18:7) + 16 | const scriptsInserted= [] + 17 | const scriptsRun= [] +> 18 | const function findAndRunScripts() { + | ^ + 19 | // TODO: Move this function to standalone one in '@builder.io/utils' + 20 | if (elem.current && typeof window !== 'undefined') { + 21 | /** @type {HTMLScriptElement[]} */" `; exports[`React - stateType: variables > jsx > Javascript Test > Form 1`] = ` -"',' expected. (108:2796) - 106 | if (contentType && contentType !== 'multipart/form-data') { - 107 | if ( -> 108 | /* Zapier doesn't allow content-type header to be sent from browsers */ !(sendWithJs && props.action?.includes('zapier.com'))) { headers['content-type'] = contentType; } } const presubmitEvent = new CustomEvent('presubmit', { detail: { body } }); if (formRef.current) { formRef.current.dispatchEvent(presubmitEvent); if (presubmitEvent.defaultPrevented) { return; } } formState = 'sending'; const formUrl = \`\${builder.env === 'dev' ? 'http://localhost:5000' : 'https://builder.io'}/api/v1/form-submit?apiKey=\${builder.apiKey}&to=\${btoa(props.sendSubmissionsToEmail || '')}&name=\${encodeURIComponent(props.name || '')}\`; fetch(props.sendSubmissionsTo === 'email' ? formUrl : props.action /* TODO: throw error if no action URL */ , { body, headers, method: props.method || 'post' }).then(async res => { let body; const contentType = res.headers.get('content-type'); if (contentType && contentType.indexOf('application/json') !== -1) { body = await res.json(); } else { body = await res.text(); } if (!res.ok && props.errorMessagePath) { /* TODO: allow supplying an error formatter function */ let message = get(body, props.errorMessagePath); if (message) { if (typeof message !== 'string') { /* TODO: ideally convert json to yaml so it woul dbe like error: - email has been taken */ message = JSON.stringify(message); } formErrorMessage = message; } } responseData = body; formState = res.ok ? 'success' : 'error'; if (res.ok) { const submitSuccessEvent = new CustomEvent('submit:success', { detail: { res, body } }); if (formRef.current) { formRef.current.dispatchEvent(submitSuccessEvent); if (submitSuccessEvent.defaultPrevented) { return; } /* TODO: option to turn this on/off? */ if (props.resetFormOnSubmit !== false) { formRef.current.reset(); } } /* TODO: client side route event first that can be preventDefaulted */ if (props.successUrl) { if (formRef.current) { const event = new CustomEvent('route', { detail: { url: props.successUrl } }); formRef.current.dispatchEvent(event); if (!event.defaultPrevented) { location.href = props.successUrl; } } else { location.href = props.successUrl; } } } }, err => { const submitErrorEvent = new CustomEvent('submit:error', { detail: { error: err } }); if (formRef.current) { formRef.current.dispatchEvent(submitErrorEvent); if (submitErrorEvent.defaultPrevented) { return; } } responseData = err; formState = 'error'; }); } } return ( <>
onSubmit(event) } {...(props.attributes)}>{props.builderBlock && props.builderBlock.children ? ( <>{props.builderBlock?.children?.map((block, index) => ( ))} ) : null}{submissionState() === 'error' ? ( ) : null}{submissionState() === 'sending' ? ( ) : null}{submissionState() === 'error' && responseData ? (
{JSON.stringify(responseData, null, 2)}
) : null}{submissionState() === 'success' ? ( ) : null} ); } export default FormComponent; - | ^ - 109 | - 110 | - 111 |" +"'function' is not allowed as a variable declaration name. (20:7) + 18 | const responseData= null + 19 | const formErrorMessage= '' +> 20 | const function submissionState() { + | ^ + 21 | return Builder.isEditing && props.previewState || formState; + 22 | } + 23 | const function onSubmit(event) {" `; exports[`React - stateType: variables > jsx > Javascript Test > Image 1`] = ` -"'(' expected. (25:40) - 23 | return !!props.lazy && isBrowser(); - 24 | } -> 25 | const isBrowser = isBrowser = function function isBrowser() { - | ^ - 26 | return typeof window !== 'undefined' && window.navigator.product != 'ReactNative'; - 27 | } - 28 | const load= false" +"'function' is not allowed as a variable declaration name. (18:7) + 16 | const scrollListener= null + 17 | const imageLoaded= false +> 18 | const function setLoaded() { + | ^ + 19 | imageLoaded = true; + 20 | } + 21 | const function useLazyLoading() {" `; exports[`React - stateType: variables > jsx > Javascript Test > Image State 1`] = ` @@ -683,25 +587,14 @@ export default FormInputComponent; `; exports[`React - stateType: variables > jsx > Javascript Test > InputParent 1`] = ` -"import * as React from \\"react\\"; -import FormInputComponent from \\"./input.raw\\"; - -function Stepper(props) { - const handleChange = function handleChange(value) { - console.log(value); - }; - - return ( - handleChange(value)} - /> - ); -} - -export default Stepper; -" +"'function' is not allowed as a variable declaration name. (14:9) + 12 | + 13 | function Stepper(props) { +> 14 | const function handleChange(value) { + | ^ + 15 | console.log(value); + 16 | } + 17 |" `; exports[`React - stateType: variables > jsx > Javascript Test > NestedStore 1`] = ` @@ -862,150 +755,47 @@ export default SlotCode; `; exports[`React - stateType: variables > jsx > Javascript Test > Stamped.io 1`] = ` -"\\"use client\\"; -import * as React from \\"react\\"; -import { useEffect } from \\"react\\"; -import { kebabCase, snakeCase } from \\"lodash\\"; - -function SmileReviews(props) { - const reviews = []; - const name = \\"test\\"; - const showReviewPrompt = false; - const kebabCaseValue = function kebabCaseValue() { - return kebabCase(\\"testThat\\"); - }; - const snakeCaseValue = function snakeCaseValue() { - return snakeCase(\\"testThis\\"); - }; - - useEffect(() => { - fetch( - \`https://stamped.io/api/widget/reviews?storeUrl=builder-io.myshopify.com&apiKey=\${ - props.apiKey || \\"pubkey-8bbDq7W6w4sB3OWeM1HUy2s47702hM\\" - }&productId=\${props.productId || \\"2410511106127\\"}\` - ) - .then((res) => res.json()) - .then((data) => { - reviews = data.data; - }); - }, []); - - return ( - <> -
- - {showReviewPrompt || \\"asdf\\" ? ( - <> - - -