Skip to content

Commit 14dfda5

Browse files
kazuponota-meshi
andauthoredDec 9, 2024··
feat: add no-deprecated-v-t rule (#580)
* feat: add `no-deprecated-v-t` rule * docs: fix version * Create chilled-colts-destroy.md * Revert "docs: fix version" This reverts commit 7e88518. * fix: ref #580 (comment) * fix: add valid test --------- Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
1 parent 5207924 commit 14dfda5

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed
 

‎.changeset/chilled-colts-destroy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intlify/eslint-plugin-vue-i18n": minor
3+
---
4+
5+
feat: add `no-deprecated-v-t` rule

‎docs/rules/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [@intlify/vue-i18n/<wbr>no-deprecated-i18n-places-prop](./no-deprecated-i18n-places-prop.html) | disallow using deprecated `places` prop (Removed in Vue I18n 9.0.0+) | :star: |
1414
| [@intlify/vue-i18n/<wbr>no-deprecated-modulo-syntax](./no-deprecated-modulo-syntax.html) | enforce modulo interpolation to be named interpolation | :star::black_nib: |
1515
| [@intlify/vue-i18n/<wbr>no-deprecated-tc](./no-deprecated-tc.html) | disallow using deprecated `tc` or `$tc` (Deprecated in Vue I18n 10.0.0, removed fully in Vue I18n 11.0.0) | :star: |
16+
| [@intlify/vue-i18n/<wbr>no-deprecated-v-t](./no-deprecated-v-t.html) | disallow using deprecated `v-t` custom directive (Deprecated in Vue I18n 11.0.0, removed fully in Vue I18n 12.0.0) | :star: |
1617
| [@intlify/vue-i18n/<wbr>no-html-messages](./no-html-messages.html) | disallow use HTML localization messages | :star: |
1718
| [@intlify/vue-i18n/<wbr>no-i18n-t-path-prop](./no-i18n-t-path-prop.html) | disallow using `path` prop with `<i18n-t>` | :star::black_nib: |
1819
| [@intlify/vue-i18n/<wbr>no-missing-keys](./no-missing-keys.html) | disallow missing locale message key at localization methods | :star: |

‎docs/rules/no-deprecated-v-t.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: '@intlify/vue-i18n/no-deprecated-v-t'
3+
description: disallow using deprecated `v-t` custom directive (Deprecated in Vue I18n 11.0.0, removed fully in Vue I18n 12.0.0)
4+
since: v3.0.0
5+
---
6+
7+
# @intlify/vue-i18n/no-deprecated-v-t
8+
9+
> disallow using deprecated `v-t` custom directive (Deprecated in Vue I18n 11.0.0, removed fully in Vue I18n 12.0.0)
10+
11+
- :star: The `"extends": "plugin:@intlify/vue-i18n/recommended"` or `*.configs["flat/recommended"]` property in a configuration file enables this rule.
12+
13+
If you are migrating from Vue I18n v10 to v11, `v-t` custom direcitve should be replaced with `t` or `$t`.
14+
15+
## :book: Rule Details
16+
17+
This rule reports use of deprecated `v-t` custom directive (Deprecated in Vue I18n 11.0.0, removed fully in Vue I18n 12.0.0)
18+
19+
:-1: Examples of **incorrect** code for this rule:
20+
21+
<eslint-code-block>
22+
23+
<!-- eslint-skip -->
24+
25+
```vue
26+
<script>
27+
/* eslint @intlify/vue-i18n/no-deprecated-v-t: 'error' */
28+
</script>
29+
<template>
30+
<!-- ✗ BAD -->
31+
<p v-t="'banana'"></p>
32+
</template>
33+
```
34+
35+
</eslint-code-block>
36+
37+
:+1: Examples of **correct** code for this rule:
38+
39+
<eslint-code-block>
40+
41+
<!-- eslint-skip -->
42+
43+
```vue
44+
<script>
45+
/* eslint @intlify/vue-i18n/no-deprecated-v-t: 'error' */
46+
</script>
47+
<template>
48+
<!-- ✓ GOOD -->
49+
<p>{{ $t('banana') }}</p>
50+
</template>
51+
```
52+
53+
</eslint-code-block>
54+
55+
## :books: Further reading
56+
57+
- [Vue I18n > Breaking Changes in v11 - Deprecate Custom Directive `v-t`](https://vue-i18n.intlify.dev/guide/migration/breaking11.html#deprecate-custom-directive-v-t)
58+
59+
## :rocket: Version
60+
61+
This rule was introduced in `@intlify/eslint-plugin-vue-i18n` v3.0.0
62+
63+
## :mag: Implementation
64+
65+
- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/no-deprecated-v-t.ts)
66+
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/no-deprecated-v-t.ts)

‎lib/configs/flat/recommended.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export = [
2424
'@intlify/vue-i18n/no-deprecated-i18n-places-prop': 'warn',
2525
'@intlify/vue-i18n/no-deprecated-modulo-syntax': 'warn',
2626
'@intlify/vue-i18n/no-deprecated-tc': 'warn',
27+
'@intlify/vue-i18n/no-deprecated-v-t': 'warn',
2728
'@intlify/vue-i18n/no-html-messages': 'warn',
2829
'@intlify/vue-i18n/no-i18n-t-path-prop': 'warn',
2930
'@intlify/vue-i18n/no-missing-keys': 'warn',

‎lib/configs/recommended.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export = {
1818
'@intlify/vue-i18n/no-deprecated-i18n-places-prop': 'warn',
1919
'@intlify/vue-i18n/no-deprecated-modulo-syntax': 'warn',
2020
'@intlify/vue-i18n/no-deprecated-tc': 'warn',
21+
'@intlify/vue-i18n/no-deprecated-v-t': 'warn',
2122
'@intlify/vue-i18n/no-html-messages': 'warn',
2223
'@intlify/vue-i18n/no-i18n-t-path-prop': 'warn',
2324
'@intlify/vue-i18n/no-missing-keys': 'warn',

‎lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import noDeprecatedI18nPlaceAttr from './rules/no-deprecated-i18n-place-attr'
1212
import noDeprecatedI18nPlacesProp from './rules/no-deprecated-i18n-places-prop'
1313
import noDeprecatedModuloSyntax from './rules/no-deprecated-modulo-syntax'
1414
import noDeprecatedTc from './rules/no-deprecated-tc'
15+
import noDeprecatedVT from './rules/no-deprecated-v-t'
1516
import noDuplicateKeysInLocale from './rules/no-duplicate-keys-in-locale'
1617
import noDynamicKeys from './rules/no-dynamic-keys'
1718
import noHtmlMessages from './rules/no-html-messages'
@@ -45,6 +46,7 @@ export = {
4546
'no-deprecated-i18n-places-prop': noDeprecatedI18nPlacesProp,
4647
'no-deprecated-modulo-syntax': noDeprecatedModuloSyntax,
4748
'no-deprecated-tc': noDeprecatedTc,
49+
'no-deprecated-v-t': noDeprecatedVT,
4850
'no-duplicate-keys-in-locale': noDuplicateKeysInLocale,
4951
'no-dynamic-keys': noDynamicKeys,
5052
'no-html-messages': noHtmlMessages,

‎lib/rules/no-deprecated-v-t.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @author kazuya kawaguchi (a.k.a. kazupon)
3+
*/
4+
import { defineTemplateBodyVisitor } from '../utils/index'
5+
import { createRule } from '../utils/rule'
6+
7+
import type { RuleContext, RuleListener } from '../types'
8+
import type { AST as VAST } from 'vue-eslint-parser'
9+
10+
function checkDirective(context: RuleContext, node: VAST.VDirective) {
11+
context.report({
12+
node,
13+
message: `'v-t' custom directive is used, but it is deprecated. Use 't' or '$t' instead.`
14+
})
15+
}
16+
17+
function create(context: RuleContext): RuleListener {
18+
return defineTemplateBodyVisitor(context, {
19+
"VAttribute[directive=true][key.name='t']"(node: VAST.VDirective) {
20+
checkDirective(context, node)
21+
},
22+
23+
"VAttribute[directive=true][key.name.name='t']"(node: VAST.VDirective) {
24+
checkDirective(context, node)
25+
}
26+
})
27+
}
28+
29+
export = createRule({
30+
meta: {
31+
type: 'problem',
32+
docs: {
33+
description:
34+
'disallow using deprecated `v-t` custom directive (Deprecated in Vue I18n 11.0.0, removed fully in Vue I18n 12.0.0)',
35+
category: 'Recommended',
36+
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/no-deprecated-v-t.html',
37+
recommended: false
38+
},
39+
fixable: null,
40+
schema: []
41+
},
42+
create
43+
})

‎tests/lib/rules/no-deprecated-v-t.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author kazuya kawaguchi (a.k.a. kazupon)
3+
*/
4+
import { RuleTester } from '../eslint-compat'
5+
import rule from '../../../lib/rules/no-deprecated-v-t'
6+
import * as vueParser from 'vue-eslint-parser'
7+
8+
const tester = new RuleTester({
9+
languageOptions: {
10+
parser: vueParser,
11+
ecmaVersion: 2020,
12+
sourceType: 'module'
13+
}
14+
})
15+
16+
tester.run('no-deprecated-v-t', rule as never, {
17+
valid: [
18+
{
19+
code: `<template><p v-html="$t('hello')"></p></template>`
20+
}
21+
],
22+
invalid: [
23+
{
24+
code: `<template><p v-t="'banana'"></p></template>`,
25+
errors: [
26+
`'v-t' custom directive is used, but it is deprecated. Use 't' or '$t' instead.`
27+
]
28+
}
29+
]
30+
})

0 commit comments

Comments
 (0)
Please sign in to comment.