Skip to content

Commit e3ff46e

Browse files
committed
$derived.fn -> $derived.call
1 parent f3681d8 commit e3ff46e

File tree

13 files changed

+44
-44
lines changed

13 files changed

+44
-44
lines changed

.changeset/nervous-spoons-relax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"svelte": patch
33
---
44

5-
chore: add $derived.fn rune
5+
chore: add $derived.call rune

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ const runes_scope_js_tweaker = {
674674
rune !== '$state' &&
675675
rune !== '$state.frozen' &&
676676
rune !== '$derived' &&
677-
rune !== '$derived.fn'
677+
rune !== '$derived.call'
678678
)
679679
return;
680680

@@ -710,7 +710,7 @@ const runes_scope_tweaker = {
710710
rune !== '$state' &&
711711
rune !== '$state.frozen' &&
712712
rune !== '$derived' &&
713-
rune !== '$derived.fn' &&
713+
rune !== '$derived.call' &&
714714
rune !== '$props'
715715
)
716716
return;
@@ -723,7 +723,7 @@ const runes_scope_tweaker = {
723723
? 'state'
724724
: rune === '$state.frozen'
725725
? 'frozen_state'
726-
: rune === '$derived' || rune === '$derived.fn'
726+
: rune === '$derived' || rune === '$derived.call'
727727
? 'derived'
728728
: path.is_rest
729729
? 'rest_prop'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ function validate_call_expression(node, scope, path) {
715715
error(node, 'invalid-props-location');
716716
}
717717

718-
if (rune === '$state' || rune === '$derived' || rune === '$derived.fn') {
718+
if (rune === '$state' || rune === '$derived' || rune === '$derived.call') {
719719
if (parent.type === 'VariableDeclarator') return;
720720
if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
721721
error(node, rune === '$derived' ? 'invalid-derived-location' : 'invalid-state-location');

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface ComponentClientTransformState extends ClientTransformState {
5858
}
5959

6060
export interface StateField {
61-
kind: 'state' | 'frozen_state' | 'derived' | 'derived_fn';
61+
kind: 'state' | 'frozen_state' | 'derived' | 'derived_call';
6262
id: PrivateIdentifier;
6363
}
6464

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const javascript_visitors_runes = {
3333
rune === '$state' ||
3434
rune === '$state.frozen' ||
3535
rune === '$derived' ||
36-
rune === '$derived.fn'
36+
rune === '$derived.call'
3737
) {
3838
/** @type {import('../types.js').StateField} */
3939
const field = {
@@ -42,8 +42,8 @@ export const javascript_visitors_runes = {
4242
? 'state'
4343
: rune === '$state.frozen'
4444
? 'frozen_state'
45-
: rune === '$derived.fn'
46-
? 'derived_fn'
45+
: rune === '$derived.call'
46+
? 'derived_call'
4747
: 'derived',
4848
// @ts-expect-error this is set in the next pass
4949
id: is_private ? definition.key : null
@@ -102,7 +102,7 @@ export const javascript_visitors_runes = {
102102
'$.source',
103103
should_proxy_or_freeze(init) ? b.call('$.freeze', init) : init
104104
)
105-
: field.kind === 'derived_fn'
105+
: field.kind === 'derived_call'
106106
? b.call('$.derived', init)
107107
: b.call('$.derived', b.thunk(init));
108108
} else {
@@ -146,7 +146,7 @@ export const javascript_visitors_runes = {
146146
);
147147
}
148148

149-
if ((field.kind === 'derived' || field.kind === 'derived_fn') && state.options.dev) {
149+
if ((field.kind === 'derived' || field.kind === 'derived_call') && state.options.dev) {
150150
body.push(
151151
b.method(
152152
'set',
@@ -286,12 +286,12 @@ export const javascript_visitors_runes = {
286286
continue;
287287
}
288288

289-
if (rune === '$derived' || rune === '$derived.fn') {
289+
if (rune === '$derived' || rune === '$derived.call') {
290290
if (declarator.id.type === 'Identifier') {
291291
declarations.push(
292292
b.declarator(
293293
declarator.id,
294-
b.call('$.derived', rune === '$derived.fn' ? value : b.thunk(value))
294+
b.call('$.derived', rune === '$derived.call' ? value : b.thunk(value))
295295
)
296296
);
297297
} else {
@@ -307,7 +307,7 @@ export const javascript_visitors_runes = {
307307
b.block([
308308
b.let(
309309
declarator.id,
310-
rune === '$derived.fn' ? b.call(value, b.id('$$derived')) : value
310+
rune === '$derived.call' ? b.call(value, b.id('$$derived')) : value
311311
),
312312
b.return(b.array(bindings.map((binding) => binding.node)))
313313
])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ const javascript_visitors_runes = {
558558
: /** @type {import('estree').Expression} */ (visit(node.value.arguments[0]))
559559
};
560560
}
561-
if (rune === '$derived.fn') {
561+
if (rune === '$derived.call') {
562562
return {
563563
...node,
564564
value:
@@ -592,7 +592,7 @@ const javascript_visitors_runes = {
592592
? b.id('undefined')
593593
: /** @type {import('estree').Expression} */ (visit(args[0]));
594594

595-
if (rune === '$derived.fn') {
595+
if (rune === '$derived.call') {
596596
declarations.push(
597597
b.declarator(
598598
/** @type {import('estree').Pattern} */ (visit(declarator.id)),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const Runes = /** @type {const} */ ([
7575
'$state.frozen',
7676
'$props',
7777
'$derived',
78-
'$derived.fn',
78+
'$derived.call',
7979
'$effect',
8080
'$effect.pre',
8181
'$effect.active',

packages/svelte/src/main/ambient.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ declare function $derived<T>(expression: T): T;
6262
declare namespace $derived {
6363
/**
6464
* Sometimes you need to create complex derivations which don't fit inside a short expression.
65-
* In this case, you can resort to `$derived.fn` which accepts a function as its argument and returns its value.
65+
* In this case, you can resort to `$derived.call` which accepts a function as its argument and returns its value.
6666
*
6767
* Example:
6868
* ```ts
69-
* $derived.fn(() => {
69+
* $derived.call(() => {
7070
* let tmp = count;
7171
* if (count > 10) {
7272
* tmp += 100;

packages/svelte/tests/runtime-runes/samples/class-state-derived-fn/main.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
class Counter {
33
count = $state(0);
4-
doubled = $derived.fn(() => this.count * 2);
4+
doubled = $derived.call(() => this.count * 2);
55
}
66
77
const counter = new Counter();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
let count = $state(0);
3-
let double = $derived.fn(() => count * 2);
3+
let double = $derived.call(() => count * 2);
44
</script>
55

66
<button on:click={() => count++}>{double}</button>

packages/svelte/types/index.d.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -988,15 +988,15 @@ declare module 'svelte/compiler' {
988988
filename?: string | undefined;
989989
} | undefined): Promise<Processed>;
990990
export class CompileError extends Error {
991-
991+
992992
constructor(code: string, message: string, position: [number, number] | undefined);
993-
993+
994994
filename: CompileError_1['filename'];
995-
995+
996996
position: CompileError_1['position'];
997-
997+
998998
start: CompileError_1['start'];
999-
999+
10001000
end: CompileError_1['end'];
10011001
code: string;
10021002
}
@@ -1007,9 +1007,9 @@ declare module 'svelte/compiler' {
10071007
* */
10081008
export const VERSION: string;
10091009
class Scope {
1010-
1010+
10111011
constructor(root: ScopeRoot, parent: Scope | null, porous: boolean);
1012-
1012+
10131013
root: ScopeRoot;
10141014
/**
10151015
* A map of every identifier declared by this scope, and all the
@@ -1033,25 +1033,25 @@ declare module 'svelte/compiler' {
10331033
* which is usually an error. Block statements do not increase this value
10341034
*/
10351035
function_depth: number;
1036-
1036+
10371037
declare(node: import('estree').Identifier, kind: Binding['kind'], declaration_kind: DeclarationKind, initial?: null | import('estree').Expression | import('estree').FunctionDeclaration | import('estree').ClassDeclaration | import('estree').ImportDeclaration | EachBlock): Binding;
10381038
child(porous?: boolean): Scope;
1039-
1039+
10401040
generate(preferred_name: string): string;
1041-
1041+
10421042
get(name: string): Binding | null;
1043-
1043+
10441044
get_bindings(node: import('estree').VariableDeclarator | LetDirective): Binding[];
1045-
1045+
10461046
owner(name: string): Scope | null;
1047-
1047+
10481048
reference(node: import('estree').Identifier, path: SvelteNode[]): void;
10491049
#private;
10501050
}
10511051
class ScopeRoot {
1052-
1052+
10531053
conflicts: Set<string>;
1054-
1054+
10551055
unique(preferred_name: string): import("estree").Identifier;
10561056
}
10571057
interface BaseNode {
@@ -2464,11 +2464,11 @@ declare function $derived<T>(expression: T): T;
24642464
declare namespace $derived {
24652465
/**
24662466
* Sometimes you need to create complex derivations which don't fit inside a short expression.
2467-
* In this case, you can resort to `$derived.fn` which accepts a function as its argument and returns its value.
2467+
* In this case, you can resort to `$derived.call` which accepts a function as its argument and returns its value.
24682468
*
24692469
* Example:
24702470
* ```ts
2471-
* $derived.fn(() => {
2471+
* $derived.call(() => {
24722472
* let tmp = count;
24732473
* if (count > 10) {
24742474
* tmp += 100;
@@ -2603,4 +2603,4 @@ declare function $inspect<T extends any[]>(
26032603
...values: T
26042604
): { with: (fn: (type: 'init' | 'update', ...values: T) => void) => void };
26052605

2606-
//# sourceMappingURL=index.d.ts.map
2606+
//# sourceMappingURL=index.d.ts.map

sites/svelte-5-preview/src/lib/CodeMirror.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@
208208
{ label: '$state', type: 'keyword', boost: 10 },
209209
{ label: '$props', type: 'keyword', boost: 9 },
210210
{ label: '$derived', type: 'keyword', boost: 8 },
211-
snip('$derived.fn(() => {\n\t${}\n});', {
212-
label: '$derived.fn',
211+
snip('$derived.call(() => {\n\t${}\n});', {
212+
label: '$derived.call',
213213
type: 'keyword',
214214
boost: 7
215215
}),

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ If the value of a reactive variable is being computed it should be replaced with
134134
```
135135
...`double` will be calculated first despite the source order. In runes mode, `triple` cannot reference `double` before it has been declared.
136136

137-
### `$derived.fn`
137+
### `$derived.call`
138138

139-
Sometimes you need to create complex derivations which don't fit inside a short expression. In this case, you can resort to `$derived.fn` which accepts a function as its argument and returns its value.
139+
Sometimes you need to create complex derivations which don't fit inside a short expression. In this case, you can resort to `$derived.call` which accepts a function as its argument and returns its value.
140140

141141
```svelte
142142
<script>
143143
let count = $state(0);
144-
let complex = $derived.fn(() => {
144+
let complex = $derived.call(() => {
145145
let tmp = count;
146146
if (count > 10) {
147147
tmp += 100;

0 commit comments

Comments
 (0)