From 4eb47d42168b1002a63b0fd16570d564800ca823 Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 28 Sep 2024 15:30:39 +0800 Subject: [PATCH] chore(plugin): add autofix for i18n json Signed-off-by: Innei --- plugins/eslint-check-i18n-json.js | 11 ++++++++++- plugins/eslint-recursive-sort.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/eslint-check-i18n-json.js b/plugins/eslint-check-i18n-json.js index af98d8a82..fccdbe1c0 100644 --- a/plugins/eslint-check-i18n-json.js +++ b/plugins/eslint-check-i18n-json.js @@ -71,7 +71,7 @@ export default { category: "Possible Errors", recommended: true, }, - fixable: null, + fixable: "code", // 将 fixable 设置为 "code" }, create(context) { return { @@ -109,6 +109,15 @@ export default { context.report({ node, message: `Key "${key}" is present in ${lang}.json but not in en.json for namespace "${namespace}"`, + fix(fixer) { + const newJson = Object.fromEntries( + Object.entries(currentJson).filter(([k]) => !extraKeys.includes(k)), + ) + + const newText = `${JSON.stringify(newJson, null, 2)}\n` + + return fixer.replaceText(node, newText) + }, }) } }, diff --git a/plugins/eslint-recursive-sort.js b/plugins/eslint-recursive-sort.js index 113a070a3..924cbff62 100644 --- a/plugins/eslint-recursive-sort.js +++ b/plugins/eslint-recursive-sort.js @@ -34,7 +34,7 @@ export default { try { const json = JSON.parse(text) const sortedJson = sortObjectKeys(json) - const sortedText = JSON.stringify(sortedJson, null, 2) + const sortedText = `${JSON.stringify(sortedJson, null, 2)}\n` if (text.trim() !== sortedText.trim()) { context.report({