Skip to content

Commit 5312279

Browse files
authored
fix: prevent false positive store declarations (#2422)
variable declarations not at the top level can't be subscribed to using the `$` prefix
1 parent 6e64abd commit 5312279

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ export function processInstanceScriptContent(
203203
if (ts.isVariableDeclaration(node)) {
204204
events.checkIfIsStringLiteralDeclaration(node);
205205
events.checkIfDeclarationInstantiatedEventDispatcher(node);
206-
implicitStoreValues.addVariableDeclaration(node);
206+
// Only top level declarations can be stores
207+
if (node.parent?.parent?.parent === tsAst) {
208+
implicitStoreValues.addVariableDeclaration(node);
209+
}
207210
}
208211

209212
if (ts.isCallExpression(node)) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference types="svelte" />
2+
;function render() {
3+
4+
function x(tr) {
5+
for (let notAStore of tr.effects) {}
6+
}
7+
8+
$notAStore;
9+
;
10+
async () => {};
11+
return { props: /** @type {Record<string, never>} */ ({}), slots: {}, events: {} }}
12+
13+
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
function x(tr) {
3+
for (let notAStore of tr.effects) {}
4+
}
5+
6+
$notAStore;
7+
</script>

0 commit comments

Comments
 (0)