Skip to content

Commit 2f46701

Browse files
authored
fix: don't hoist types referencing stores or destructured variables (#2661)
#2659 #2660
1 parent 90c9953 commit 2f46701

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

packages/svelte-vscode/prettier-options-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"styles-options-scripts-markup",
3434
"styles-scripts-markup-options",
3535
"styles-scripts-options-markup",
36-
"none",
36+
"none"
3737
]
3838
},
3939
"svelteStrictMode": {

packages/svelte2tsx/src/svelte2tsx/nodes/HoistableInterfaces.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ export class HoistableInterfaces {
179179
node.declarationList.declarations.forEach((declaration) => {
180180
if (ts.isIdentifier(declaration.name)) {
181181
this.disallowed_values.add(declaration.name.text);
182+
} else {
183+
const walk = (node: ts.Node) => {
184+
if (
185+
ts.isIdentifier(node) &&
186+
ts.isBindingElement(node.parent) &&
187+
node.parent.name === node
188+
) {
189+
this.disallowed_values.add(node.text);
190+
}
191+
ts.forEachChild(node, walk);
192+
};
193+
194+
walk(declaration.name);
182195
}
183196
});
184197
}
@@ -256,7 +269,7 @@ export class HoistableInterfaces {
256269
}
257270

258271
for (const dep of deps.value_deps) {
259-
if (this.disallowed_values.has(dep)) {
272+
if (!this.isAllowedReference(dep)) {
260273
this.disallowed_types.add(interface_name);
261274
can_hoist = false;
262275
break;
@@ -275,7 +288,7 @@ export class HoistableInterfaces {
275288
...this.props_interface.type_deps,
276289
...this.props_interface.value_deps
277290
].every((dep) => {
278-
return !this.disallowed_types.has(dep) && !this.disallowed_values.has(dep);
291+
return !this.disallowed_types.has(dep) && this.isAllowedReference(dep);
279292
});
280293

281294
if (can_hoist) {
@@ -333,7 +346,16 @@ export class HoistableInterfaces {
333346
}
334347

335348
isAllowedReference(reference: string) {
336-
return !this.disallowed_values.has(reference);
349+
return !(
350+
this.disallowed_values.has(reference) ||
351+
reference === '$$props' ||
352+
reference === '$$restProps' ||
353+
reference === '$$slots' ||
354+
// could be a $store reference
355+
(reference[0] === '$' &&
356+
reference[1] !== '$' &&
357+
this.disallowed_values.has(reference.slice(1)))
358+
);
337359
}
338360

339361
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
///<reference types="svelte" />
2+
;function render() {
3+
4+
let store = null/*Ωignore_startΩ*/;let $store = __sveltets_2_store_get(store);/*Ωignore_endΩ*/;;type $$ComponentProps = { someProp: typeof $store };
5+
let { someProp }:/*Ωignore_startΩ*/$$ComponentProps/*Ωignore_endΩ*/ = $props();
6+
;
7+
async () => {};
8+
return { props: {} as any as $$ComponentProps, exports: {}, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
9+
const Input__SvelteComponent_ = __sveltets_2_fn_component(render());
10+
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
11+
export default Input__SvelteComponent_;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script lang="ts">
2+
let store = null;
3+
let { someProp }: { someProp: typeof $store } = $props();
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference types="svelte" />
2+
;function render() {
3+
4+
let { destructured } = {};
5+
type Props = {
6+
prop: typeof destructured;
7+
};
8+
let { prop }: Props = $props();
9+
;
10+
async () => {};
11+
return { props: {} as any as Props, exports: {}, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
12+
const Input__SvelteComponent_ = __sveltets_2_fn_component(render());
13+
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
14+
export default Input__SvelteComponent_;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
let { destructured } = {};
3+
type Props = {
4+
prop: typeof destructured;
5+
};
6+
let { prop }: Props = $props();
7+
</script>

0 commit comments

Comments
 (0)