Skip to content

Commit 063ed9d

Browse files
committed
redo
1 parent 98ae05b commit 063ed9d

File tree

28 files changed

+63
-160
lines changed

28 files changed

+63
-160
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/visitors/CallExpression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function CallExpression(node, context) {
7373
break;
7474

7575
case '$state':
76-
case '$state.frozen':
76+
case '$state.raw':
7777
case '$derived':
7878
case '$derived.by':
7979
if (

packages/svelte/src/compiler/phases/2-analyze/visitors/VariableDeclarator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function VariableDeclarator(node, context) {
2121
// TODO feels like this should happen during scope creation?
2222
if (
2323
rune === '$state' ||
24-
rune === '$state.frozen' ||
24+
rune === '$state.raw' ||
2525
rune === '$derived' ||
2626
rune === '$derived.by' ||
2727
rune === '$props'
@@ -32,7 +32,7 @@ export function VariableDeclarator(node, context) {
3232
binding.kind =
3333
rune === '$state'
3434
? 'state'
35-
: rune === '$state.frozen'
35+
: rune === '$state.raw'
3636
? 'frozen_state'
3737
: rune === '$derived' || rune === '$derived.by'
3838
? 'derived'

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
@@ -256,18 +256,8 @@ export function client_component(analysis, options) {
256256
}
257257

258258
if (binding?.kind === 'state' || binding?.kind === 'frozen_state') {
259-
return [
260-
getter,
261-
b.set(alias ?? name, [
262-
b.stmt(
263-
b.call(
264-
'$.set',
265-
b.id(name),
266-
b.call(binding.kind === 'state' ? '$.proxy' : '$.freeze', b.id('$$value'))
267-
)
268-
)
269-
])
270-
];
259+
const value = binding.kind === 'state' ? b.call('$.proxy', b.id('$$value')) : b.id('$$value');
260+
return [getter, b.set(alias ?? name, [b.stmt(b.call('$.set', b.id(name), value))])];
271261
}
272262

273263
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
@@ -210,7 +210,7 @@ export function build_setter(node, context, fallback, prefix, options) {
210210
if (assignment.type === 'AssignmentExpression') {
211211
assignment.right =
212212
private_state.kind === 'frozen_state'
213-
? b.call('$.freeze', value)
213+
? value
214214
: build_proxy_reassignment(value, private_state.id);
215215
return assignment;
216216
}
@@ -223,7 +223,7 @@ export function build_setter(node, context, fallback, prefix, options) {
223223
!options?.skip_proxy_and_freeze &&
224224
should_proxy_or_freeze(value, context.state.scope)
225225
? private_state.kind === 'frozen_state'
226-
? b.call('$.freeze', value)
226+
? value
227227
: build_proxy_reassignment(value, private_state.id)
228228
: value
229229
);
@@ -247,7 +247,7 @@ export function build_setter(node, context, fallback, prefix, options) {
247247
if (assignment.type === 'AssignmentExpression') {
248248
assignment.right =
249249
public_state.kind === 'frozen_state'
250-
? b.call('$.freeze', value)
250+
? value
251251
: build_proxy_reassignment(value, public_state.id);
252252
return assignment;
253253
}
@@ -344,7 +344,7 @@ export function build_setter(node, context, fallback, prefix, options) {
344344
context.state.analysis.runes &&
345345
!options?.skip_proxy_and_freeze &&
346346
should_proxy_or_freeze(value, context.state.scope)
347-
? b.call('$.freeze', value)
347+
? value
348348
: value
349349
);
350350
} else if (

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function ClassBody(node, context) {
4444
const rune = get_rune(definition.value, context.state.scope);
4545
if (
4646
rune === '$state' ||
47-
rune === '$state.frozen' ||
47+
rune === '$state.raw' ||
4848
rune === '$derived' ||
4949
rune === '$derived.by'
5050
) {
@@ -53,7 +53,7 @@ export function ClassBody(node, context) {
5353
kind:
5454
rune === '$state'
5555
? 'state'
56-
: rune === '$state.frozen'
56+
: rune === '$state.raw'
5757
? 'frozen_state'
5858
: rune === '$derived.by'
5959
? 'derived_by'
@@ -117,12 +117,7 @@ export function ClassBody(node, context) {
117117
should_proxy_or_freeze(init, context.state.scope) ? b.call('$.proxy', init) : init
118118
)
119119
: field.kind === 'frozen_state'
120-
? b.call(
121-
'$.source',
122-
should_proxy_or_freeze(init, context.state.scope)
123-
? b.call('$.freeze', init)
124-
: init
125-
)
120+
? b.call('$.source', init)
126121
: field.kind === 'derived_by'
127122
? b.call('$.derived', init)
128123
: b.call('$.derived', b.thunk(init));
@@ -158,12 +153,7 @@ export function ClassBody(node, context) {
158153
// set foo(value) { this.#foo = value; }
159154
const value = b.id('value');
160155
body.push(
161-
b.method(
162-
'set',
163-
definition.key,
164-
[value],
165-
[b.stmt(b.call('$.set', member, b.call('$.freeze', value)))]
166-
)
156+
b.method('set', definition.key, [value], [b.stmt(b.call('$.set', member, value))])
167157
);
168158
}
169159

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function VariableDeclaration(node, context) {
119119
const value =
120120
args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (context.visit(args[0]));
121121

122-
if (rune === '$state' || rune === '$state.frozen') {
122+
if (rune === '$state' || rune === '$state.raw') {
123123
/**
124124
* @param {Identifier} id
125125
* @param {Expression} value
@@ -128,8 +128,8 @@ export function VariableDeclaration(node, context) {
128128
const binding = /** @type {import('#compiler').Binding} */ (
129129
context.state.scope.get(id.name)
130130
);
131-
if (should_proxy_or_freeze(value, context.state.scope)) {
132-
value = b.call(rune === '$state' ? '$.proxy' : '$.freeze', value);
131+
if (rune === '$state' && should_proxy_or_freeze(value, context.state.scope)) {
132+
value = b.call('$.proxy', value);
133133
}
134134
if (is_state_source(binding, context.state)) {
135135
value = b.call('$.source', value);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function PropertyDefinition(node, context) {
1111
if (context.state.analysis.runes && node.value != null && node.value.type === 'CallExpression') {
1212
const rune = get_rune(node.value, context.state.scope);
1313

14-
if (rune === '$state' || rune === '$state.frozen' || rune === '$derived') {
14+
if (rune === '$state' || rune === '$state.raw' || rune === '$derived') {
1515
return {
1616
...node,
1717
value:

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)