pull/15589/head
ComputerGuy 1 year ago
parent 190c0c7653
commit 0d526ce5b8

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: allow `$state` in return statements

@ -104,6 +104,15 @@ export function CallExpression(node, context) {
}
case '$state':
if (
(!(parent.type === 'VariableDeclarator' || parent.type === 'ReturnStatement') ||
get_parent(context.path, -3).type === 'ConstTag') &&
!(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed)
) {
e.state_invalid_placement(node, rune);
}
break;
case '$state.raw':
case '$derived':
case '$derived.by':

@ -4,6 +4,7 @@ import { dev, is_ignored } from '../../../../state.js';
import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js';
import { transform_inspect_rune } from '../../utils.js';
import { should_proxy } from '../utils.js';
/**
* @param {CallExpression} node
@ -33,6 +34,20 @@ export function CallExpression(node, context) {
case '$inspect':
case '$inspect().with':
return transform_inspect_rune(node, context);
case '$state':
if (context.path.at(-1)?.type === 'ReturnStatement') {
if (
node.arguments[0] &&
should_proxy(
/** @type {Expression} */ (context.visit(node.arguments[0])),
context.state.scope
)
) {
return b.call('$.proxy', node.arguments[0]);
} else {
return node.arguments[0] ?? b.void0;
}
}
}
if (

Loading…
Cancel
Save