Skip to content

fix: ensure exports in runes mode are marked as used #2719

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

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 17 additions & 14 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,18 +758,17 @@ export class ExportedNames {
let str = '';

if (others.length > 0 || this.usesRunes() || needsAccessors) {
const exports = needsAccessors ? names : others;

if (others.length > 0 || needsAccessors) {
if (this.isTsFile) {
str +=
', exports: {} as any as { ' +
this.createReturnElementsType(
needsAccessors ? names : others,
undefined,
true
).join(',') +
// Reference imports that have a type, else they are marked as unused if nothing in the component references them
`, exports: {${this.createReturnElements(this.usesRunes() ? others : [], false, true)}} as any as { ` +
this.createReturnElementsType(exports, undefined, true).join(',') +
' }';
} else {
str += `, exports: /** @type {{${this.createReturnElementsType(needsAccessors ? names : others, false, true)}}} */ ({})`;
str += `, exports: /** @type {{${this.createReturnElementsType(exports, false, true)}}} */ ({})`;
}
} else {
// Always add that, in TS5.5+ the type for Exports is infered to never when this is not present, which breaks types.
Expand All @@ -791,14 +790,18 @@ export class ExportedNames {

private createReturnElements(
names: Array<[string, ExportedName]>,
dontAddTypeDef: boolean
dontAddTypeDef: boolean,
omitTyped = false
): string[] {
return names.map(([key, value]) => {
// Important to not use shorthand props for rename functionality
return `${dontAddTypeDef && value.doc ? `\n${value.doc}` : ''}${
value.identifierText || key
}: ${key}`;
});
return names
.map(([key, value]) => {
if (omitTyped && value.type) return;
// Important to not use shorthand props for rename functionality
return `${dontAddTypeDef && value.doc ? `\n${value.doc}` : ''}${
value.identifierText || key
}: ${key}`;
})
.filter(Boolean);
}

private createReturnElementsType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
;
async () => { { svelteHTML.createElement("svelte:options", {"runes":true,});}
};
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: {} }}
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: {} }}
const Input__SvelteComponent_ = __sveltets_2_fn_component($$render());
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
export default Input__SvelteComponent_;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let { form, data }: $$ComponentProps = $props();
;
async () => {};
return { props: {} as any as $$ComponentProps, exports: {} as any as { snapshot: typeof snapshot }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
return { props: {} as any as $$ComponentProps, exports: {snapshot: snapshot} as any as { snapshot: typeof snapshot }, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
const Page__SvelteComponent_ = __sveltets_2_fn_component($$render());
type Page__SvelteComponent_ = ReturnType<typeof Page__SvelteComponent_>;
export default Page__SvelteComponent_;