fix compiler errors around exported state

proxied-state-set
Rich Harris 10 months ago
parent 0cc90a7bfe
commit 4b6e0eb4c8

@ -164,8 +164,8 @@ const runes = {
'invalid-legacy-export': () => `Cannot use \`export let\` in runes mode — use $props instead`, 'invalid-legacy-export': () => `Cannot use \`export let\` in runes mode — use $props instead`,
/** @param {string} rune */ /** @param {string} rune */
'invalid-rune-usage': (rune) => `Cannot use ${rune} rune in non-runes mode`, 'invalid-rune-usage': (rune) => `Cannot use ${rune} rune in non-runes mode`,
/** @param {string} rune */ 'invalid-state-export': () => `Cannot export state if it is reassigned`,
'invalid-rune-export': (rune) => `Cannot export value created with ${rune}`, 'invalid-derived-export': () => `Cannot export derived state`,
'invalid-props-id': () => `$props() can only be used with an object destructuring pattern`, 'invalid-props-id': () => `$props() can only be used with an object destructuring pattern`,
'invalid-props-pattern': () => 'invalid-props-pattern': () =>
`$props() assignment must not contain nested properties or computed keys`, `$props() assignment must not contain nested properties or computed keys`,

@ -481,8 +481,14 @@ export const validation_legacy = merge(validation, a11y_validators, {
*/ */
function validate_export(node, scope, name) { function validate_export(node, scope, name) {
const binding = scope.get(name); const binding = scope.get(name);
if (binding && (binding.kind === 'derived' || binding.kind === 'state')) { if (!binding) return;
error(node, 'invalid-rune-export', `$${binding.kind}`);
if (binding.kind === 'derived') {
error(node, 'invalid-derived-export');
}
if (binding.kind === 'state' && binding.reassigned) {
error(node, 'invalid-state-export');
} }
} }

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'invalid-derived-export',
message: 'Cannot export derived state',
position: [24, 66]
}
});

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

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'invalid-state-export',
message: 'Cannot export state if it is reassigned',
position: [46, 86]
}
});

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

@ -37,11 +37,11 @@ const { test, run } = suite<CompilerErrorTest>((config, cwd) => {
} }
} }
if (fs.existsSync(`${cwd}/main.js`)) { if (fs.existsSync(`${cwd}/main.svelte.js`)) {
let caught_error = false; let caught_error = false;
try { try {
compileModule(fs.readFileSync(`${cwd}/main.js`, 'utf-8'), { compileModule(fs.readFileSync(`${cwd}/main.svelte.js`, 'utf-8'), {
generate: 'client' generate: 'client'
}); });
} catch (e) { } catch (e) {
@ -51,6 +51,10 @@ const { test, run } = suite<CompilerErrorTest>((config, cwd) => {
expect(error.code).toMatch(config.error.code); expect(error.code).toMatch(config.error.code);
expect(error.message).toMatch(config.error.message); expect(error.message).toMatch(config.error.message);
if (config.error.position) {
expect(error.position).toEqual(config.error.position);
}
} }
if (!caught_error) { if (!caught_error) {

Loading…
Cancel
Save