From f3c291d8dd15ec7d3a47aa1e95d50a6acfb5678e Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Tue, 4 Jun 2024 19:09:36 +0200 Subject: [PATCH] fix: silence `state_referenced_locally` when state is exported (#11905) * fix: silence `state_referenced_locally` when state is exported * chore: add changesets * chore: use `some` * fix * better * we don't need a whole new test for this * Update .changeset/few-zoos-own.md * prettier * tidy up the test a bit since we're in here --------- Co-authored-by: Rich Harris Co-authored-by: Rich Harris --- .changeset/few-zoos-own.md | 5 +++++ .../svelte/src/compiler/phases/2-analyze/index.js | 7 +++++++ .../samples/static-state-reference/input.svelte | 13 +++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/few-zoos-own.md 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 }; -