Skip to content
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/strange-eagles-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix(no-unused-props): false positives for `ComponentProps<any>`
6 changes: 4 additions & 2 deletions packages/eslint-plugin-svelte/src/rules/no-unused-props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRule } from '../utils/index.js';
import { getTypeScriptTools } from '../utils/ts-utils/index.js';
import { getTypeScriptTools, isAnyType } from '../utils/ts-utils/index.js';
import type { TSESTree } from '@typescript-eslint/types';
import type ts from 'typescript';
import { findVariable } from '../utils/ast-utils.js';
Expand Down Expand Up @@ -75,11 +75,11 @@

const options = context.options[0] ?? {};

// TODO: Remove in v4

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 '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 82 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 @@ -341,7 +341,9 @@
if (parentPath.length === 0) {
const indexType = propsType.getStringIndexType();
const numberIndexType = propsType.getNumberIndexType();
const hasIndexSignature = Boolean(indexType) || Boolean(numberIndexType);
const hasIndexSignature =
Boolean(indexType && !isAnyType(indexType, tools!.ts)) ||
Boolean(numberIndexType && !isAnyType(numberIndexType, tools!.ts));

if (hasIndexSignature && !hasRestElement(declaredPropertyNames)) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
import type { ComponentProps } from 'svelte';

interface Props extends ComponentProps<any> {
a: string;
}

let { a }: Props = $props();
</script>

<p>{a}</p>
41 changes: 22 additions & 19 deletions packages/eslint-plugin-svelte/tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ export function getRuleFixturesRoot(ruleName: string): string {
}

function fileNameSuffix(fileName: string): string {
return fileName.match(/\.svelte\.(?:j|t)s$/u) ? fileName.slice(fileName.length - 10) : path.extname(fileName);
return fileName.match(/\.svelte\.(?:j|t)s$/u)
? fileName.slice(fileName.length - 10)
: path.extname(fileName);
}

function isSvelteFile(fileName): boolean {
return fileName.match(/\.svelte(?:\.(?:j|t)s)?$/u);
function isSvelteFile(fileName: string): boolean {
return Boolean(fileName.match(/\.svelte(?:\.(?:j|t)s)?$/u));
}

/**
Expand Down Expand Up @@ -230,13 +232,12 @@ function writeFixtures(

const config = getConfig(ruleName, inputFile);

const parser =
isSvelteFile(inputFile)
? svelteParser
: path.extname(inputFile) === '.ts'
? typescriptParser
: undefined;
const { code, filename, options, ...verifyConfig } = config;
const parser = isSvelteFile(inputFile)
? svelteParser
: path.extname(inputFile) === '.ts'
? typescriptParser
: undefined;
const { code, filename, options, only, ...verifyConfig } = config;
const resolvedParser = verifyConfig.languageOptions?.parser ?? parser;
const result = linter.verify(
code,
Expand All @@ -251,7 +252,7 @@ function writeFixtures(
},
languageOptions: {
globals: globals.browser,
ecmaVersion:"latest",
ecmaVersion: 'latest',
sourceType: 'module',
...verifyConfig?.languageOptions,
parserOptions: {
Expand Down Expand Up @@ -320,12 +321,11 @@ function getConfig(ruleName: string, inputFile: string) {
? require(configFile)
: JSON.parse(fs.readFileSync(configFile, 'utf8'));
}
const parser =
isSvelteFile(filename)
? svelteParser
: path.extname(inputFile) === '.ts'
? typescriptParser
: undefined;
const parser = isSvelteFile(filename)
? svelteParser
: path.extname(inputFile) === '.ts'
? typescriptParser
: undefined;

const resolvedParser = config?.languageOptions?.parser
? require(config.languageOptions.parser)
Expand All @@ -335,7 +335,7 @@ function getConfig(ruleName: string, inputFile: string) {
...config,
languageOptions: {
globals: globals.browser,
ecmaVersion:"latest",
ecmaVersion: 'latest',
sourceType: 'module',
...config?.languageOptions,
parserOptions: {
Expand All @@ -359,7 +359,10 @@ function getConfig(ruleName: string, inputFile: string) {
}

function getRequirements(inputFile: string): Record<string, string> {
let requirementsFile: string = inputFile.replace(/(input|\+.+)(?:\.[a-z]+)+$/u, 'requirements.json');
let requirementsFile: string = inputFile.replace(
/(input|\+.+)(?:\.[a-z]+)+$/u,
'requirements.json'
);
if (!fs.existsSync(requirementsFile)) {
requirementsFile = path.join(path.dirname(inputFile), '_requirements.json');
}
Expand Down
Loading