Skip to content

Commit

Permalink
fix: prevent false positives when detecting runes mode (#9599)
Browse files Browse the repository at this point in the history
Move references from module scope to instance scope if we determined that these references are store subscriptions
fixes #9580
  • Loading branch information
dummdidumm authored Nov 22, 2023
1 parent f40efb2 commit 13c6c27
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-crabs-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent false positives when detecting runes mode
8 changes: 7 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { writable } from 'svelte/store';
export let initial;
const state = writable(initial);
</script>

<div>{$state}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]

1 comment on commit 13c6c27

@vercel
Copy link

@vercel vercel bot commented on 13c6c27 Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview-svelte.vercel.app
svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.