chore(plugin): add autofix for i18n json

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei 2024-09-28 15:30:39 +08:00
parent 234cdd3c71
commit 4eb47d4216
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
2 changed files with 11 additions and 2 deletions

View File

@ -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)
},
})
}
},

View File

@ -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({