Skip to content
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

fix: stop reporting child properties in no-unused-props when the parent object itself is used #1143

Merged
merged 1 commit into from
Mar 18, 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/floppy-symbols-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix: stop reporting child properties in `no-unused-props` when the parent object itself is used
38 changes: 31 additions & 7 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 @@ -156,7 +156,12 @@

const paths: PropertyPath[] = [];
for (const reference of variable.references) {
if ('identifier' in reference && reference.identifier.type === 'Identifier') {
if (
'identifier' in reference &&
reference.identifier.type === 'Identifier' &&
(reference.identifier.range[0] !== node.range[0] ||
reference.identifier.range[1] !== node.range[1])
) {
const referencePath = getPropertyPath(reference.identifier);
paths.push(referencePath);
}
Expand Down Expand Up @@ -265,11 +270,17 @@
if (reportedProps.has(currentPathStr)) continue;

const propType = typeChecker.getTypeOfSymbol(prop);
const isUsedInPath = usedPaths.some((path) => {
const usedPath = path.join('.');
return usedPath === currentPathStr || usedPath.startsWith(`${currentPathStr}.`);

const joinedUsedPaths = usedPaths.map((path) => path.join('.'));
const isUsedThisInPath = joinedUsedPaths.includes(currentPathStr);
const isUsedInPath = joinedUsedPaths.some((path) => {
return path.startsWith(`${currentPathStr}.`);
});

if (isUsedThisInPath && !isUsedInPath) {
continue;
}

const isUsedInProps = usedProps.has(propName);

if (!isUsedInPath && !isUsedInProps) {
Expand All @@ -282,10 +293,11 @@
parent: parentPath.join('.')
}
});
continue;
}

const isUsedNested = usedPaths.some((path) => {
return path.join('.').startsWith(`${currentPathStr}.`);
const isUsedNested = joinedUsedPaths.some((path) => {
return path.startsWith(`${currentPathStr}.`);
});

if (isUsedNested || isUsedInProps) {
Expand Down Expand Up @@ -324,6 +336,18 @@
return usedProps.size === 0;
}

function normalizeUsedPaths(paths: PropertyPath[]): PropertyPath[] {
const normalized: PropertyPath[] = [];
for (const path of paths.sort((a, b) => a.length - b.length)) {
if (path.length === 0) continue;
if (normalized.some((p) => p.every((part, idx) => part === path[idx]))) {
continue;
}
normalized.push(path);
}
return normalized;
}

return {
'VariableDeclaration > VariableDeclarator': (node: TSESTree.VariableDeclarator) => {
// Only check $props declarations
Expand Down Expand Up @@ -359,7 +383,7 @@

checkUnusedProperties(
propType,
usedPaths,
normalizeUsedPaths(usedPaths),
usedProps,
node.id,
[],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import GenericPopout from './GenericPopout.svelte';
let {
position
}: {
position: {
x: number;
y: number;
};
} = $props();
</script>

<GenericPopout {position}>Test</GenericPopout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
import GenericPopout from './GenericPopout.svelte';
let {
wrapper
}: {
wrapper: {
position: {
x: number;
y: number;
};
};
} = $props();
</script>

<GenericPopout position={wrapper.position}>Test</GenericPopout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
import GenericPopout from './GenericPopout.svelte';

let {
wrapper
}: {
wrapper: {
position: {
x: number;
y: number;
};
};
} = $props();
</script>

<GenericPopout x={wrapper.position.x}>Test</GenericPopout>
<GenericPopout position={wrapper.position}>Test</GenericPopout>