Skip to content

Commit 1233e46

Browse files
authored
fix(no-unused-props): resolve false positives on props with default values or $bindable usage (#1146)
1 parent 423fe5e commit 1233e46

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

Diff for: .changeset/few-singers-marry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix(no-unused-props): resolve false positives on props with default values or $bindable usage

Diff for: packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,17 @@ export default createRule('no-unused-props', {
369369
if (node.id.type === 'ObjectPattern') {
370370
usedProps = getUsedPropertiesFromPattern(node.id);
371371
if (usedProps.size === 0) return;
372-
const identifiers = node.id.properties
373-
.filter((p): p is TSESTree.Property => p.type === 'Property')
374-
.map((p) => p.value)
375-
.filter((v): v is TSESTree.Identifier => v.type === 'Identifier');
372+
const identifiers: TSESTree.Identifier[] = [];
373+
for (const p of node.id.properties) {
374+
if (p.type !== 'Property') {
375+
continue;
376+
}
377+
if (p.value.type === 'Identifier') {
378+
identifiers.push(p.value);
379+
} else if (p.value.type === 'AssignmentPattern' && p.value.left.type === 'Identifier') {
380+
identifiers.push(p.value.left);
381+
}
382+
}
376383
for (const identifier of identifiers) {
377384
const paths = getUsedNestedPropertyNames(identifier);
378385
usedPaths.push(...paths.map((path) => [identifier.name, ...path]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
interface Props {
3+
selected: { value: string };
4+
}
5+
6+
let { selected = $bindable() }: Props = $props();
7+
</script>
8+
9+
<input type="text" bind:value={selected.value} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
interface Props {
3+
newTaskAttributes: { attribute: number; attribute2: string };
4+
}
5+
6+
const { newTaskAttributes = { attribute: 0, attribute2: '' } }: Props = $props();
7+
</script>
8+
9+
<div class="col-span-full">
10+
<div>
11+
{newTaskAttributes.attribute}
12+
{newTaskAttributes.attribute2}
13+
</div>
14+
</div>

0 commit comments

Comments
 (0)