From 455dcee60c631e03dc721e27acd598dc1ceba23f Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Wed, 20 Jul 2022 14:52:12 -0400 Subject: [PATCH 1/3] Properly support and type optional props in Svelte components --- packages/integrations/svelte/src/editor.cts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/integrations/svelte/src/editor.cts b/packages/integrations/svelte/src/editor.cts index 202609d3393f..22946b07b141 100644 --- a/packages/integrations/svelte/src/editor.cts +++ b/packages/integrations/svelte/src/editor.cts @@ -7,14 +7,14 @@ export function toTSX(code: string, className: string): string { `; try { - let tsx = svelte2tsx(code).code; - tsx = 'let Props = render().props;\n' + tsx; + let tsx = svelte2tsx(code, { mode: 'ts' }).code; + tsx = '/// \n' + tsx; - // Replace Svelte's class export with a function export result = tsx.replace( - /^export default[\S\s]*/gm, - `export default function ${className}__AstroComponent_(_props: typeof Props): any {}` + 'export default class extends __sveltets_1_createSvelte2TsxComponent(', + 'let Component = ' ); + result += `\nexport default function ${className}__AstroComponent_(_props: typeof Component.props): any {}`; } catch (e: any) { return result; } From 579e90cd5f0ba00e8b42b2180c2908067a8da2df Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Wed, 20 Jul 2022 15:48:13 -0400 Subject: [PATCH 2/3] Change output to support documentating components --- packages/integrations/svelte/src/editor.cts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/integrations/svelte/src/editor.cts b/packages/integrations/svelte/src/editor.cts index 22946b07b141..c14f082b1505 100644 --- a/packages/integrations/svelte/src/editor.cts +++ b/packages/integrations/svelte/src/editor.cts @@ -9,12 +9,10 @@ export function toTSX(code: string, className: string): string { try { let tsx = svelte2tsx(code, { mode: 'ts' }).code; tsx = '/// \n' + tsx; - result = tsx.replace( 'export default class extends __sveltets_1_createSvelte2TsxComponent(', - 'let Component = ' + `export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = ` ); - result += `\nexport default function ${className}__AstroComponent_(_props: typeof Component.props): any {}`; } catch (e: any) { return result; } From dd14843a1965f732ecd34eeb4956677cbd149401 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Wed, 20 Jul 2022 17:32:25 -0400 Subject: [PATCH 3/3] Add changeset --- .changeset/lovely-lions-attend.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lovely-lions-attend.md diff --git a/.changeset/lovely-lions-attend.md b/.changeset/lovely-lions-attend.md new file mode 100644 index 000000000000..e81a2b6dcf27 --- /dev/null +++ b/.changeset/lovely-lions-attend.md @@ -0,0 +1,5 @@ +--- +'@astrojs/svelte': patch +--- + +Fix optional props not being recognized properly in the editor