Repository: ota-meshi/eslint-plugin-svelte (ts)

Issue:
`no-unused-svelte-ignore` false positive for a11y warnings on elements with inner element
### Before You File a Bug Report Please Confirm You Have Done The Following...

- [X] I have tried restarting my IDE and the issue persists.
- [X] I have updated to the latest version of the packages.

### What version of ESLint are you using?

8.35.0

### What version of `eslint-plugin-svelte` are you using?

2.19.1

### What did you do?

<details>
<summary>Configuration</summary>

```
plugin:svelte/recommended
```
</details>

```svelte
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<span tabindex="0">
    <span class="element" />
</span>
```


### What did you expect to happen?

no error, as the compile warning is being suppressed by the `svelte-ignore` comment

### What actually happened?

```
[1:20]: svelte-ignore comment is used, but not warned (svelte/no-unused-svelte-ignore)
[2:1]: A11y: noninteractive element cannot have nonnegative tabIndex value(a11y-no-noninteractive-tabindex) (svelte/valid-compile)
```

### Link to **GitHub Repo** with Minimal Reproducible Example

https://ota-meshi.github.io/eslint-plugin-svelte/playground/#eJw1y8EKgzAQhOFX2e59sV7F+iS5rHEogXRTzCKW4rsbKYU5fT/z5VgW8MDjTYTqhuyQ9LSygrTvP2KlzZI5Vo2eNojrnGzBTiJTsLG+1ehvj8D3wI2JfiFmrbUpMl4wD0zddequOPFxAsAxKvI=

### Additional comments

the `selte/valid-compile` warning appears even though the `svelte-ignore` comment successfully suppresses the warning when running `svelte-check`

Pull request description:
fix: false positive for element in `svelte/no-unused-svelte-ignore`
fixes #386

Patch:
diff --git a/.changeset/lovely-hotels-breathe.md b/.changeset/lovely-hotels-breathe.md
new file mode 100644
index 0000000..01c75f8
--- /dev/null
+++ b/.changeset/lovely-hotels-breathe.md
@@ -0,0 +1,5 @@
+---
+"eslint-plugin-svelte": patch
+---
+
+fix: false positive for element in `svelte/no-unused-svelte-ignore`
diff --git a/src/shared/svelte-compile-warns/index.ts b/src/shared/svelte-compile-warns/index.ts
index 9c8e053..85c8dbe 100644
--- a/src/shared/svelte-compile-warns/index.ts
+++ b/src/shared/svelte-compile-warns/index.ts
@@ -511,10 +511,30 @@ function processIgnore(
 
   /** Get warning node */
   function getWarningNode(warning: Warning) {
-    const index = getWarningIndex(warning)
-    if (index == null) {
-      return null
+    const indexes = getWarningIndexes(warning)
+    if (indexes.start != null) {
+      const node = getWarningTargetNodeFromIndex(indexes.start)
+      if (node) {
+        return node
+      }
+      if (indexes.end != null) {
+        const center = Math.floor(
+          indexes.start + (indexes.end - indexes.start) / 2,
+        )
+        return getWarningTargetNodeFromIndex(center)
+      }
+    }
+    if (indexes.end != null) {
+      return getWarningTargetNodeFromIndex(indexes.end)
     }
+
+    return null
+  }
+
+  /**
+   * Get warning target node from the given index
+   */
+  function getWarningTargetNodeFromIndex(index: number) {
     let targetNode = sourceCode.getNodeByRangeIndex(index)
     while (targetNode) {
       if (
@@ -535,18 +555,14 @@ function processIgnore(
       }
       targetNode = targetNode.parent || null
     }
-
     return null
   }
 
   /** Get warning index */
-  function getWarningIndex(warning: Warning) {
+  function getWarningIndexes(warning: Warning) {
     const start = warning.start && sourceCode.getIndexFromLoc(warning.start)
     const end = warning.end && sourceCode.getIndexFromLoc(warning.end)
-    if (start != null && end != null) {
-      return Math.floor(start + (end - start) / 2)
-    }
-    return start ?? end
+    return { start, end }
   }
 }
 


Test patch:
diff --git a/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-input.svelte b/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-input.svelte
new file mode 100644
index 0000000..6f28adf
--- /dev/null
+++ b/tests/fixtures/rules/no-unused-svelte-ignore/valid/element-ignore01-input.svelte
@@ -0,0 +1,4 @@
+<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
+<span tabindex="0">
+  <span class="element" />
+</span>
diff --git a/tests/fixtures/rules/valid-compile/valid/ignore-input.svelte b/tests/fixtures/rules/valid-compile/valid/ignore01-input.svelte
similarity index 100%
rename from tests/fixtures/rules/valid-compile/valid/ignore-input.svelte
rename to tests/fixtures/rules/valid-compile/valid/ignore01-input.svelte
diff --git a/tests/fixtures/rules/valid-compile/valid/ignore02-input.svelte b/tests/fixtures/rules/valid-compile/valid/ignore02-input.svelte
new file mode 100644
index 0000000..6f28adf
--- /dev/null
+++ b/tests/fixtures/rules/valid-compile/valid/ignore02-input.svelte
@@ -0,0 +1,4 @@
+<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
+<span tabindex="0">
+  <span class="element" />
+</span>