Skip to content

fix: error when exporting reassigned state from module context #10728

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
Mar 8, 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/small-sheep-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: error when exporting reassigned state from module context
28 changes: 23 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,11 +997,29 @@ export const validation_runes = merge(validation, a11y_validators, {
if (node.label.name !== '$' || path.at(-1)?.type !== 'Program') return;
error(node, 'invalid-legacy-reactive-statement');
},
ExportNamedDeclaration(node, { state }) {
if (node.declaration?.type !== 'VariableDeclaration') return;
if (node.declaration.kind !== 'let') return;
if (state.analysis.instance.scope !== state.scope) return;
error(node, 'invalid-legacy-export');
ExportNamedDeclaration(node, { state, next }) {
if (state.ast_type === 'module') {
if (node.declaration?.type !== 'VariableDeclaration') return;

// visit children, so bindings are correctly initialised
next();

for (const declarator of node.declaration.declarations) {
for (const id of extract_identifiers(declarator.id)) {
validate_export(node, state.scope, id.name);
}
}
} else {
if (node.declaration?.type !== 'VariableDeclaration') return;
if (node.declaration.kind !== 'let') return;
if (state.analysis.instance.scope !== state.scope) return;
error(node, 'invalid-legacy-export');
}
},
ExportSpecifier(node, { state }) {
if (state.ast_type === 'module') {
validate_export(node, state.scope, node.local.name);
}
},
CallExpression(node, { state, path }) {
validate_call_expression(node, state.scope, path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from '../../test';

export default test({
error: {
code: 'invalid-state-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: [76, 114]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script context="module">
export const object = $state({
ok: true
});

export let primitive = $state('nope');

export function update_object() {
object.ok = !object.ok;
}

export function update_primitive() {
primitive = 'yep';
}
</script>