diff --git a/.changeset/few-zoos-own.md b/.changeset/few-zoos-own.md new file mode 100644 index 000000000..ae1b77f9c --- /dev/null +++ b/.changeset/few-zoos-own.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: omit `state_referenced_locally` warning for component exports diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 618864d4e..77bfc8976 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -1244,6 +1244,12 @@ const common_visitors = { context.state.expression.metadata.dynamic = true; } + // TODO it would be better to just bail out when we hit the ExportSpecifier node but that's + // not currently possibly because of our visitor merging, which I desperately want to nuke + const is_export_specifier = + /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1)).type === + 'ExportSpecifier'; + if ( context.state.analysis.runes && node !== binding.node && @@ -1258,6 +1264,7 @@ const common_visitors = { !should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) || binding.kind === 'frozen_state' || binding.kind === 'derived') && + !is_export_specifier && // We're only concerned with reads here (parent.type !== 'AssignmentExpression' || parent.left !== node) && parent.type !== 'UpdateExpression' diff --git a/packages/svelte/tests/validator/samples/static-state-reference/input.svelte b/packages/svelte/tests/validator/samples/static-state-reference/input.svelte index 39482f0c2..cd0c7b734 100644 --- a/packages/svelte/tests/validator/samples/static-state-reference/input.svelte +++ b/packages/svelte/tests/validator/samples/static-state-reference/input.svelte @@ -6,15 +6,20 @@ console.log(obj); console.log(count); console.log(doubled); - // these are ok because they're writes + + // writes are okay count++; count = 1; obj.a++; obj.a = 1; - // @ts-ignore - let typed: {count: number} | null = null; + + // `count` here is correctly identified as a non-reference + let typed: { count: number } | null = null; + + // exports are okay as this is turned into a live reference + export { count }; -