Skip to content
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

fix: allow binding to const with spread in legacy mode #13849

Merged
merged 1 commit into from
Oct 24, 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/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>