Skip to content

fix: correct detection of externally defined types in no-unused-props rule #1135

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
Mar 17, 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/plain-chairs-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix: correct detection of externally defined types in `no-unused-props` rule
10 changes: 3 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 @@ -80,15 +80,11 @@ export default createRule('no-unused-props', {
return shouldIgnore(typeStr) || (symbolName ? shouldIgnore(symbolName) : false);
}

function isExternalType(type: ts.Type): boolean {
const symbol = type.getSymbol();
if (!symbol) return false;

function isInternalProperty(symbol: ts.Symbol): boolean {
const declarations = symbol.getDeclarations();
if (!declarations || declarations.length === 0) return false;

const sourceFile = declarations[0].getSourceFile();
return sourceFile.fileName !== fileName;
return declarations.every((decl) => decl.getSourceFile().fileName === fileName);
}

/**
Expand Down Expand Up @@ -200,7 +196,6 @@ export default createRule('no-unused-props', {
if (checkedTypes.has(typeStr)) return;
checkedTypes.add(typeStr);
if (shouldIgnoreType(type)) return;
if (!checkImportedTypes && isExternalType(type)) return;

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

for (const prop of properties) {
if (isBuiltInProperty(prop)) continue;
if (!checkImportedTypes && !isInternalProperty(prop)) continue;

const propName = prop.getName();
const currentPath = [...parentPath, propName];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: "'child2' is an unused Props property."
line: 9
column: 6
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
import type { FooDTO } from './shared-types';

interface Props extends FooDTO {
child1: string;
child2: number;
}

let { foo, child1 }: Props = $props();
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
- message: "'name' is an unused Props property."
line: 6
column: 6
endLine: 6
endColumn: 20
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export interface BaseProps {
name: string;
}

export interface FooDTO {
foo: string;
bar: number;
baz: BazDTO;
}

interface BazDTO {
qux: string;
quux: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
import type { FooDTO } from './shared-types';

interface Props extends FooDTO {
child1: string;
child2: number;
}

let { foo, child1, child2 }: Props = $props();
</script>
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export interface BaseProps {
name: string;
}

export interface FooDTO {
foo: string;
bar: number;
baz: BazDTO;
}

interface BazDTO {
qux: string;
quux: number;
}