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
pull/9602/head
Simon H 2 years ago committed by GitHub
parent f40efb2027
commit 13c6c273c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: prevent false positives when detecting runes mode

@ -284,7 +284,11 @@ export function analyze_component(root, options) {
if (declaration === null && /[a-z]/.test(store_name[0])) {
error(references[0].node, 'illegal-global', name);
} else if (declaration !== null && Runes.includes(name)) {
warn(warnings, declaration.node, [], 'store-with-rune-name', store_name);
for (const { node, path } of references) {
if (path.at(-1)?.type === 'CallExpression') {
warn(warnings, node, [], 'store-with-rune-name', store_name);
}
}
}
}
@ -302,6 +306,8 @@ export function analyze_component(root, options) {
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
binding.references = references;
instance.scope.references.set(name, references);
module.scope.references.delete(name);
}
}

@ -0,0 +1,8 @@
<script>
import { writable } from 'svelte/store';
export let initial;
const state = writable(initial);
</script>
<div>{$state}</div>

@ -3,12 +3,12 @@
"code": "store-with-rune-name",
"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.",
"start": {
"column": 7,
"line": 2
"column": 1,
"line": 3
},
"end": {
"column": 12,
"line": 2
"column": 7,
"line": 3
}
}
]

Loading…
Cancel
Save