Skip to content

Commit

Permalink
fix: wrong suggestion for remove all unused keys in `vue-i18n/no-unus…
Browse files Browse the repository at this point in the history
…ed-keys` rule (#474)

* fix: wrong suggestion for remove all unused keys in `no-unused-keys` rule

* Create ninety-snakes-scream.md
  • Loading branch information
ota-meshi authored Feb 20, 2024
1 parent 56b1761 commit ebd7353
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-snakes-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@intlify/eslint-plugin-vue-i18n": patch
---

fix: wrong suggestion for remove all unused keys in `vue-i18n/no-unused-keys` rule
32 changes: 20 additions & 12 deletions lib/rules/no-unused-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,36 +208,44 @@ function create(context: RuleContext): RuleListener {
}

function* fixAllRemoveKeys(fixer: RuleFixer, nodes: JSONAST.JSONNode[]) {
const ranges = nodes.map(node => fixRemoveRange(node))

const removed = new Set<JSONAST.JSONNode>()
let preLast = 0
for (const range of ranges) {
yield fixer.removeRange([Math.max(preLast, range[0]), range[1]])
for (const node of nodes) {
const range = fixRemoveRange(node, removed)
const start = Math.max(preLast, range[0])
yield fixer.removeRange([start, range[1]])
preLast = range[1]
}
}

/**
* @param {JSONNode} node
*/
function fixRemoveRange(node: JSONAST.JSONNode): Range {
function fixRemoveRange(
node: JSONAST.JSONNode,
removedNodes: Set<JSONAST.JSONNode> = new Set()
): Range {
const parent = node.parent!
let removeNode
let isFirst = false
let isLast = false
if (parent.type === 'JSONProperty') {
removeNode = parent
const index = parent.parent.properties.indexOf(parent)
const properties = parent.parent.properties.filter(
p => !removedNodes.has(p)
)
const index = properties.indexOf(parent)
isFirst = index === 0
isLast = index === parent.parent.properties.length - 1
isLast = index === properties.length - 1
} else {
removeNode = node
if (parent.type === 'JSONArrayExpression') {
const index = parent.elements.indexOf(node as never)
const elements = parent.elements.filter(
e => e == null || !removedNodes.has(e)
)
const index = elements.indexOf(node as never)
isFirst = index === 0
isLast = index === parent.elements.length - 1
isLast = index === elements.length - 1
}
}
removedNodes.add(removeNode)
const range: Range = [...removeNode.range]

if (isLast || isFirst) {
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/rules/no-unused-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ ${' '.repeat(6)}
- "하이"
</i18n>
<i18n locale="ja" lang="json5">
{,
{
"array-unuse": [
]
}
Expand Down Expand Up @@ -1647,7 +1647,7 @@ ${' '.repeat(6)}
- "하이"
</i18n>
<i18n locale="ja" lang="json5">
{,
{
"array-unuse": [
]
}
Expand Down Expand Up @@ -1718,7 +1718,7 @@ ${' '.repeat(6)}
- "하이"
</i18n>
<i18n locale="ja" lang="json5">
{,
{
"array-unuse": [
]
}
Expand Down

0 comments on commit ebd7353

Please sign in to comment.