Skip to content

Commit f3c291d

Browse files
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 <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 03945bd commit f3c291d

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

.changeset/few-zoos-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: omit `state_referenced_locally` warning for component exports

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,12 @@ const common_visitors = {
12441244
context.state.expression.metadata.dynamic = true;
12451245
}
12461246

1247+
// TODO it would be better to just bail out when we hit the ExportSpecifier node but that's
1248+
// not currently possibly because of our visitor merging, which I desperately want to nuke
1249+
const is_export_specifier =
1250+
/** @type {import('#compiler').SvelteNode} */ (context.path.at(-1)).type ===
1251+
'ExportSpecifier';
1252+
12471253
if (
12481254
context.state.analysis.runes &&
12491255
node !== binding.node &&
@@ -1258,6 +1264,7 @@ const common_visitors = {
12581264
!should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) ||
12591265
binding.kind === 'frozen_state' ||
12601266
binding.kind === 'derived') &&
1267+
!is_export_specifier &&
12611268
// We're only concerned with reads here
12621269
(parent.type !== 'AssignmentExpression' || parent.left !== node) &&
12631270
parent.type !== 'UpdateExpression'

packages/svelte/tests/validator/samples/static-state-reference/input.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
console.log(obj);
77
console.log(count);
88
console.log(doubled);
9-
// these are ok because they're writes
9+
10+
// writes are okay
1011
count++;
1112
count = 1;
1213
obj.a++;
1314
obj.a = 1;
14-
// @ts-ignore
15-
let typed: {count: number} | null = null;
15+
16+
// `count` here is correctly identified as a non-reference
17+
let typed: { count: number } | null = null;
18+
19+
// exports are okay as this is turned into a live reference
20+
export { count };
1621
</script>
1722

18-
<button onclick={() => count += 1}>
23+
<button onclick={() => (count += 1)}>
1924
clicks: {count}
2025
</button>

0 commit comments

Comments
 (0)