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

Properly support and type optional props in Svelte components #3993

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Changes from 2 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
10 changes: 4 additions & 6 deletions packages/integrations/svelte/src/editor.cts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ export function toTSX(code: string, className: string): string {
`;

try {
let tsx = svelte2tsx(code).code;
tsx = 'let Props = render().props;\n' + tsx;

// Replace Svelte's class export with a function export
let tsx = svelte2tsx(code, { mode: 'ts' }).code;
tsx = '/// <reference types="svelte2tsx/svelte-shims" />\n' + tsx;
result = tsx.replace(
/^export default[\S\s]*/gm,
`export default function ${className}__AstroComponent_(_props: typeof Props): any {}`
'export default class extends __sveltets_1_createSvelte2TsxComponent(',
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `
);
Copy link
Member Author

@Princesseuh Princesseuh Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR:

Svelte Component

<script lang="ts">
  export const hello = "Hello";
  export const wow = "Hello";
  export let hey: string;
</script>
{hey}

Old

let Props = render().props;
///<reference types="svelte" />
;function render() {

   const hello = "Hello";
   const wow = "Hello";
   let hey: string/*Ωignore_startΩ*/;hey = __sveltets_1_any(hey);/*Ωignore_endΩ*/;
;
async () => {

hey;

};
return { props: {hello: hello , wow: wow , hey: hey}, slots: {}, getters: {hello: hello, wow: wow}, events: {} }}

export default function MySvelteComponent__AstroComponent_(_props: typeof Props): any {}

New

/// <reference types="svelte2tsx/svelte-shims" />
///<reference types="svelte" />
;function render() {

   const hello = "Hello";
   const wow = "Hello";
   let hey: string/*Ωignore_startΩ*/;hey = __sveltets_1_any(hey);/*Ωignore_endΩ*/;
;
async () => {

hey;
};
return { props: {hello: hello , wow: wow , hey: hey}, slots: {}, getters: {hello: hello, wow: wow}, events: {} }}

export default function MySvelteComponent__AstroComponent_(_props: typeof Component.props): any {}
let Component = __sveltets_1_partial(['hello','wow'], __sveltets_1_with_any_event(render()))) {
    get hello() { return render().getters.hello }
    get wow() { return render().getters.wow }
}

(The export being on top of the let Component is on purpose, so the component documentation ends up on the exported function)

} catch (e: any) {
return result;
Expand Down