Skip to content

Commit 8559e03

Browse files
authored
Add no-missing-keys-in-other-locales rule and change no-missing-keys rule to not report if there is one matching key (#148)
* Add `no-missing-keys-in-other-locales` rule and change `no-missing-keys` rule to not report if there is one matching key * fixes
1 parent c6e1263 commit 8559e03

File tree

19 files changed

+929
-96
lines changed

19 files changed

+929
-96
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
| [@intlify/vue-i18n/<wbr>key-format-style](./key-format-style.html) | enforce specific casing for localization keys | |
2121
| [@intlify/vue-i18n/<wbr>no-duplicate-keys-in-locale](./no-duplicate-keys-in-locale.html) | disallow duplicate localization keys within the same locale | |
2222
| [@intlify/vue-i18n/<wbr>no-dynamic-keys](./no-dynamic-keys.html) | disallow localization dynamic keys at localization methods | |
23+
| [@intlify/vue-i18n/<wbr>no-missing-keys-in-other-locales](./no-missing-keys-in-other-locales.html) | disallow missing locale message keys in other locales | |
2324
| [@intlify/vue-i18n/<wbr>no-unused-keys](./no-unused-keys.html) | disallow unused localization keys | :black_nib: |
2425

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# @intlify/vue-i18n/no-missing-keys-in-other-locales
2+
3+
> disallow missing locale message keys in other locales
4+
5+
This rule warns if a key with the same path as the key of resource does not exist in another locale.
6+
7+
## :book: Rule Details
8+
9+
:-1: Examples of **incorrect** code for this rule:
10+
11+
locale messages:
12+
13+
```json5
14+
{
15+
"en": {
16+
/* ✓ GOOD */
17+
"hello": "Hello!",
18+
/* ✗ BAD */
19+
"goodbye": "Goodbye!"
20+
},
21+
"ja": {
22+
"hello": "こんにちは!"
23+
}
24+
}
25+
```
26+
27+
:+1: Examples of **correct** code for this rule:
28+
29+
locale messages:
30+
31+
```json5
32+
{
33+
"en": {
34+
/* ✓ GOOD */
35+
"hello": "Hello!",
36+
"goodbye": "Goodbye!"
37+
},
38+
"ja": {
39+
"hello": "こんにちは!",
40+
"goodbye": "さようなら!"
41+
}
42+
}
43+
```
44+
45+
## Options
46+
47+
```json
48+
{
49+
"@intlify/vue-i18n/no-missing-keys-in-other-locales": ["error",
50+
{
51+
"ignoreLocales": []
52+
}
53+
]
54+
}
55+
```
56+
57+
- `ignoreLocales`: If you specify an array of locales, that locale is allowed even if it is missing.

lib/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import keyFormatStyle from './rules/key-format-style'
33
import noDuplicateKeysInLocale from './rules/no-duplicate-keys-in-locale'
44
import noDynamicKeys from './rules/no-dynamic-keys'
55
import noHtmlMessages from './rules/no-html-messages'
6+
import noMissingKeysInOtherLocales from './rules/no-missing-keys-in-other-locales'
67
import noMissingKeys from './rules/no-missing-keys'
78
import noRawText from './rules/no-raw-text'
89
import noUnusedKeys from './rules/no-unused-keys'
@@ -14,6 +15,7 @@ export = {
1415
'no-duplicate-keys-in-locale': noDuplicateKeysInLocale,
1516
'no-dynamic-keys': noDynamicKeys,
1617
'no-html-messages': noHtmlMessages,
18+
'no-missing-keys-in-other-locales': noMissingKeysInOtherLocales,
1719
'no-missing-keys': noMissingKeys,
1820
'no-raw-text': noRawText,
1921
'no-unused-keys': noUnusedKeys,

lib/rules/no-duplicate-keys-in-locale.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface DictData {
2323

2424
interface PathStack {
2525
otherDictionaries: DictData[]
26-
keyPath: string
26+
keyPath: (string | number)[]
2727
locale: string | null
2828
node?: JSONAST.JSONNode | YAMLAST.YAMLNode
2929
upper?: PathStack
@@ -50,7 +50,7 @@ function create(context: RuleContext): RuleListener {
5050
return createInitLocalePathStack(locale, otherLocaleMessages)
5151
} else {
5252
return {
53-
keyPath: '',
53+
keyPath: [],
5454
locale: null,
5555
otherDictionaries: []
5656
}
@@ -61,7 +61,7 @@ function create(context: RuleContext): RuleListener {
6161
otherLocaleMessages: LocaleMessage[]
6262
): PathStack {
6363
return {
64-
keyPath: '',
64+
keyPath: [],
6565
locale,
6666
otherDictionaries: otherLocaleMessages.map(lm => {
6767
return {
@@ -118,12 +118,13 @@ function create(context: RuleContext): RuleListener {
118118
source: dict.source
119119
}
120120
})
121-
const keyPath = joinPath(pathStack.keyPath, key)
121+
const keyPath = [...pathStack.keyPath, key]
122+
const keyPathStr = joinPath(...keyPath)
122123
const nextOtherDictionaries: DictData[] = []
123124
for (const value of keyOtherValues) {
124125
if (typeof value.value === 'string') {
125126
context.report({
126-
message: `duplicate key '${keyPath}' in '${
127+
message: `duplicate key '${keyPathStr}' in '${
127128
pathStack.locale
128129
}'. "${getMessageFilepath(
129130
value.source.fullpath
@@ -141,7 +142,7 @@ function create(context: RuleContext): RuleListener {
141142
pushKey(
142143
existsKeyNodes[pathStack.locale] ||
143144
(existsKeyNodes[pathStack.locale] = {}),
144-
keyPath,
145+
keyPathStr,
145146
reportNode
146147
)
147148

0 commit comments

Comments
 (0)