Skip to content

fix(no-unused-props): resolve false positives on props with default values or $bindable usage #1146

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 1 commit into from
Mar 20, 2025
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-singers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(no-unused-props): resolve false positives on props with default values or $bindable usage
15 changes: 11 additions & 4 deletions packages/eslint-plugin-svelte/src/rules/no-unused-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@

const options = context.options[0] ?? {};

// TODO: Remove in v4

Check warning on line 74 in packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Remove in v4'
// MEMO: `ignorePatterns` was a property that only existed from v3.2.0 to v3.2.2.
// From v3.3.0, it was replaced with `ignorePropertyPatterns` and `ignoreTypePatterns`.
if (options.ignorePatterns != null && !isRemovedWarningShown) {
console.warn(

Check warning on line 78 in packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
'eslint-plugin-svelte: The `ignorePatterns` option in the `no-unused-props` rule has been removed. Please use `ignorePropertyPatterns` or/and `ignoreTypePatterns` instead.'
);
isRemovedWarningShown = true;
Expand Down Expand Up @@ -369,10 +369,17 @@
if (node.id.type === 'ObjectPattern') {
usedProps = getUsedPropertiesFromPattern(node.id);
if (usedProps.size === 0) return;
const identifiers = node.id.properties
.filter((p): p is TSESTree.Property => p.type === 'Property')
.map((p) => p.value)
.filter((v): v is TSESTree.Identifier => v.type === 'Identifier');
const identifiers: TSESTree.Identifier[] = [];
for (const p of node.id.properties) {
if (p.type !== 'Property') {
continue;
}
if (p.value.type === 'Identifier') {
identifiers.push(p.value);
} else if (p.value.type === 'AssignmentPattern' && p.value.left.type === 'Identifier') {
identifiers.push(p.value.left);
}
}
for (const identifier of identifiers) {
const paths = getUsedNestedPropertyNames(identifier);
usedPaths.push(...paths.map((path) => [identifier.name, ...path]));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
interface Props {
selected: { value: string };
}

let { selected = $bindable() }: Props = $props();
</script>

<input type="text" bind:value={selected.value} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
interface Props {
newTaskAttributes: { attribute: number; attribute2: string };
}

const { newTaskAttributes = { attribute: 0, attribute2: '' } }: Props = $props();
</script>

<div class="col-span-full">
<div>
{newTaskAttributes.attribute}
{newTaskAttributes.attribute2}
</div>
</div>