-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule: no missing translations
- Loading branch information
1 parent
39a0293
commit c047ee1
Showing
7 changed files
with
252 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import noFunctionWithoutLogging from './rules/no-function-without-logging' | ||
import noMissingTranslations from './rules/no-missing-translations' | ||
|
||
const rules = { | ||
'no-function-without-logging': noFunctionWithoutLogging, | ||
'no-missing-translations': noMissingTranslations, | ||
} | ||
|
||
export { rules } |
This file contains 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,81 @@ | ||
import { jest } from '@jest/globals' | ||
import { RuleTester } from '@typescript-eslint/rule-tester' | ||
import noMissingTranslations from '../no-missing-translations' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
jest.mock('fs', () => { | ||
const actualFs = jest.requireActual<typeof import('fs')>('fs') | ||
const newFs = { | ||
...actualFs, | ||
readFileSync: jest.fn((file: string) => { | ||
if (file === 'en.json') { | ||
return JSON.stringify({ | ||
'Existing key': 'Existing value', | ||
'Key that only exists in en.json': 'Value', | ||
}) | ||
} | ||
if (file === 'nl.json') { | ||
return JSON.stringify({ | ||
'Existing key': 'Existing value', | ||
}) | ||
} | ||
}), | ||
} | ||
return { | ||
__esModule: true, | ||
...newFs, | ||
} | ||
}) | ||
|
||
ruleTester.run('no-missing-translations', noMissingTranslations, { | ||
valid: [ | ||
{ | ||
name: 'Function declaration', | ||
code: "i18n.t('Existing key')", | ||
options: [ | ||
{ | ||
translationFiles: ['en.json', 'nl.json'], | ||
}, | ||
], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'Missing translation key in multiple files', | ||
code: 'i18n.t("Missing key")', | ||
errors: [ | ||
{ | ||
messageId: 'missingTranslationKey', | ||
data: { | ||
translationKey: 'Missing key', | ||
invalidFiles: "'en.json', 'nl.json'", | ||
}, | ||
}, | ||
], | ||
options: [ | ||
{ | ||
translationFiles: ['en.json', 'nl.json'], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Missing translation key in one file', | ||
code: 'i18n.t("Key that only exists in en.json")', | ||
errors: [ | ||
{ | ||
messageId: 'missingTranslationKey', | ||
data: { | ||
translationKey: 'Key that only exists in en.json', | ||
invalidFiles: "'nl.json'", | ||
}, | ||
}, | ||
], | ||
options: [ | ||
{ | ||
translationFiles: ['en.json', 'nl.json'], | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
This file contains 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 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,93 @@ | ||
import { readFileSync } from 'fs' | ||
|
||
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils' | ||
import { RuleContext } from '@typescript-eslint/utils/ts-eslint' | ||
import { isIdentifier, isLiteral, isMemberExpression } from '../utils' | ||
|
||
const createRule = ESLintUtils.RuleCreator(() => 'https://github.com/observation/eslint-rules') | ||
|
||
type MessageIds = 'missingTranslationKey' | ||
type Options = [ | ||
{ | ||
translationFiles: string[] | ||
}, | ||
] | ||
|
||
const checkTranslationFileForKey = (translationFile: string, translationKey: string) => { | ||
const fileContent = readFileSync(translationFile, 'utf8') | ||
const jsonData = JSON.parse(fileContent) | ||
return !(translationKey in jsonData) | ||
} | ||
|
||
const checkCallExpression = ( | ||
context: Readonly<RuleContext<MessageIds, unknown[]>>, | ||
node: TSESTree.CallExpression, | ||
translationFiles: string[], | ||
) => { | ||
if (isMemberExpression(node.callee)) { | ||
const { object, property } = node.callee | ||
|
||
if (isIdentifier(object) && isIdentifier(property)) { | ||
const [argument] = node.arguments | ||
if (object.name === 'i18n' && property.name === 't' && isLiteral(argument)) { | ||
const translationKey = argument.value | ||
|
||
if (typeof translationKey === 'string') { | ||
const invalidTranslationFiles = translationFiles.filter((translationFile) => | ||
checkTranslationFileForKey(translationFile, translationKey), | ||
) | ||
|
||
if (invalidTranslationFiles.length > 0) { | ||
context.report({ | ||
node, | ||
messageId: 'missingTranslationKey', | ||
data: { | ||
translationKey, | ||
invalidFiles: invalidTranslationFiles.map((file) => `'${file}'`).join(', '), | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const noMissingTranslations = createRule<Options, MessageIds>({ | ||
create(context, options) { | ||
const [{ translationFiles }] = options | ||
return { | ||
CallExpression: (node) => checkCallExpression(context, node, translationFiles), | ||
} | ||
}, | ||
name: 'no-missing-translations', | ||
meta: { | ||
docs: { | ||
description: | ||
'All translation keys used in the codebase should have a corresponding translation in the translation files', | ||
}, | ||
messages: { | ||
missingTranslationKey: "Translation key '{{ translationKey }}' is missing in: {{ invalidFiles }}", | ||
}, | ||
type: 'problem', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
translationFiles: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [ | ||
{ | ||
translationFiles: [], | ||
}, | ||
], | ||
}) | ||
|
||
export default noMissingTranslations |
This file contains 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