fix: allow runelike writable as prop (#11768)

Fixes #11742
pull/11793/head
Paolo Ricciuti 7 months ago committed by GitHub
parent d856c50092
commit 9084f1796b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: allow runelike writable as prop

@ -285,7 +285,9 @@ export function analyze_component(root, source, options) {
!Runes.includes(/** @type {any} */ (name)) || !Runes.includes(/** @type {any} */ (name)) ||
(declaration !== null && (declaration !== null &&
// const state = $state(0) is valid // const state = $state(0) is valid
get_rune(declaration.initial, instance.scope) === null && (get_rune(declaration.initial, instance.scope) === null ||
// rune-line names received as props are valid too (but we have to protect against $props as store)
(store_name !== 'props' && get_rune(declaration.initial, instance.scope) === '$props')) &&
// allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other // allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other
!( !(
name === '$derived' && name === '$derived' &&

@ -0,0 +1,18 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
test({ assert, target, ok }) {
const button = target.querySelector('button');
flushSync(() => {
button?.click();
});
assert.htmlEqual(
target.innerHTML,
`
<button>1</button>
`
);
}
});

@ -0,0 +1,5 @@
<script>
let { state } = $props();
</script>
<button onclick={()=> $state++}>{$state}</button>

@ -0,0 +1,8 @@
<svelte:options runes />
<script>
import { writable } from "svelte/store";
import Child from "./child.svelte";
const state = writable(0);
</script>
<Child {state} />

@ -0,0 +1,6 @@
<script>
let { state } = $props();
let x = $state();
</script>
{$state}

@ -0,0 +1,14 @@
[
{
"code": "store_rune_conflict",
"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": {
"line": 3,
"column": 9
},
"end": {
"line": 3,
"column": 15
}
}
]
Loading…
Cancel
Save