Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-queens-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: extend derived/state validation error to indirect exports
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let count = $state(0);

const double = $derived(count * 2);

export { double };
Original file line number Diff line number Diff line change
@@ -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]
}
});
Original file line number Diff line number Diff line change
@@ -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 };