Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-mugs-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: prevent false positive store error in module script
4 changes: 0 additions & 4 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 5 additions & 3 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
});
Original file line number Diff line number Diff line change
@@ -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>