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

fix: don't hoist types referencing stores or destructured variables #2661

Merged
merged 3 commits into from
Jan 10, 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
2 changes: 1 addition & 1 deletion packages/svelte-vscode/prettier-options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"styles-options-scripts-markup",
"styles-scripts-markup-options",
"styles-scripts-options-markup",
"none",
"none"
]
},
"svelteStrictMode": {
Expand Down
28 changes: 25 additions & 3 deletions packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ export class HoistableInterfaces {
node.declarationList.declarations.forEach((declaration) => {
if (ts.isIdentifier(declaration.name)) {
this.disallowed_values.add(declaration.name.text);
} else {
const walk = (node: ts.Node) => {
if (
ts.isIdentifier(node) &&
ts.isBindingElement(node.parent) &&
node.parent.name === node
) {
this.disallowed_values.add(node.text);
}
ts.forEachChild(node, walk);
};

walk(declaration.name);
}
});
}
Expand Down Expand Up @@ -256,7 +269,7 @@ export class HoistableInterfaces {
}

for (const dep of deps.value_deps) {
if (this.disallowed_values.has(dep)) {
if (!this.isAllowedReference(dep)) {
this.disallowed_types.add(interface_name);
can_hoist = false;
break;
Expand All @@ -275,7 +288,7 @@ export class HoistableInterfaces {
...this.props_interface.type_deps,
...this.props_interface.value_deps
].every((dep) => {
return !this.disallowed_types.has(dep) && !this.disallowed_values.has(dep);
return !this.disallowed_types.has(dep) && this.isAllowedReference(dep);
});

if (can_hoist) {
Expand Down Expand Up @@ -333,7 +346,16 @@ export class HoistableInterfaces {
}

isAllowedReference(reference: string) {
return !this.disallowed_values.has(reference);
return !(
this.disallowed_values.has(reference) ||
reference === '$$props' ||
reference === '$$restProps' ||
reference === '$$slots' ||
// could be a $store reference
(reference[0] === '$' &&
reference[1] !== '$' &&
this.disallowed_values.has(reference.slice(1)))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
///<reference types="svelte" />
;function render() {

let store = null/*Ωignore_startΩ*/;let $store = __sveltets_2_store_get(store);/*Ωignore_endΩ*/;;type $$ComponentProps = { someProp: typeof $store };
let { someProp }:/*Ωignore_startΩ*/$$ComponentProps/*Ωignore_endΩ*/ = $props();
;
async () => {};
return { props: {} as any as $$ComponentProps, exports: {}, 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
@@ -0,0 +1,4 @@
<script lang="ts">
let store = null;
let { someProp }: { someProp: typeof $store } = $props();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///<reference types="svelte" />
;function render() {

let { destructured } = {};
type Props = {
prop: typeof destructured;
};
let { prop }: Props = $props();
;
async () => {};
return { props: {} as any as Props, exports: {}, 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
@@ -0,0 +1,7 @@
<script lang="ts">
let { destructured } = {};
type Props = {
prop: typeof destructured;
};
let { prop }: Props = $props();
</script>
Loading