Skip to content

fix(no-unused-props): handle alias props name properly #1178

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 2 commits into from
Apr 1, 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/fifty-jeans-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(no-unused-props): handle alias props name properly
32 changes: 23 additions & 9 deletions packages/eslint-plugin-svelte/src/rules/no-unused-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { toRegExp } from '../utils/regexp.js';

type PropertyPathArray = string[];
type DeclaredPropertyNames = Set<{ originalName: string; aliasName: string }>;

let isRemovedWarningShown = false;

Expand Down Expand Up @@ -70,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 @@ -182,16 +183,27 @@
return sourceFile.fileName.includes('node_modules/typescript/lib/');
}

function getUsedPropertyNamesFromPattern(pattern: TSESTree.ObjectPattern): Set<string> {
const usedProps = new Set<string>();
function getUsedPropertyNamesFromPattern(
pattern: TSESTree.ObjectPattern
): DeclaredPropertyNames {
const usedProps: DeclaredPropertyNames = new Set();
for (const prop of pattern.properties) {
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
usedProps.add(prop.key.name);
if (prop.type === 'Property') {
if (prop.key.type === 'Identifier') {
usedProps.add({ originalName: prop.key.name, aliasName: prop.key.name });
} else if (
prop.key.type === 'Literal' &&
typeof prop.key.value === 'string' &&
prop.value.type === 'Identifier'
) {
usedProps.add({ originalName: prop.key.value, aliasName: prop.value.name });
}
} else if (prop.type === 'RestElement') {
// If there's a rest element, all properties are potentially used
return new Set();
}
}

return usedProps;
}

Expand Down Expand Up @@ -229,7 +241,7 @@
}: {
propsType: ts.Type;
usedPropertyPaths: string[];
declaredPropertyNames: Set<string>;
declaredPropertyNames: DeclaredPropertyNames;
reportNode: TSESTree.Node;
parentPath: string[];
checkedPropsTypes: Set<string>;
Expand Down Expand Up @@ -287,7 +299,9 @@
continue;
}

const isUsedInProps = declaredPropertyNames.has(propName);
const isUsedInProps = Array.from(declaredPropertyNames).some((p) => {
return p.originalName === propName;
});

if (!isUsedInPath && !isUsedInProps) {
reportedPropertyPaths.add(currentPathStr);
Expand Down Expand Up @@ -338,8 +352,8 @@
* Returns true if the destructuring pattern includes a rest element,
* which means all remaining properties are potentially used.
*/
function hasRestElement(usedProps: Set<string>): boolean {
return usedProps.size === 0;
function hasRestElement(declaredPropertyNames: DeclaredPropertyNames): boolean {
return declaredPropertyNames.size === 0;
}

function normalizeUsedPaths(paths: PropertyPathArray[]): PropertyPathArray[] {
Expand Down Expand Up @@ -370,7 +384,7 @@

const propsType = typeChecker.getTypeFromTypeNode(tsNode.type);
let usedPropertyPathsArray: PropertyPathArray[] = [];
let declaredPropertyNames = new Set<string>();
let declaredPropertyNames: DeclaredPropertyNames = new Set();

if (node.id.type === 'ObjectPattern') {
declaredPropertyNames = getUsedPropertyNamesFromPattern(node.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: "'test' is an unused Props property."
line: 7
column: 8
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
type Props = {
test: string;
'aria-label'?: string;
};

const { 'aria-label': foo }: Props = $props();
</script>

{foo}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
type Props = {
test: string;
'aria-label'?: string;
};

const { test, 'aria-label': ariaLabel }: Props = $props();
</script>

<h1>{test}</h1>
<div aria-label={ariaLabel}>svelte/no-unused-props does not always respect aliases</div>