Skip to content

Commit

Permalink
fix: false positive for element in svelte/no-unused-svelte-ignore (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 1, 2023
1 parent e00d582 commit 6422ee8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-hotels-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix: false positive for element in `svelte/no-unused-svelte-ignore`
34 changes: 25 additions & 9 deletions src/shared/svelte-compile-warns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 }
}
}

Expand Down
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>
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>

0 comments on commit 6422ee8

Please sign in to comment.