Skip to content

Commit 0d526ce

Browse files
committed
init
1 parent 190c0c7 commit 0d526ce

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.changeset/sharp-rings-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: allow `$state` in return statements

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ export function CallExpression(node, context) {
104104
}
105105

106106
case '$state':
107+
if (
108+
(!(parent.type === 'VariableDeclarator' || parent.type === 'ReturnStatement') ||
109+
get_parent(context.path, -3).type === 'ConstTag') &&
110+
!(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed)
111+
) {
112+
e.state_invalid_placement(node, rune);
113+
}
114+
115+
break;
107116
case '$state.raw':
108117
case '$derived':
109118
case '$derived.by':

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { dev, is_ignored } from '../../../../state.js';
44
import * as b from '../../../../utils/builders.js';
55
import { get_rune } from '../../../scope.js';
66
import { transform_inspect_rune } from '../../utils.js';
7+
import { should_proxy } from '../utils.js';
78

89
/**
910
* @param {CallExpression} node
@@ -33,6 +34,20 @@ export function CallExpression(node, context) {
3334
case '$inspect':
3435
case '$inspect().with':
3536
return transform_inspect_rune(node, context);
37+
case '$state':
38+
if (context.path.at(-1)?.type === 'ReturnStatement') {
39+
if (
40+
node.arguments[0] &&
41+
should_proxy(
42+
/** @type {Expression} */ (context.visit(node.arguments[0])),
43+
context.state.scope
44+
)
45+
) {
46+
return b.call('$.proxy', node.arguments[0]);
47+
} else {
48+
return node.arguments[0] ?? b.void0;
49+
}
50+
}
3651
}
3752

3853
if (

0 commit comments

Comments
 (0)