loosen further

loosen-state-rune
Dominic Gannaway 10 months ago
parent 2fb8a527e7
commit b182a4181d

@ -2,4 +2,4 @@
"svelte": patch
---
fix: loosen restrictions on $state rune for return statements
fix: loosen restrictions on $state rune when passed objects or arrays

@ -174,7 +174,7 @@ const runes = {
'invalid-derived-location': () =>
`$derived() can only be used as a variable declaration initializer or a class field`,
'invalid-state-location': () =>
`$state() can only be used as a variable declaration initializer, a class field or a return statement if passed an object or array`,
`$state() can only be used as a variable declaration initializer, a class field or if passed an object or array, can be used as an expression`,
'invalid-state-frozen-location': () =>
`$state.frozen() can only be used as a variable declaration initializer or a class field`,
'invalid-effect-location': () => `$effect() can only be used as an expression statement`,

@ -719,12 +719,12 @@ function validate_call_expression(node, scope, path) {
if (rune === '$state' || rune === '$derived') {
if (parent.type === 'VariableDeclarator') return;
if (
parent.type === 'ReturnStatement' &&
node.arguments.length === 1 &&
node.arguments[0].type !== 'SpreadElement' &&
should_proxy_or_freeze(node.arguments[0])
)
) {
return;
}
if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
error(node, rune === '$derived' ? 'invalid-derived-location' : 'invalid-state-location');
}

@ -154,17 +154,6 @@ export const javascript_visitors_runes = {
return { ...node, body };
},
ReturnStatement(node, { state, visit, next }) {
const rune = get_rune(node.argument, state.scope);
if (rune === '$state' && node.argument?.type === 'CallExpression') {
const value = /** @type {import('estree').Expression} **/ (visit(node.argument.arguments[0]));
return {
...node,
argument: b.call('$.proxy', value)
};
}
next();
},
VariableDeclaration(node, { state, visit }) {
const declarations = [];
@ -373,6 +362,11 @@ export const javascript_visitors_runes = {
return transform_inspect_rune(node, context);
}
if (rune === '$state') {
const value = /** @type {import('estree').Expression} **/ (context.visit(node.arguments[0]));
return b.call('$.proxy', value);
}
context.next();
}
};

@ -3,7 +3,8 @@ import { test } from '../../test';
export default test({
error: {
code: 'invalid-state-location',
message: '$state() can only be used as a variable declaration initializer, a class field or a return statement if passed an object or array',
message:
'$state() can only be used as a variable declaration initializer, a class field or if passed an object or array, can be used as an expression',
position: [33, 41]
}
});

@ -3,6 +3,7 @@ import { test } from '../../test';
export default test({
error: {
code: 'invalid-state-location',
message: '$state() can only be used as a variable declaration initializer, a class field or a return statement if passed an object or array'
message:
'$state() can only be used as a variable declaration initializer, a class field or if passed an object or array, can be used as an expression'
}
});

@ -3,6 +3,7 @@ import { test } from '../../test';
export default test({
error: {
code: 'invalid-state-location',
message: '$state() can only be used as a variable declaration initializer, a class field or a return statement if passed an object or array'
message:
'$state() can only be used as a variable declaration initializer, a class field or if passed an object or array, can be used as an expression'
}
});

@ -3,6 +3,7 @@ import { test } from '../../test';
export default test({
error: {
code: 'invalid-state-location',
message: '$state() can only be used as a variable declaration initializer, a class field or a return statement if passed an object or array'
message:
'$state() can only be used as a variable declaration initializer, a class field or if passed an object or array, can be used as an expression'
}
});

Loading…
Cancel
Save