Repository: sveltejs/svelte (JavaScript)
Date: 2021-07-14 01:47:43+00:00
Task type: Bug Fix

Issue:
Destructuring into a store without rename syntax results in undefined behavior
**Describe the bug**
Destructuring into a svelte store directly, without typing the prop name in a rename syntax results in undefined behavior.
```js
import {writable} from 'svelte/store'
let userName1 = writable('')
let userName2 = writable('')
let userName3 = writable('')
let obj = {userName1: 'user1', userName2: 'user2', userName3: 'user3'}
;({userName1: $userName1, $userName2 } = obj)
;({$userName3} = obj)
// $userName1 === 'user1'
// $userName2 === undefined
// $userName3 === obj
```

**To Reproduce**
<https://svelte.dev/repl/1e4f6ca9cf9c4f60ab48f40c5cf73d2a?version=3.29.4>

**Expected behavior**
`$userName1` works as expected.
`$userName2` and `$userName3` are erroneous. I am not sure what behavior should be expected here, but what occurs is certainly not it. We could 

1) throw an error (possibly at compile time).
2) Attempt to destructure the prop with the same name. That is, `$userName3` should look for a prop named `$userName3` and get undefined (matching what appears to happen to userName2). This is a no magic solution following standard js behavior.
3) Attempt to access the prop with the same name as the store, without the dollar sign. So `$userName2` and `$userName3` would get `obj.userName2` and `obj.userName3` respectively. This is a magic solution that makes sense combined with the inherit magic of stores.

**Severity**
Low. I think attempting to destructure in any way other than the way I did with `$userName1` is asking for trouble. I have no intention of trying to de-structure into stores any other way, if I destructure into stores at all.  

**Additional context**
Found due to question asked by [OwnageJuice](https://discord.com/channels/457912077277855764/457912077277855766/774541807849570305)



Issue discussion:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Solution patch:
diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts
--- a/src/runtime/internal/utils.ts
+++ b/src/runtime/internal/utils.ts
@@ -159,7 +159,7 @@ export function null_to_empty(value) {
 	return value == null ? '' : value;
 }
 
-export function set_store_value(store, ret, value = ret) {
+export function set_store_value(store, ret, value) {
 	store.set(value);
 	return ret;
 }


Test patch:
diff --git a/test/runtime/samples/store-assignment-updates-destructure/_config.js b/test/runtime/samples/store-assignment-updates-destructure/_config.js
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/store-assignment-updates-destructure/_config.js
@@ -0,0 +1,11 @@
+export default {
+	html: `
+		<div>$userName1: user1</div>
+		<div>$userName2: undefined</div>
+		<div>$userName3: undefined</div>
+		<div>$userName4: user4</div>
+		<div>$userName5: undefined</div>
+		<div>$userName6: user6</div>
+		<div>$userName7: undefined</div>
+	`
+};
diff --git a/test/runtime/samples/store-assignment-updates-destructure/main.svelte b/test/runtime/samples/store-assignment-updates-destructure/main.svelte
new file mode 100644
--- /dev/null
+++ b/test/runtime/samples/store-assignment-updates-destructure/main.svelte
@@ -0,0 +1,34 @@
+<script>
+	import { writable } from 'svelte/store';
+
+	let userName1 = writable('init1');
+	let userName2 = writable('init2');
+	let userName3 = writable('init3');
+	let userName4 = writable('init4');
+	let userName5 = writable('init5');
+	let userName6 = writable('init6');
+	let userName7 = writable('init7');
+	
+	let obj = {
+		userName1: 'user1', 
+		userName2: 'user2', 
+		userName3: 'user3', 
+		$userName4: 'user4',
+		userName5: 'user5',
+		$userName6: 'user6',
+		userName7: 'user7',
+	};
+	
+	({userName1: $userName1, $userName2 } = obj);
+	({$userName3} = obj);
+	({$userName4} = obj);
+	({$userName5, $userName6, $userName7} = obj);
+</script>
+
+<div>$userName1: {$userName1}</div>
+<div>$userName2: {$userName2}</div>
+<div>$userName3: {$userName3}</div>
+<div>$userName4: {$userName4}</div>
+<div>$userName5: {$userName5}</div>
+<div>$userName6: {$userName6}</div>
+<div>$userName7: {$userName7}</div>