Skip to content

Commit 469bedf

Browse files
authored
chore: add bind tests for no-unnecessary-state-wrap (#1181)
1 parent 3308b87 commit 469bedf

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

packages/eslint-plugin-svelte/src/rules/no-unnecessary-state-wrap.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ export default createRule('no-unnecessary-state-wrap', {
5353
const options = context.options[0] ?? {};
5454
const additionalReactiveClasses = options.additionalReactiveClasses ?? [];
5555
const allowReassign = options.allowReassign ?? false;
56+
const { globalScope } = context.sourceCode.scopeManager;
57+
if (globalScope == null) {
58+
return {};
59+
}
5660

57-
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
61+
const referenceTracker = new ReferenceTracker(globalScope);
5862
const traceMap: Record<string, Record<string, boolean>> = {};
5963
for (const reactiveClass of REACTIVE_CLASSES) {
6064
traceMap[reactiveClass] = {
@@ -79,8 +83,10 @@ export default createRule('no-unnecessary-state-wrap', {
7983
});
8084

8185
function isReassigned(identifier: TSESTree.Identifier): boolean {
82-
const variable = context.sourceCode.scopeManager.getDeclaredVariables(identifier.parent)[0];
83-
return variable.references.some((ref) => {
86+
const references = context.sourceCode.scopeManager
87+
.getDeclaredVariables(identifier.parent)
88+
.flatMap((v) => v.references);
89+
return references.some((ref) => {
8490
return ref.isWrite() && ref.identifier !== identifier;
8591
});
8692
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"allowReassign": true
5+
}
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
import { SvelteSet } from 'svelte/reactivity';
3+
import Bug3 from './Bug3.svelte';
4+
5+
let svelteSet = $state(new SvelteSet<number>([]));
6+
</script>
7+
8+
<Bug3 bind:svelteSet />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"allowReassign": true
5+
}
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
import { SvelteSet } from 'svelte/reactivity';
3+
import Bug3 from './Bug3.svelte';
4+
5+
let svelteSet = $state(new SvelteSet<number>([]));
6+
</script>
7+
8+
<Bug3 bind:svelteSet={() => svelteSet, (v) => (svelteSet = v)} />

0 commit comments

Comments
 (0)