Skip to content

Commit 3922861

Browse files
authored
fix: ensure exports in runes mode are marked as used (#2719)
Ensure they are read as part of exporting the types #2717
1 parent 05c515e commit 3922861

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -758,18 +758,17 @@ export class ExportedNames {
758758
let str = '';
759759

760760
if (others.length > 0 || this.usesRunes() || needsAccessors) {
761+
const exports = needsAccessors ? names : others;
762+
761763
if (others.length > 0 || needsAccessors) {
762764
if (this.isTsFile) {
763765
str +=
764-
', exports: {} as any as { ' +
765-
this.createReturnElementsType(
766-
needsAccessors ? names : others,
767-
undefined,
768-
true
769-
).join(',') +
766+
// Reference imports that have a type, else they are marked as unused if nothing in the component references them
767+
`, exports: {${this.createReturnElements(this.usesRunes() ? others : [], false, true)}} as any as { ` +
768+
this.createReturnElementsType(exports, undefined, true).join(',') +
770769
' }';
771770
} else {
772-
str += `, exports: /** @type {{${this.createReturnElementsType(needsAccessors ? names : others, false, true)}}} */ ({})`;
771+
str += `, exports: /** @type {{${this.createReturnElementsType(exports, false, true)}}} */ ({})`;
773772
}
774773
} else {
775774
// Always add that, in TS5.5+ the type for Exports is infered to never when this is not present, which breaks types.
@@ -791,14 +790,18 @@ export class ExportedNames {
791790

792791
private createReturnElements(
793792
names: Array<[string, ExportedName]>,
794-
dontAddTypeDef: boolean
793+
dontAddTypeDef: boolean,
794+
omitTyped = false
795795
): string[] {
796-
return names.map(([key, value]) => {
797-
// Important to not use shorthand props for rename functionality
798-
return `${dontAddTypeDef && value.doc ? `\n${value.doc}` : ''}${
799-
value.identifierText || key
800-
}: ${key}`;
801-
});
796+
return names
797+
.map(([key, value]) => {
798+
if (omitTyped && value.type) return;
799+
// Important to not use shorthand props for rename functionality
800+
return `${dontAddTypeDef && value.doc ? `\n${value.doc}` : ''}${
801+
value.identifierText || key
802+
}: ${key}`;
803+
})
804+
.filter(Boolean);
802805
}
803806

804807
private createReturnElementsType(

packages/svelte2tsx/test/svelte2tsx/samples/ts-export-list-runes.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
;
2121
async () => { { svelteHTML.createElement("svelte:options", {"runes":true,});}
2222
};
23-
return { props: {} as Record<string, never>, exports: {} as any as { name1: string,name2: string,name3: string,name4: string,renamed1: string,renamed2: string,Foo: typeof Foo,bar: typeof bar,baz: string,RenamedFoo: typeof RenameFoo,renamedbar: typeof renamebar,renamedbaz: string }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
23+
return { props: {} as Record<string, never>, exports: {Foo: Foo,bar: bar,RenamedFoo: RenameFoo,renamedbar: renamebar} as any as { name1: string,name2: string,name3: string,name4: string,renamed1: string,renamed2: string,Foo: typeof Foo,bar: typeof bar,baz: string,RenamedFoo: typeof RenameFoo,renamedbar: typeof renamebar,renamedbaz: string }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
2424
const Input__SvelteComponent_ = __sveltets_2_fn_component($$render());
2525
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
2626
export default Input__SvelteComponent_;

packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune.v5/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let { form, data }: $$ComponentProps = $props();
66
;
77
async () => {};
8-
return { props: {} as any as $$ComponentProps, exports: {} as any as { snapshot: typeof snapshot }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
8+
return { props: {} as any as $$ComponentProps, exports: {snapshot: snapshot} as any as { snapshot: typeof snapshot }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
99
const Page__SvelteComponent_ = __sveltets_2_fn_component($$render());
1010
type Page__SvelteComponent_ = ReturnType<typeof Page__SvelteComponent_>;
1111
export default Page__SvelteComponent_;

0 commit comments

Comments
 (0)