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: wrong suggestion for remove all unused keys in vue-i18n/no-unused-keys rule #474

Merged
merged 2 commits into from
Feb 20, 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-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
Loading