Skip to content

Commit b17d302

Browse files
committed
add allowUnusedNestedProperties option
1 parent bafbf5f commit b17d302

File tree

7 files changed

+69
-3
lines changed

7 files changed

+69
-3
lines changed

docs/rules/no-unused-props.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ Note: Properties of class types are not checked for usage, as they might be used
163163
"ignoreTypePatterns": [],
164164
// Patterns to ignore when checking for unused props
165165
"ignorePropertyPatterns": [],
166+
// Whether to allow unused nested properties
167+
"allowUnusedNestedProperties": false
166168
}]
167169
}
168170
```
169171

170172
- `checkImportedTypes` ... Controls whether to check properties from types defined in external files. Default is `false`, meaning the rule only checks types defined within the component file itself. When set to `true`, the rule will also check properties from imported and extended types.
171173
- `ignoreTypePatterns` ... Regular expression patterns for type names to exclude from checks. Default is `[]` (no exclusions). Most useful when `checkImportedTypes` is `true`, allowing you to exclude specific imported types (like utility types or third-party types) from being checked.
172174
- `ignorePropertyPatterns` ... Regular expression patterns for property names to exclude from unused checks. Default is `[]` (no exclusions). Most useful when `checkImportedTypes` is `true`, allowing you to ignore specific properties from external types that shouldn't trigger warnings.
175+
- `allowUnusedNestedProperties` ... Controls whether to allow unused nested properties. Default is `false`, meaning the rule will report unused properties from nested objects.
173176

174177
Examples:
175178

@@ -219,6 +222,21 @@ Examples:
219222
</script>
220223
```
221224

225+
```svelte
226+
<!-- ✓ Good Example with allowUnusedNestedProperties: true -->
227+
<script lang="ts">
228+
/* eslint svelte/no-unused-props: ["error", { "allowUnusedNestedProperties": true }] */
229+
interface Props {
230+
user: {
231+
name: string;
232+
age: number; // Won't be reported as unused
233+
};
234+
}
235+
let { user }: Props = $props();
236+
console.log(user.name);
237+
</script>
238+
```
239+
222240
## :gear: Required Configuration
223241

224242
This rule requires `@typescript-eslint/parser` to work. Please refer to the [User Guide](../user-guide.md) for more information.

packages/eslint-plugin-svelte/src/rule-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ type SvelteNoUnusedProps = []|[{
537537
checkImportedTypes?: boolean
538538
ignoreTypePatterns?: string[]
539539
ignorePropertyPatterns?: string[]
540+
allowUnusedNestedProperties?: boolean
540541
}]
541542
// ----- svelte/no-useless-mustaches -----
542543
type SvelteNoUselessMustaches = []|[{

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export default createRule('no-unused-props', {
3737
type: 'string'
3838
},
3939
default: []
40+
},
41+
allowUnusedNestedProperties: {
42+
type: 'boolean',
43+
default: false
4044
}
4145
},
4246
additionalProperties: false
@@ -342,7 +346,10 @@ export default createRule('no-unused-props', {
342346
return usedProps.size === 0;
343347
}
344348

345-
function normalizeUsedPaths(paths: PropertyPathArray[]): PropertyPathArray[] {
349+
function normalizeUsedPaths(
350+
paths: PropertyPathArray[],
351+
allowUnusedNestedProperties: boolean
352+
): PropertyPathArray[] {
346353
const normalized: PropertyPathArray[] = [];
347354
for (const path of paths.sort((a, b) => a.length - b.length)) {
348355
if (path.length === 0) continue;
@@ -351,7 +358,10 @@ export default createRule('no-unused-props', {
351358
}
352359
normalized.push(path);
353360
}
354-
return normalized;
361+
return normalized.map((path) => {
362+
if (allowUnusedNestedProperties) return [path[0]];
363+
return path;
364+
});
355365
}
356366

357367
return {
@@ -396,7 +406,10 @@ export default createRule('no-unused-props', {
396406

397407
checkUnusedProperties({
398408
propsType,
399-
usedPropertyPaths: normalizeUsedPaths(usedPropertyPathsArray).map((pathArray) => {
409+
usedPropertyPaths: normalizeUsedPaths(
410+
usedPropertyPathsArray,
411+
options.allowUnusedNestedProperties
412+
).map((pathArray) => {
400413
return pathArray.join('.');
401414
}),
402415
declaredPropertyNames,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"allowUnusedNestedProperties": true
5+
}
6+
]
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
interface Props {
3+
user: {
4+
name: string;
5+
location: string;
6+
};
7+
}
8+
let props: Props = $props();
9+
console.log(props.user.name);
10+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"allowUnusedNestedProperties": true
5+
}
6+
]
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
interface Props {
3+
user: {
4+
name: string;
5+
age: number; // Won't be reported as unused
6+
};
7+
}
8+
let { user }: Props = $props();
9+
console.log(user.name);
10+
</script>

0 commit comments

Comments
 (0)