Skip to content

Commit 1fc1589

Browse files
committed
🆙 update(no-dynamic-keys): support i18n functional component
1 parent 7b3437a commit 1fc1589

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

docs/rules/no-dynamic-keys.md

Lines changed: 5 additions & 0 deletions
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(msg) }}</p>
3536
<!-- ✗ BAD -->
3637
<p v-t="msg"></p>
38+
<!-- ✗ BAD -->
39+
<i18n :path="msg"></i18n>
3740
</div>
3841
</template>
3942
```
@@ -78,6 +81,8 @@ localization codes:
7881
<p>{{ $t('hello') }}</p>
7982
<!-- ✓ GOOD -->
8083
<p v-t="'hello'"></p>
84+
<!-- ✓ GOOD -->
85+
<i18n path="hello"></i18n>
8186
</div>
8287
</template>
8388
```

lib/rules/no-dynamic-keys.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ function checkDirective (context, node) {
1616
}
1717
}
1818

19+
function checkComponent (context, node) {
20+
if ((node.name.type === 'VIdentifier' && node.name.name === 'bind') &&
21+
(node.argument.type === 'VIdentifier' && node.argument.name === 'path') &&
22+
(node.parent.value && node.parent.value.type === 'VExpressionContainer') &&
23+
(node.parent.value.expression && node.parent.value.expression.type === 'Identifier')) {
24+
const name = node.parent.value.expression.name
25+
context.report({
26+
node,
27+
message: `'${name}' dynamic key is used'`
28+
})
29+
}
30+
}
31+
1932
function checkCallExpression (context, node) {
2033
const funcName = (node.callee.type === 'MemberExpression' && node.callee.property.name) || node.callee.name
2134

@@ -43,6 +56,10 @@ function create (context) {
4356
checkDirective(context, node)
4457
},
4558

59+
'VElement[name=i18n] > VStartTag > VAttribute[directive=true] > VDirectiveKey' (node) {
60+
checkComponent(context, node)
61+
},
62+
4663
CallExpression (node) {
4764
checkCallExpression(context, node)
4865
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ tester.run('no-dynamic-keys', rule, {
5656
errors: [
5757
`'missing' dynamic key is used'`
5858
]
59+
}, {
60+
// using <i18n> functional component in template block
61+
code: `<template>
62+
<i18n :path="missing"/>
63+
</template>`,
64+
errors: [
65+
`'missing' dynamic key is used'`
66+
]
5967
}, {
6068
// using custom directive in template block
6169
code: `<template>

0 commit comments

Comments
 (0)