Skip to content

breaking: make non-bindable props read-only #12937

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

Closed
wants to merge 1 commit into from
Closed
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/two-ties-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

breaking: make non-bindable props read-only
2 changes: 2 additions & 0 deletions packages/svelte/messages/compile-errors/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

> Cannot assign to %thing%

> Cannot assign to %thing%. %suggestion%

## constant_binding

> Cannot bind to %thing%
Expand Down
7 changes: 4 additions & 3 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export function bindable_invalid_location(node) {
}

/**
* Cannot assign to %thing%
* Cannot assign to %thing%. %suggestion%
* @param {null | number | NodeLike} node
* @param {string} thing
* @param {string | undefined | null} [suggestion]
* @returns {never}
*/
export function constant_assignment(node, thing) {
e(node, "constant_assignment", `Cannot assign to ${thing}`);
export function constant_assignment(node, thing, suggestion) {
e(node, "constant_assignment", suggestion ? `Cannot assign to ${thing}. ${suggestion}` : `Cannot assign to ${thing}`);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export function validate_assignment(node, argument, state) {
e.constant_assignment(node, 'derived state');
}

if (binding?.kind === 'prop') {
e.constant_assignment(
node,
'a non-bindable prop',
'Use `$state.link(...)` to create a local copy of the value'
);
}

if (binding?.kind === 'each') {
e.each_item_invalid_assignment(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@
<button onclick={() => (non_bindable.count += 1)}>
mutate: {non_bindable.count}
</button>

<button onclick={() => (non_bindable = { count: non_bindable.count + 1 })}>
reassign: {non_bindable.count}
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ export default test({
<button>mutate: 0</button>
<button>reassign: 0</button>
<button>mutate: 0</button>
<button>reassign: 0</button>
`,

async test({ assert, target }) {
const [btn1, btn2, btn3, btn4] = target.querySelectorAll('button');
const [btn1, btn2, btn3] = target.querySelectorAll('button');

flushSync(() => {
btn1?.click();
Expand All @@ -22,7 +21,6 @@ export default test({
<button>mutate: 1</button>
<button>reassign: 1</button>
<button>mutate: 0</button>
<button>reassign: 0</button>
`
);

Expand All @@ -36,7 +34,6 @@ export default test({
<button>mutate: 2</button>
<button>reassign: 2</button>
<button>mutate: 0</button>
<button>reassign: 0</button>
`
);

Expand All @@ -50,7 +47,6 @@ export default test({
<button>mutate: 3</button>
<button>reassign: 3</button>
<button>mutate: 0</button>
<button>reassign: 0</button>
`
);

Expand All @@ -64,35 +60,6 @@ export default test({
<button>mutate: 3</button>
<button>reassign: 3</button>
<button>mutate: 0</button>
<button>reassign: 0</button>
`
);

flushSync(() => {
btn4?.click();
});

assert.htmlEqual(
target.innerHTML,
`
<button>mutate: 3</button>
<button>reassign: 3</button>
<button>mutate: 2</button>
<button>reassign: 2</button>
`
);

flushSync(() => {
btn3?.click();
});

assert.htmlEqual(
target.innerHTML,
`
<button>mutate: 3</button>
<button>reassign: 3</button>
<button>mutate: 2</button>
<button>reassign: 2</button>
`
);
}
Expand Down
Loading