fix: extend derived/state validation error to indirect exports (#14039)

Closes #14029
pull/14041/head
Simon H 2 months ago committed by GitHub
parent 1434f48f7c
commit 8a2c97a7b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: extend derived/state validation error to indirect exports

@ -57,5 +57,21 @@ export function ExportNamedDeclaration(node, context) {
}
}
}
if (!context.state.ast_type /* .svelte.js module */ || context.state.ast_type === 'module') {
for (const specified of node.specifiers) {
const binding = context.state.scope.get(specified.local.name);
if (!binding) continue;
if (binding.kind === 'derived') {
e.derived_invalid_export(node);
}
if ((binding.kind === 'state' || binding.kind === 'raw_state') && binding.reassigned) {
e.state_invalid_export(node);
}
}
}
}
}

@ -0,0 +1,10 @@
import { test } from '../../test';
export default test({
error: {
code: 'derived_invalid_export',
message:
'Cannot export derived state from a module. To expose the current derived value, export a function returning its value',
position: [70, 76]
}
});

@ -0,0 +1,5 @@
let count = $state(0);
const double = $derived(count * 2);
export { double };

@ -0,0 +1,10 @@
import { test } from '../../test';
export default test({
error: {
code: 'state_invalid_export',
message:
"Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties",
position: [211, 220]
}
});

@ -0,0 +1,15 @@
const object = $state({
ok: true
});
let primitive = $state('nope');
export function update_object() {
object.ok = !object.ok;
}
export function update_primitive() {
primitive = 'yep';
}
export { object, primitive };
Loading…
Cancel
Save