From 7cbd188f801aff718111172d272156c1e8b5e3be Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 12 Aug 2024 12:27:10 -0400 Subject: [PATCH] breaking: replace `$state.frozen` with `$state.raw` (#12808) * breaking: replace `$state.frozen` with `$state.raw` * regenerate * rename * rename * rename * rename * rename * rename * rename * rename * rename * typo * add compiler error for existing `$state.frozen` uses * regenerate * rename * tidy up * move proxy logic into props function --- .changeset/four-yaks-boil.md | 5 +++ .../svelte/messages/client-errors/errors.md | 4 -- packages/svelte/src/ambient.d.ts | 9 +++-- .../2-analyze/visitors/BindDirective.js | 2 +- .../2-analyze/visitors/CallExpression.js | 2 +- .../visitors/ExportNamedDeclaration.js | 2 +- .../2-analyze/visitors/ExportSpecifier.js | 4 +- .../phases/2-analyze/visitors/Identifier.js | 10 +++-- .../2-analyze/visitors/VariableDeclarator.js | 6 +-- .../phases/2-analyze/visitors/shared/utils.js | 2 +- .../3-transform/client/transform-client.js | 16 ++------ .../phases/3-transform/client/types.d.ts | 2 +- .../phases/3-transform/client/utils.js | 13 ++++-- .../client/visitors/AssignmentExpression.js | 22 +++++----- .../3-transform/client/visitors/ClassBody.js | 28 +++++-------- .../3-transform/client/visitors/EachBlock.js | 23 ++++++++--- .../client/visitors/VariableDeclaration.js | 17 +++----- .../client/visitors/shared/declarations.js | 4 +- .../server/visitors/PropertyDefinition.js | 2 +- packages/svelte/src/compiler/types/index.d.ts | 2 +- packages/svelte/src/constants.js | 5 ++- .../svelte/src/internal/client/constants.js | 1 - .../src/internal/client/dom/blocks/each.js | 19 ++------- packages/svelte/src/internal/client/errors.js | 16 -------- packages/svelte/src/internal/client/freeze.js | 40 ------------------- .../svelte/src/internal/client/freeze.test.ts | 16 -------- packages/svelte/src/internal/client/index.js | 1 - packages/svelte/src/internal/client/proxy.js | 9 +---- .../src/internal/client/reactivity/props.js | 5 ++- packages/svelte/src/utils.js | 2 +- .../ComponentA.svelte | 0 .../ComponentB.svelte | 0 .../_config.js | 0 .../main.svelte | 2 +- .../binding-property-static/_config.js | 4 +- .../binding-property-static/main.svelte | 6 +-- .../class-frozen-state-object/main.svelte | 14 ------- .../_config.js | 4 +- .../main.svelte | 14 +++---- .../_config.js | 0 .../main.svelte | 2 +- .../_config.js | 4 +- .../class-raw-state-object/main.svelte | 12 ++++++ .../_config.js | 0 .../main.svelte | 2 +- .../_config.js | 0 .../main.svelte | 0 .../samples/each-updates-frozen/main.svelte | 16 -------- .../_config.js | 0 .../samples/each-updates-raw/main.svelte | 18 +++++++++ .../readonly-state-replace/main.svelte | 2 +- .../samples/readonly-state/main.svelte | 4 +- .../samples/state-raw-bindable/Child.svelte | 6 +++ .../samples/state-raw-bindable/_config.js | 20 ++++++++++ .../samples/state-raw-bindable/main.svelte | 8 ++++ packages/svelte/types/index.d.ts | 11 ++--- 56 files changed, 187 insertions(+), 251 deletions(-) create mode 100644 .changeset/four-yaks-boil.md delete mode 100644 packages/svelte/src/internal/client/freeze.js delete mode 100644 packages/svelte/src/internal/client/freeze.test.ts rename packages/svelte/tests/runtime-runes/samples/{bind-this-frozen => bind-this-raw}/ComponentA.svelte (100%) rename packages/svelte/tests/runtime-runes/samples/{bind-this-frozen => bind-this-raw}/ComponentB.svelte (100%) rename packages/svelte/tests/runtime-runes/samples/{bind-this-frozen => bind-this-raw}/_config.js (100%) rename packages/svelte/tests/runtime-runes/samples/{bind-this-frozen => bind-this-raw}/main.svelte (93%) delete mode 100644 packages/svelte/tests/runtime-runes/samples/class-frozen-state-object/main.svelte rename packages/svelte/tests/runtime-runes/samples/{class-private-frozen-state-object => class-private-raw-state-object}/_config.js (80%) rename packages/svelte/tests/runtime-runes/samples/{class-private-frozen-state-object => class-private-raw-state-object}/main.svelte (60%) rename packages/svelte/tests/runtime-runes/samples/{class-frozen-state => class-private-raw-state}/_config.js (100%) rename packages/svelte/tests/runtime-runes/samples/{class-private-frozen-state => class-private-raw-state}/main.svelte (91%) rename packages/svelte/tests/runtime-runes/samples/{class-frozen-state-object => class-raw-state-object}/_config.js (80%) create mode 100644 packages/svelte/tests/runtime-runes/samples/class-raw-state-object/main.svelte rename packages/svelte/tests/runtime-runes/samples/{class-private-frozen-state => class-raw-state}/_config.js (100%) rename packages/svelte/tests/runtime-runes/samples/{class-frozen-state => class-raw-state}/main.svelte (83%) rename packages/svelte/tests/runtime-runes/samples/{class-state-frozen => class-state-raw}/_config.js (100%) rename packages/svelte/tests/runtime-runes/samples/{class-state-frozen => class-state-raw}/main.svelte (100%) delete mode 100644 packages/svelte/tests/runtime-runes/samples/each-updates-frozen/main.svelte rename packages/svelte/tests/runtime-runes/samples/{each-updates-frozen => each-updates-raw}/_config.js (100%) create mode 100644 packages/svelte/tests/runtime-runes/samples/each-updates-raw/main.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/state-raw-bindable/Child.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/state-raw-bindable/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/state-raw-bindable/main.svelte diff --git a/.changeset/four-yaks-boil.md b/.changeset/four-yaks-boil.md new file mode 100644 index 0000000000..ede6c1de19 --- /dev/null +++ b/.changeset/four-yaks-boil.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +breaking: replace `$state.frozen` with `$state.raw` diff --git a/packages/svelte/messages/client-errors/errors.md b/packages/svelte/messages/client-errors/errors.md index f0ca476ff1..e509c970a2 100644 --- a/packages/svelte/messages/client-errors/errors.md +++ b/packages/svelte/messages/client-errors/errors.md @@ -64,10 +64,6 @@ > The `%rune%` rune is only available inside `.svelte` and `.svelte.js/ts` files -## state_frozen_invalid_argument - -> The argument to `$state.frozen(...)` cannot be an object created with `$state(...)`. You should create a copy of it first, for example with `$state.snapshot` - ## state_prototype_fixed > Cannot set prototype of `$state` object diff --git a/packages/svelte/src/ambient.d.ts b/packages/svelte/src/ambient.d.ts index 63a3873d3f..82242cb494 100644 --- a/packages/svelte/src/ambient.d.ts +++ b/packages/svelte/src/ambient.d.ts @@ -102,12 +102,13 @@ declare namespace $state { : never; /** - * Declares reactive read-only state that is shallowly immutable. + * Declares state that is _not_ made deeply reactive — instead of mutating it, + * you must reassign it. * * Example: * ```ts * - - diff --git a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/_config.js b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/_config.js similarity index 80% rename from packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/_config.js rename to packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/_config.js index 4b043a75e2..ad93c66761 100644 --- a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/_config.js @@ -4,7 +4,7 @@ import { test } from '../../test'; export default test({ html: ``, - test({ assert, target, logs }) { + test({ assert, target }) { const btn = target.querySelector('button'); btn?.click(); @@ -14,7 +14,5 @@ export default test({ btn?.click(); flushSync(); assert.htmlEqual(target.innerHTML, ``); - - assert.deepEqual(logs, ['read only', 'read only']); } }); diff --git a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/main.svelte b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/main.svelte similarity index 60% rename from packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/main.svelte rename to packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/main.svelte index 557dc86998..f7f1c74c29 100644 --- a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state-object/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state-object/main.svelte @@ -1,6 +1,6 @@ - + diff --git a/packages/svelte/tests/runtime-runes/samples/class-frozen-state/_config.js b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state/_config.js similarity index 100% rename from packages/svelte/tests/runtime-runes/samples/class-frozen-state/_config.js rename to packages/svelte/tests/runtime-runes/samples/class-private-raw-state/_config.js diff --git a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state/main.svelte b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state/main.svelte similarity index 91% rename from packages/svelte/tests/runtime-runes/samples/class-private-frozen-state/main.svelte rename to packages/svelte/tests/runtime-runes/samples/class-private-raw-state/main.svelte index f509f351bb..f4812b1dde 100644 --- a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/class-private-raw-state/main.svelte @@ -1,6 +1,6 @@ + + diff --git a/packages/svelte/tests/runtime-runes/samples/class-private-frozen-state/_config.js b/packages/svelte/tests/runtime-runes/samples/class-raw-state/_config.js similarity index 100% rename from packages/svelte/tests/runtime-runes/samples/class-private-frozen-state/_config.js rename to packages/svelte/tests/runtime-runes/samples/class-raw-state/_config.js diff --git a/packages/svelte/tests/runtime-runes/samples/class-frozen-state/main.svelte b/packages/svelte/tests/runtime-runes/samples/class-raw-state/main.svelte similarity index 83% rename from packages/svelte/tests/runtime-runes/samples/class-frozen-state/main.svelte rename to packages/svelte/tests/runtime-runes/samples/class-raw-state/main.svelte index a723976ea9..2913d1ecec 100644 --- a/packages/svelte/tests/runtime-runes/samples/class-frozen-state/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/class-raw-state/main.svelte @@ -1,6 +1,6 @@ diff --git a/packages/svelte/tests/runtime-runes/samples/class-state-frozen/_config.js b/packages/svelte/tests/runtime-runes/samples/class-state-raw/_config.js similarity index 100% rename from packages/svelte/tests/runtime-runes/samples/class-state-frozen/_config.js rename to packages/svelte/tests/runtime-runes/samples/class-state-raw/_config.js diff --git a/packages/svelte/tests/runtime-runes/samples/class-state-frozen/main.svelte b/packages/svelte/tests/runtime-runes/samples/class-state-raw/main.svelte similarity index 100% rename from packages/svelte/tests/runtime-runes/samples/class-state-frozen/main.svelte rename to packages/svelte/tests/runtime-runes/samples/class-state-raw/main.svelte diff --git a/packages/svelte/tests/runtime-runes/samples/each-updates-frozen/main.svelte b/packages/svelte/tests/runtime-runes/samples/each-updates-frozen/main.svelte deleted file mode 100644 index 8aabde46cf..0000000000 --- a/packages/svelte/tests/runtime-runes/samples/each-updates-frozen/main.svelte +++ /dev/null @@ -1,16 +0,0 @@ - - -{#each frozen_items as item (item.id)} - {console.log(item.text)} - {item.text} -{/each} - - diff --git a/packages/svelte/tests/runtime-runes/samples/each-updates-frozen/_config.js b/packages/svelte/tests/runtime-runes/samples/each-updates-raw/_config.js similarity index 100% rename from packages/svelte/tests/runtime-runes/samples/each-updates-frozen/_config.js rename to packages/svelte/tests/runtime-runes/samples/each-updates-raw/_config.js diff --git a/packages/svelte/tests/runtime-runes/samples/each-updates-raw/main.svelte b/packages/svelte/tests/runtime-runes/samples/each-updates-raw/main.svelte new file mode 100644 index 0000000000..07efc5e349 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/each-updates-raw/main.svelte @@ -0,0 +1,18 @@ + + +{#each raw_items as item (item.id)} + {console.log(item.text)} + {item.text} +{/each} + + diff --git a/packages/svelte/tests/runtime-runes/samples/readonly-state-replace/main.svelte b/packages/svelte/tests/runtime-runes/samples/readonly-state-replace/main.svelte index 4a53807030..4b5dc5ac59 100644 --- a/packages/svelte/tests/runtime-runes/samples/readonly-state-replace/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/readonly-state-replace/main.svelte @@ -1,5 +1,5 @@ + + + diff --git a/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/_config.js b/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/_config.js new file mode 100644 index 0000000000..995aec0ad8 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/_config.js @@ -0,0 +1,20 @@ +import { flushSync } from 'svelte'; +import { ok, test } from '../../test'; + +export default test({ + html: '

0

', + test({ assert, target }) { + const [reassign, mutate] = target.querySelectorAll('button'); + const output = target.querySelector('p'); + ok(output); + + flushSync(() => mutate.click()); + assert.htmlEqual(output.innerHTML, '0'); + + flushSync(() => reassign.click()); + assert.htmlEqual(output.innerHTML, '2'); + + flushSync(() => mutate.click()); + assert.htmlEqual(output.innerHTML, '2'); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/main.svelte b/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/main.svelte new file mode 100644 index 0000000000..39599673fc --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/state-raw-bindable/main.svelte @@ -0,0 +1,8 @@ + + + +

{object.count}

diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index ed61515c5a..f31cb2a6d3 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -927,7 +927,7 @@ declare module 'svelte/compiler' { | 'bindable_prop' | 'rest_prop' | 'state' - | 'frozen_state' + | 'raw_state' | 'derived' | 'each' | 'snippet' @@ -2879,12 +2879,13 @@ declare namespace $state { : never; /** - * Declares reactive read-only state that is shallowly immutable. + * Declares state that is _not_ made deeply reactive — instead of mutating it, + * you must reassign it. * * Example: * ```ts *