Skip to content

Commit d6f08ea

Browse files
committed
🆙 update(no-missing-keys): support i18n functional component
1 parent f568eed commit d6f08ea

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

docs/rules/no-missing-keys.md

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You can be detected with this rule the following:
1515
- `$tc`
1616
- `tc`
1717
- `v-t`
18+
- `<i18n>`
1819

1920
:-1: Examples of **incorrect** code for this rule:
2021

@@ -34,6 +35,8 @@ localization codes:
3435
<p>{{ $t('hi) }}</p>
3536
<!-- ✗ BAD -->
3637
<p v-t="'hi'"></p>
38+
<!-- ✗ BAD -->
39+
<i18n path="hi" tag="p"></i18n>
3740
</div>
3841
</template>
3942
```
@@ -68,6 +71,8 @@ localization codes:
6871
<p>{{ $t('hello') }}</p>
6972
<!-- ✗ GOOD -->
7073
<p v-t="'hello'"></p>
74+
<!-- ✗ GOOD -->
75+
<i18n path="hello" tag="p"></i18n>
7176
</div>
7277
</template>
7378
```

lib/rules/no-missing-keys.js

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ function create (context) {
3434
checkDirective(context, localeDir, localeMessages, node)
3535
},
3636

37+
"VElement[name=i18n] > VStartTag > VAttribute[key.name='path']" (node) {
38+
checkComponent(context, localeDir, localeMessages, node)
39+
},
40+
41+
"VElement[name=i18n] > VStartTag > VAttribute[key.name.name='path']" (node) {
42+
checkComponent(context, localeDir, localeMessages, node)
43+
},
44+
3745
CallExpression (node) {
3846
checkCallExpression(context, localeDir, localeMessages, node)
3947
}
@@ -59,6 +67,20 @@ function checkDirective (context, localeDir, localeMessages, node) {
5967
}
6068
}
6169

70+
function checkComponent (context, localeDir, localeMessages, node) {
71+
if (node.value && node.value.type === 'VLiteral') {
72+
const key = node.value.value
73+
if (!key) {
74+
// TODO: should be error
75+
return
76+
}
77+
const missings = findMissingsFromLocaleMessages(localeMessages, key, localeDir)
78+
if (missings.length) {
79+
missings.forEach(missing => context.report({ node, ...missing }))
80+
}
81+
}
82+
}
83+
6284
function checkCallExpression (context, localeDir, localeMessages, node) {
6385
const funcName = (node.callee.type === 'MemberExpression' && node.callee.property.name) || node.callee.name
6486

tests/lib/rules/no-missing-keys.js

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ tester.run('no-missing-keys', rule, {
9191
`'missing' does not exist in '${resolve('en.json')}'`,
9292
`'missing' does not exist in '${resolve('ja.json')}'`
9393
]
94+
}, {
95+
// using <i18n> functional component in template block
96+
settings,
97+
code: `<template>
98+
<div id="app">
99+
<i18n path="missing"/>
100+
</div>
101+
</template>`,
102+
errors: [
103+
`'missing' does not exist in '${resolve('en.json')}'`,
104+
`'missing' does not exist in '${resolve('ja.json')}'`
105+
]
94106
}, {
95107
// settings.vue-i18n.localeDir' error
96108
code: `$t('missing')`,

0 commit comments

Comments
 (0)