Skip to content

Commit 13c6c27

Browse files
authored
fix: prevent false positives when detecting runes mode (#9599)
Move references from module scope to instance scope if we determined that these references are store subscriptions fixes #9580
1 parent f40efb2 commit 13c6c27

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

.changeset/fair-crabs-check.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: prevent false positives when detecting runes mode

packages/svelte/src/compiler/phases/2-analyze/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ export function analyze_component(root, options) {
284284
if (declaration === null && /[a-z]/.test(store_name[0])) {
285285
error(references[0].node, 'illegal-global', name);
286286
} else if (declaration !== null && Runes.includes(name)) {
287-
warn(warnings, declaration.node, [], 'store-with-rune-name', store_name);
287+
for (const { node, path } of references) {
288+
if (path.at(-1)?.type === 'CallExpression') {
289+
warn(warnings, node, [], 'store-with-rune-name', store_name);
290+
}
291+
}
288292
}
289293
}
290294

@@ -302,6 +306,8 @@ export function analyze_component(root, options) {
302306

303307
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
304308
binding.references = references;
309+
instance.scope.references.set(name, references);
310+
module.scope.references.delete(name);
305311
}
306312
}
307313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
export let initial;
5+
const state = writable(initial);
6+
</script>
7+
8+
<div>{$state}</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

packages/svelte/tests/validator/samples/store-runes-conflict/warnings.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"code": "store-with-rune-name",
44
"message": "It looks like you're using the $state rune, but there is a local binding called state. Referencing a local variable with a $ prefix will create a store subscription. Please rename state to avoid the ambiguity.",
55
"start": {
6-
"column": 7,
7-
"line": 2
6+
"column": 1,
7+
"line": 3
88
},
99
"end": {
10-
"column": 12,
11-
"line": 2
10+
"column": 7,
11+
"line": 3
1212
}
1313
}
1414
]

0 commit comments

Comments
 (0)