- Notifications
You must be signed in to change notification settings - Fork 20.9k
chore(web): add ESLint rules for i18n JSON validation #30491
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
Merged
+160 −9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import fs from 'node:fs' | ||
| import path, { normalize, sep } from 'node:path' | ||
| | ||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| export default { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Ensure non-English JSON files don\'t have extra keys not present in en-US', | ||
| }, | ||
| fixable: 'code', | ||
| }, | ||
| create(context) { | ||
| return { | ||
| Program(node) { | ||
| const { filename, sourceCode } = context | ||
| | ||
| if (!filename.endsWith('.json')) | ||
| return | ||
| | ||
| const parts = normalize(filename).split(sep) | ||
| // e.g., i18n/ar-TN/common.json -> jsonFile = common.json, lang = ar-TN | ||
| const jsonFile = parts.at(-1) | ||
| const lang = parts.at(-2) | ||
| | ||
| // Skip English files | ||
| if (lang === 'en-US') | ||
| return | ||
| | ||
| let currentJson = {} | ||
| let englishJson = {} | ||
| | ||
| try { | ||
| currentJson = JSON.parse(sourceCode.text) | ||
| // Look for the same filename in en-US folder | ||
| // e.g., i18n/ar-TN/common.json -> i18n/en-US/common.json | ||
| const englishFilePath = path.join(path.dirname(filename), '..', 'en-US', jsonFile ?? '') | ||
| englishJson = JSON.parse(fs.readFileSync(englishFilePath, 'utf8')) | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| } | ||
| catch (error) { | ||
| context.report({ | ||
| node, | ||
| message: `Error parsing JSON: ${error instanceof Error ? error.message : String(error)}`, | ||
| }) | ||
| return | ||
| } | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| const extraKeys = Object.keys(currentJson).filter( | ||
| key => !Object.prototype.hasOwnProperty.call(englishJson, key), | ||
| ) | ||
| | ||
| for (const key of extraKeys) { | ||
| context.report({ | ||
| node, | ||
| message: `Key "${key}" is present in ${lang}/${jsonFile} but not in en-US/${jsonFile}`, | ||
| fix(fixer) { | ||
| const newJson = Object.fromEntries( | ||
| Object.entries(currentJson).filter(([k]) => !extraKeys.includes(k)), | ||
| ) | ||
| | ||
| const newText = `${JSON.stringify(newJson, null, 2)}\n` | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| return fixer.replaceText(node, newText) | ||
| }, | ||
| }) | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| } | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { cleanJsonText } from '../utils.js' | ||
| | ||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| export default { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Ensure i18n JSON keys are flat and valid as object paths', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| return { | ||
| Program(node) { | ||
| const { filename, sourceCode } = context | ||
| | ||
| if (!filename.endsWith('.json')) | ||
| return | ||
| | ||
| let json | ||
| try { | ||
| json = JSON.parse(cleanJsonText(sourceCode.text)) | ||
| } | ||
| catch { | ||
| context.report({ | ||
| node, | ||
| message: 'Invalid JSON format', | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| }) | ||
| return | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| } | ||
| | ||
| const keys = Object.keys(json) | ||
| const keyPrefixes = new Set() | ||
| | ||
| for (const key of keys) { | ||
| if (key.includes('.')) { | ||
| const parts = key.split('.') | ||
| for (let i = 1; i < parts.length; i++) { | ||
| const prefix = parts.slice(0, i).join('.') | ||
| if (keys.includes(prefix)) { | ||
| context.report({ | ||
| node, | ||
| message: `Invalid key structure: '${key}' conflicts with '${prefix}'`, | ||
| }) | ||
| } | ||
| keyPrefixes.add(prefix) | ||
| } | ||
| } | ||
| } | ||
| | ||
| for (const key of keys) { | ||
| if (keyPrefixes.has(key)) { | ||
| context.report({ | ||
| node, | ||
| message: `Invalid key structure: '${key}' is a prefix of another key`, | ||
| }) | ||
| } | ||
| } | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const cleanJsonText = (text) => { | ||
| const cleaned = text.replaceAll(/,\s*\}/g, '}') | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| try { | ||
| JSON.parse(cleaned) | ||
| return cleaned | ||
| } | ||
| catch { | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
| return text | ||
| } | ||
| } | ||
crazywoola marked this conversation as resolved. Show resolved Hide resolved | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.