Skip to content
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

fix: false positive for kebab-case with svelte v5 in svelte/no-unused-svelte-ignore #772

Merged
merged 2 commits into from
Jun 9, 2024
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/ninety-poets-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix: false positive for kebab-case with svelte v5 in `svelte/no-unused-svelte-ignore`
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | undefined> = {
'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;
Expand All @@ -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;
};

Expand Down Expand Up @@ -75,6 +92,7 @@ function extractSvelteIgnore(
if (trimmed) {
results.push({
code: trimmed,
codeForV5: V5_REPLACEMENTS[trimmed] || trimmed.replace(/-/gu, '_'),
range: [start, end],
token
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AST } from 'svelte-eslint-parser';
import type {} from 'svelte'; // FIXME: Workaround to get type information for "svelte/compiler"

Check warning on line 2 in packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Workaround to get type...'
import * as compiler from 'svelte/compiler';
import type { SourceMapMappings } from '@jridgewell/sourcemap-codec';
import { decode } from '@jridgewell/sourcemap-codec';
Expand Down Expand Up @@ -45,6 +45,7 @@

const CSS_WARN_CODES = new Set([
'css-unused-selector',
'css_unused_selector',
'css-invalid-global',
'css-invalid-global-selector'
]);
Expand Down Expand Up @@ -481,7 +482,9 @@
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);
Expand All @@ -497,7 +500,9 @@
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);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<span tabindex="0">
<span class="element"></span>
</span>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading