fix: prevent false positive store error in module script (#10291)

When a variable with the same name was declared in the instance script, the module-no-auto-store-subscription-validation would fail
fixes #10285
pull/10299/head
Simon H 10 months ago committed by GitHub
parent 107ec1c848
commit 005ff894ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: prevent false positive store error in module script

@ -435,10 +435,6 @@ const errors = {
// code: 'illegal-declaration',
// message: 'The $ prefix is reserved, and cannot be used for variable and import names'
// },
// illegal_subscription: {
// code: 'illegal-subscription',
// message: 'Cannot reference store value inside <script context="module">'
// },
// illegal_global: /** @param {string} name */ (name) => ({
// code: 'illegal-global',
// message: `${name} is an illegal variable name`

@ -306,11 +306,13 @@ export function analyze_component(root, options) {
}
if (module.ast) {
// if the reference is inside context="module", error. this is a bit hacky but it works
for (const { node } of references) {
for (const { node, path } of references) {
// if the reference is inside context="module", error. this is a bit hacky but it works
if (
/** @type {number} */ (node.start) > /** @type {number} */ (module.ast.start) &&
/** @type {number} */ (node.end) < /** @type {number} */ (module.ast.end)
/** @type {number} */ (node.end) < /** @type {number} */ (module.ast.end) &&
// const state = $state(0) is valid
get_rune(/** @type {import('estree').Node} */ (path.at(-1)), module.scope) === null
) {
error(node, 'illegal-subscription');
}

@ -3,6 +3,7 @@ import { test } from '../../test';
export default test({
error: {
code: 'illegal-subscription',
message: 'Cannot reference store value inside <script context="module">'
message: 'Cannot reference store value inside <script context="module">',
position: [164, 168]
}
});

@ -1,6 +1,13 @@
<script context="module">
// this should be fine (state rune is not treated as a store)
const state = $state(0);
// this is not
const foo = {};
const answer = $foo;
</script>
<script>
let state;
</script>
<p>{answer}</p>
Loading…
Cancel
Save