Skip to content

Commit

Permalink
fix: allow binding to const with spread in legacy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Oct 23, 2024
1 parent 86ef181 commit 0dd2125
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nine-pigs-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: allow binding to const with spread in legacy mode
13 changes: 13 additions & 0 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ const spread_props_handler = {
if (typeof p === 'object' && p !== null && key in p) return p[key];
}
},
set(target, key, value) {
let i = target.props.length;
while (i--) {
let p = target.props[i];
if (is_function(p)) p = p();
const desc = get_descriptor(p, key);
if (desc && desc.set) {
desc.set(value);
return true;
}
}
return false;
},
getOwnPropertyDescriptor(target, key) {
let i = target.props.length;
while (i--) {
Expand Down
7 changes: 6 additions & 1 deletion packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ export function spread_props(props) {
for (let i = 0; i < props.length; i++) {
const obj = props[i];
for (key in obj) {
merged_props[key] = obj[key];
const desc = Object.getOwnPropertyDescriptor(obj, key);
if (desc) {
Object.defineProperty(merged_props, key, desc);
} else {
merged_props[key] = obj[key];
}
}
}
return merged_props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export const x = 42;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
html: `<p>42</p>`,
async test({ target, assert }) {
const p = target.querySelector('p');
assert.equal(p?.innerHTML, '42');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Test from "./Test.svelte";
let x;
</script>

<Test
bind:x
{...{}}
/>
<p>{x}</p>

0 comments on commit 0dd2125

Please sign in to comment.