From 0ecab95f74a9bd88d831671c2048a821a2194ec6 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 9 Jun 2024 14:18:56 +0900 Subject: [PATCH] fix: false positive for kebab-case with svelte v5 in `svelte/no-unused-svelte-ignore` (#772) fixes #765 --- .changeset/ninety-poets-divide.md | 5 +++++ .../svelte-compile-warns/ignore-comment.ts | 18 ++++++++++++++++++ .../src/shared/svelte-compile-warns/index.ts | 9 +++++++-- .../html-comment-svelte4-requirements.json | 3 --- ...d-svelte-ignore01-svelte4-requirements.json | 3 --- ...d-svelte-ignore02-svelte4-requirements.json | 3 --- ...d-svelte-ignore03-svelte4-requirements.json | 3 --- .../script-comment01-svelte4-requirements.json | 3 --- .../script-comment02-svelte4-requirements.json | 3 --- .../transform-test-svelte4-requirements.json | 3 --- .../element-ignore01-svelte4-requirements.json | 3 --- .../valid/has-error-svelte4-requirements.json | 3 --- .../html-comment-svelte4-requirements.json | 3 --- .../valid/kebab-ignore-input.svelte | 4 ++++ .../script-comment-svelte4-requirements.json | 3 --- .../svelte-ignore01-svelte4-requirements.json | 3 --- .../svelte-ignore02-svelte4-requirements.json | 3 --- .../svelte-ignore03-svelte4-requirements.json | 3 --- .../svelte-ignore04-svelte4-requirements.json | 3 --- .../svelte-ignore05-svelte4-requirements.json | 3 --- .../svelte-ignore06-svelte4-requirements.json | 3 --- .../svelte-ignore07-svelte4-requirements.json | 3 --- .../valid/ts-lang01-svelte4-requirements.json | 3 --- 23 files changed, 34 insertions(+), 59 deletions(-) create mode 100644 .changeset/ninety-poets-divide.md delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/html-comment-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore01-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore02-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore03-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment01-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment02-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/transform-test-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/html-comment-svelte4-requirements.json create mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/kebab-ignore-input.svelte delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/script-comment-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore01-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore02-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore03-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore04-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore05-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore06-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore07-svelte4-requirements.json delete mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-svelte4-requirements.json diff --git a/.changeset/ninety-poets-divide.md b/.changeset/ninety-poets-divide.md new file mode 100644 index 000000000..89dc48715 --- /dev/null +++ b/.changeset/ninety-poets-divide.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-svelte": patch +--- + +fix: false positive for kebab-case with svelte v5 in `svelte/no-unused-svelte-ignore` diff --git a/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/ignore-comment.ts b/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/ignore-comment.ts index 4d3fada04..623909ab1 100644 --- a/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/ignore-comment.ts +++ b/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/ignore-comment.ts @@ -4,6 +4,22 @@ import { getSourceCode } from '../../utils/compat'; const SVELTE_IGNORE_PATTERN = /^\s*svelte-ignore/m; +/** + * Map of legacy code -> new code + * See https://github.com/sveltejs/svelte/blob/c9202a889612df3c2fcb369096a5573668be99d6/packages/svelte/src/compiler/utils/extract_svelte_ignore.js#L6 + */ +const V5_REPLACEMENTS: Record = { + 'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement', + 'module-script-reactive-declaration': 'reactive_declaration_module_script', + 'empty-block': 'block_empty', + 'avoid-is': 'attribute_avoid_is', + 'invalid-html-attribute': 'attribute_invalid_property_name', + 'a11y-structure': 'a11y_figcaption_parent', + 'illegal-attribute-character': 'attribute_illegal_colon', + 'invalid-rest-eachblock-binding': 'bind_invalid_each_rest', + 'unused-export-let': 'export_let_unused' +}; + export type IgnoreItemWithoutCode = { range: [number, number]; code: null; @@ -12,6 +28,7 @@ export type IgnoreItemWithoutCode = { export type IgnoreItem = { range: [number, number]; code: string; + codeForV5: string; // Code targeting Svelte v5. token: AST.Token | AST.Comment; }; @@ -75,6 +92,7 @@ function extractSvelteIgnore( if (trimmed) { results.push({ code: trimmed, + codeForV5: V5_REPLACEMENTS[trimmed] || trimmed.replace(/-/gu, '_'), range: [start, end], token }); diff --git a/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts b/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts index 20e184507..d5e3bcad4 100644 --- a/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts +++ b/packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts @@ -45,6 +45,7 @@ const STYLE_TRANSFORMS: Record const CSS_WARN_CODES = new Set([ 'css-unused-selector', + 'css_unused_selector', 'css-invalid-global', 'css-invalid-global-selector' ]); @@ -481,7 +482,9 @@ function processIgnore( while (node) { for (const comment of extractLeadingComments(context, node).reverse()) { const ignoreItem = ignoreComments.find( - (item) => item.token === comment && item.code === warning.code + (item) => + item.token === comment && + (item.code === warning.code || item.codeForV5 === warning.code) ); if (ignoreItem) { unusedIgnores.delete(ignoreItem); @@ -497,7 +500,9 @@ function processIgnore( for (const node of stripStyleElements) { for (const comment of extractLeadingComments(context, node).reverse()) { const ignoreItem = ignoreComments.find( - (item) => item.token === comment && CSS_WARN_CODES.has(item.code) + (item) => + item.token === comment && + (CSS_WARN_CODES.has(item.code) || CSS_WARN_CODES.has(item.codeForV5)) ); if (ignoreItem) { unusedIgnores.delete(ignoreItem); diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/html-comment-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/html-comment-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/html-comment-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore01-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore01-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore01-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore02-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore02-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore02-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore03-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore03-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/invalid-svelte-ignore03-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment01-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment01-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment01-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment02-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment02-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/script-comment02-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/transform-test-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/transform-test-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/invalid/transform-test-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/has-error-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/html-comment-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/html-comment-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/html-comment-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/kebab-ignore-input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/kebab-ignore-input.svelte new file mode 100644 index 000000000..4f0cb2449 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/kebab-ignore-input.svelte @@ -0,0 +1,4 @@ + + + + diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/script-comment-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/script-comment-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/script-comment-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore01-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore01-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore01-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore02-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore02-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore02-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore03-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore03-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore03-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore04-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore04-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore04-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore05-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore05-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore05-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore06-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore06-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore06-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore07-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore07-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/svelte-ignore07-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-svelte4-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-svelte4-requirements.json deleted file mode 100644 index b650cc296..000000000 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-unused-svelte-ignore/valid/ts-lang01-svelte4-requirements.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "svelte": "^3.0.0 || ^4.0.0" -}