Skip to content

fix: silence state_referenced_locally when state is exported #11905

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 10 commits into from
Jun 4, 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/few-zoos-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: omit `state_referenced_locally` warning for component exports
7 changes: 7 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
</script>

<button onclick={() => count += 1}>
<button onclick={() => (count += 1)}>
clicks: {count}
</button>
Loading