From ebd7353ada4d65eec0f504887db0911a80819979 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 20 Feb 2024 10:44:14 +0900 Subject: [PATCH] fix: wrong suggestion for remove all unused keys in `vue-i18n/no-unused-keys` rule (#474) * fix: wrong suggestion for remove all unused keys in `no-unused-keys` rule * Create ninety-snakes-scream.md --- .changeset/ninety-snakes-scream.md | 5 +++++ lib/rules/no-unused-keys.ts | 32 +++++++++++++++++++----------- tests/lib/rules/no-unused-keys.ts | 6 +++--- 3 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 .changeset/ninety-snakes-scream.md diff --git a/.changeset/ninety-snakes-scream.md b/.changeset/ninety-snakes-scream.md new file mode 100644 index 00000000..62edcb04 --- /dev/null +++ b/.changeset/ninety-snakes-scream.md @@ -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 diff --git a/lib/rules/no-unused-keys.ts b/lib/rules/no-unused-keys.ts index d6133916..6bd2d7d5 100644 --- a/lib/rules/no-unused-keys.ts +++ b/lib/rules/no-unused-keys.ts @@ -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() 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 = 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) { diff --git a/tests/lib/rules/no-unused-keys.ts b/tests/lib/rules/no-unused-keys.ts index 2664109f..1f671420 100644 --- a/tests/lib/rules/no-unused-keys.ts +++ b/tests/lib/rules/no-unused-keys.ts @@ -1576,7 +1576,7 @@ ${' '.repeat(6)} - "하이" - {, + { "array-unuse": [ ] } @@ -1647,7 +1647,7 @@ ${' '.repeat(6)} - "하이" - {, + { "array-unuse": [ ] } @@ -1718,7 +1718,7 @@ ${' '.repeat(6)} - "하이" - {, + { "array-unuse": [ ] }