Skip to content

Commit 9612f95

Browse files
committed
$state.frozen -> $state.raw
1 parent 30b143c commit 9612f95

File tree

29 files changed

+69
-189
lines changed

29 files changed

+69
-189
lines changed

packages/svelte/messages/client-errors/errors.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@
6060

6161
> The `%rune%` rune is only available inside `.svelte` and `.svelte.js/ts` files
6262
63-
## state_frozen_invalid_argument
64-
65-
> 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`
66-
6763
## state_prototype_fixed
6864

6965
> Cannot set prototype of `$state` object

packages/svelte/src/ambient.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ declare namespace $state {
102102
: never;
103103

104104
/**
105-
* Declares reactive read-only state that is shallowly immutable.
105+
* Declares state that is _not_ made deeply reactive — instead of mutating it,
106+
* you must reassign it.
106107
*
107108
* Example:
108109
* ```ts
109110
* <script>
110-
* let items = $state.frozen([0]);
111+
* let items = $state.raw([0]);
111112
*
112113
* const addItem = () => {
113114
* items = [...items, items.length];
@@ -123,8 +124,8 @@ declare namespace $state {
123124
*
124125
* @param initial The initial value
125126
*/
126-
export function frozen<T>(initial: T): Readonly<T>;
127-
export function frozen<T>(): Readonly<T> | undefined;
127+
export function raw<T>(initial: T): T;
128+
export function raw<T>(): T | undefined;
128129
/**
129130
* To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:
130131
*

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -909,19 +909,14 @@ const runes_scope_js_tweaker = {
909909
const callee = node.init.callee;
910910
if (callee.type !== 'Identifier' && callee.type !== 'MemberExpression') return;
911911

912-
if (
913-
rune !== '$state' &&
914-
rune !== '$state.frozen' &&
915-
rune !== '$derived' &&
916-
rune !== '$derived.by'
917-
)
912+
if (rune !== '$state' && rune !== '$state.raw' && rune !== '$derived' && rune !== '$derived.by')
918913
return;
919914

920915
for (const path of extract_paths(node.id)) {
921916
// @ts-ignore this fails in CI for some insane reason
922917
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(path.node.name));
923918
binding.kind =
924-
rune === '$state' ? 'state' : rune === '$state.frozen' ? 'frozen_state' : 'derived';
919+
rune === '$state' ? 'state' : rune === '$state.raw' ? 'frozen_state' : 'derived';
925920
}
926921
}
927922
};
@@ -947,7 +942,7 @@ const runes_scope_tweaker = {
947942

948943
if (
949944
rune !== '$state' &&
950-
rune !== '$state.frozen' &&
945+
rune !== '$state.raw' &&
951946
rune !== '$derived' &&
952947
rune !== '$derived.by' &&
953948
rune !== '$props'
@@ -960,7 +955,7 @@ const runes_scope_tweaker = {
960955
binding.kind =
961956
rune === '$state'
962957
? 'state'
963-
: rune === '$state.frozen'
958+
: rune === '$state.raw'
964959
? 'frozen_state'
965960
: rune === '$derived' || rune === '$derived.by'
966961
? 'derived'

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,7 @@ function validate_call_expression(node, scope, path) {
903903
e.bindable_invalid_location(node);
904904
}
905905

906-
if (
907-
rune === '$state' ||
908-
rune === '$state.frozen' ||
909-
rune === '$derived' ||
910-
rune === '$derived.by'
911-
) {
906+
if (rune === '$state' || rune === '$state.raw' || rune === '$derived' || rune === '$derived.by') {
912907
if (parent.type === 'VariableDeclarator') return;
913908
if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
914909
e.state_invalid_placement(node, rune);

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,8 @@ export function client_component(source, analysis, options) {
215215
}
216216

217217
if (binding?.kind === 'state' || binding?.kind === 'frozen_state') {
218-
return [
219-
getter,
220-
b.set(alias ?? name, [
221-
b.stmt(
222-
b.call(
223-
'$.set',
224-
b.id(name),
225-
b.call(binding.kind === 'state' ? '$.proxy' : '$.freeze', b.id('$$value'))
226-
)
227-
)
228-
])
229-
];
218+
const value = binding.kind === 'state' ? b.call('$.proxy', b.id('$$value')) : b.id('$$value');
219+
return [getter, b.set(alias ?? name, [b.stmt(b.call('$.set', b.id(name), value))])];
230220
}
231221

232222
return getter;

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
206206
if (assignment.type === 'AssignmentExpression') {
207207
assignment.right =
208208
private_state.kind === 'frozen_state'
209-
? b.call('$.freeze', value)
209+
? value
210210
: serialize_proxy_reassignment(value, private_state.id, state);
211211
return assignment;
212212
}
@@ -219,7 +219,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
219219
!options?.skip_proxy_and_freeze &&
220220
should_proxy_or_freeze(value, context.state.scope)
221221
? private_state.kind === 'frozen_state'
222-
? b.call('$.freeze', value)
222+
? value
223223
: serialize_proxy_reassignment(value, private_state.id, state)
224224
: value
225225
);
@@ -243,7 +243,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
243243
if (assignment.type === 'AssignmentExpression') {
244244
assignment.right =
245245
public_state.kind === 'frozen_state'
246-
? b.call('$.freeze', value)
246+
? value
247247
: serialize_proxy_reassignment(value, public_state.id, state);
248248
return assignment;
249249
}
@@ -325,7 +325,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
325325
context.state.analysis.runes &&
326326
!options?.skip_proxy_and_freeze &&
327327
should_proxy_or_freeze(value, context.state.scope)
328-
? b.call('$.freeze', value)
328+
? value
329329
: value
330330
);
331331
} else if (

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const javascript_visitors_runes = {
4242
const rune = get_rune(definition.value, state.scope);
4343
if (
4444
rune === '$state' ||
45-
rune === '$state.frozen' ||
45+
rune === '$state.raw' ||
4646
rune === '$derived' ||
4747
rune === '$derived.by'
4848
) {
@@ -51,7 +51,7 @@ export const javascript_visitors_runes = {
5151
kind:
5252
rune === '$state'
5353
? 'state'
54-
: rune === '$state.frozen'
54+
: rune === '$state.raw'
5555
? 'frozen_state'
5656
: rune === '$derived.by'
5757
? 'derived_call'
@@ -115,10 +115,7 @@ export const javascript_visitors_runes = {
115115
should_proxy_or_freeze(init, state.scope) ? b.call('$.proxy', init) : init
116116
)
117117
: field.kind === 'frozen_state'
118-
? b.call(
119-
'$.source',
120-
should_proxy_or_freeze(init, state.scope) ? b.call('$.freeze', init) : init
121-
)
118+
? b.call('$.source', init)
122119
: field.kind === 'derived_call'
123120
? b.call('$.derived', init)
124121
: b.call('$.derived', b.thunk(init));
@@ -158,12 +155,7 @@ export const javascript_visitors_runes = {
158155
// set foo(value) { this.#foo = value; }
159156
const value = b.id('value');
160157
body.push(
161-
b.method(
162-
'set',
163-
definition.key,
164-
[value],
165-
[b.stmt(b.call('$.set', member, b.call('$.freeze', value)))]
166-
)
158+
b.method('set', definition.key, [value], [b.stmt(b.call('$.set', member, value))])
167159
);
168160
}
169161

@@ -314,15 +306,15 @@ export const javascript_visitors_runes = {
314306
? b.id('undefined')
315307
: /** @type {import('estree').Expression} */ (visit(args[0]));
316308

317-
if (rune === '$state' || rune === '$state.frozen') {
309+
if (rune === '$state' || rune === '$state.raw') {
318310
/**
319311
* @param {import('estree').Identifier} id
320312
* @param {import('estree').Expression} value
321313
*/
322314
const create_state_declarator = (id, value) => {
323315
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
324316
if (should_proxy_or_freeze(value, state.scope)) {
325-
value = b.call(rune === '$state' ? '$.proxy' : '$.freeze', value);
317+
value = rune === '$state' ? b.call('$.proxy', value) : value;
326318
}
327319
if (is_state_source(binding, state)) {
328320
value = b.call('$.source', value);

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ const javascript_visitors_runes = {
559559
if (node.value != null && node.value.type === 'CallExpression') {
560560
const rune = get_rune(node.value, state.scope);
561561

562-
if (rune === '$state' || rune === '$state.frozen' || rune === '$derived') {
562+
if (rune === '$state' || rune === '$state.raw' || rune === '$derived') {
563563
return {
564564
...node,
565565
value:

packages/svelte/src/compiler/phases/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const PassiveEvents = ['wheel', 'touchstart', 'touchmove', 'touchend', 't
3131

3232
export const Runes = /** @type {const} */ ([
3333
'$state',
34-
'$state.frozen',
34+
'$state.raw',
3535
'$state.snapshot',
3636
'$state.is',
3737
'$props',

packages/svelte/src/internal/client/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ export const INSPECT_EFFECT = 1 << 17;
2020
export const HEAD_EFFECT = 1 << 18;
2121

2222
export const STATE_SYMBOL = Symbol('$state');
23-
export const STATE_FROZEN_SYMBOL = Symbol('$state.frozen');
2423
export const LOADING_ATTR_SYMBOL = Symbol('');

0 commit comments

Comments
 (0)