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 <rich.harris@vercel.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
pull/11913/head
Paolo Ricciuti 5 months ago committed by GitHub
parent 03945bd9bc
commit f3c291d8dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: omit `state_referenced_locally` warning for component exports

@ -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'

@ -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…
Cancel
Save