Skip to content

Commit 7f28165

Browse files
committed
fix: prevent false positive store error in module script
When a variable with the same name was declared in the instance script, the module-no-auto-store-subscription-validation would fail fixes #10285
1 parent 107ec1c commit 7f28165

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

.changeset/tall-mugs-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: prevent false positive store error in module script

packages/svelte/src/compiler/errors.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,6 @@ const errors = {
435435
// code: 'illegal-declaration',
436436
// message: 'The $ prefix is reserved, and cannot be used for variable and import names'
437437
// },
438-
// illegal_subscription: {
439-
// code: 'illegal-subscription',
440-
// message: 'Cannot reference store value inside <script context="module">'
441-
// },
442438
// illegal_global: /** @param {string} name */ (name) => ({
443439
// code: 'illegal-global',
444440
// message: `${name} is an illegal variable name`

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,13 @@ export function analyze_component(root, options) {
306306
}
307307

308308
if (module.ast) {
309-
// if the reference is inside context="module", error. this is a bit hacky but it works
310-
for (const { node } of references) {
309+
for (const { node, path } of references) {
310+
// if the reference is inside context="module", error. this is a bit hacky but it works
311311
if (
312312
/** @type {number} */ (node.start) > /** @type {number} */ (module.ast.start) &&
313-
/** @type {number} */ (node.end) < /** @type {number} */ (module.ast.end)
313+
/** @type {number} */ (node.end) < /** @type {number} */ (module.ast.end) &&
314+
// const state = $state(0) is valid
315+
get_rune(/** @type {import('estree').Node} */ (path.at(-1)), module.scope) === null
314316
) {
315317
error(node, 'illegal-subscription');
316318
}

packages/svelte/tests/compiler-errors/samples/store-autosub-context-module/_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { test } from '../../test';
33
export default test({
44
error: {
55
code: 'illegal-subscription',
6-
message: 'Cannot reference store value inside <script context="module">'
6+
message: 'Cannot reference store value inside <script context="module">',
7+
position: [164, 168]
78
}
89
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<script context="module">
2+
// this should be fine (state rune is not treated as a store)
3+
const state = $state(0);
4+
// this is not
25
const foo = {};
36
const answer = $foo;
47
</script>
58

9+
<script>
10+
let state;
11+
</script>
12+
613
<p>{answer}</p>

0 commit comments

Comments
 (0)