Skip to content

Commit 1ff148a

Browse files
authored
fix: correct detection of externally defined types in no-unused-props rule (#1135)
1 parent bea4945 commit 1ff148a

File tree

8 files changed

+54
-9
lines changed

8 files changed

+54
-9
lines changed

.changeset/plain-chairs-tie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix: correct detection of externally defined types in `no-unused-props` rule

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,11 @@ export default createRule('no-unused-props', {
8080
return shouldIgnore(typeStr) || (symbolName ? shouldIgnore(symbolName) : false);
8181
}
8282

83-
function isExternalType(type: ts.Type): boolean {
84-
const symbol = type.getSymbol();
85-
if (!symbol) return false;
86-
83+
function isInternalProperty(symbol: ts.Symbol): boolean {
8784
const declarations = symbol.getDeclarations();
8885
if (!declarations || declarations.length === 0) return false;
8986

90-
const sourceFile = declarations[0].getSourceFile();
91-
return sourceFile.fileName !== fileName;
87+
return declarations.every((decl) => decl.getSourceFile().fileName === fileName);
9288
}
9389

9490
/**
@@ -200,7 +196,6 @@ export default createRule('no-unused-props', {
200196
if (checkedTypes.has(typeStr)) return;
201197
checkedTypes.add(typeStr);
202198
if (shouldIgnoreType(type)) return;
203-
if (!checkImportedTypes && isExternalType(type)) return;
204199

205200
const properties = typeChecker.getPropertiesOfType(type);
206201
const baseTypes = type.getBaseTypes();
@@ -225,6 +220,7 @@ export default createRule('no-unused-props', {
225220

226221
for (const prop of properties) {
227222
if (isBuiltInProperty(prop)) continue;
223+
if (!checkImportedTypes && !isInternalProperty(prop)) continue;
228224

229225
const propName = prop.getName();
230226
const currentPath = [...parentPath, propName];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: "'child2' is an unused Props property."
2+
line: 9
3+
column: 6
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
import type { FooDTO } from './shared-types';
3+
4+
interface Props extends FooDTO {
5+
child1: string;
6+
child2: number;
7+
}
8+
9+
let { foo, child1 }: Props = $props();
10+
</script>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
- message: "'name' is an unused Props property."
22
line: 6
33
column: 6
4-
endLine: 6
5-
endColumn: 20
64
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
export interface BaseProps {
22
name: string;
33
}
4+
5+
export interface FooDTO {
6+
foo: string;
7+
bar: number;
8+
baz: BazDTO;
9+
}
10+
11+
interface BazDTO {
12+
qux: string;
13+
quux: number;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
import type { FooDTO } from './shared-types';
3+
4+
interface Props extends FooDTO {
5+
child1: string;
6+
child2: number;
7+
}
8+
9+
let { foo, child1, child2 }: Props = $props();
10+
</script>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
export interface BaseProps {
22
name: string;
33
}
4+
5+
export interface FooDTO {
6+
foo: string;
7+
bar: number;
8+
baz: BazDTO;
9+
}
10+
11+
interface BazDTO {
12+
qux: string;
13+
quux: number;
14+
}

0 commit comments

Comments
 (0)