-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
no-deprecated-tc
rule (#510)
* feat: add `no-deprecated-tc` rule * Create new-apples-ring.md
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"@intlify/eslint-plugin-vue-i18n": patch | ||
--- | ||
|
||
feat: add `no-deprecated-tc` rule |
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,64 @@ | ||
--- | ||
title: '@intlify/vue-i18n/no-deprecated-tc' | ||
description: disallow using deprecated `tc` or `$tc` (Deprecated in Vue I18n 10.0.0, removed fully in Vue I18n 11.0.0) | ||
since: v3.0.0 | ||
--- | ||
|
||
# @intlify/vue-i18n/no-deprecated-tc | ||
|
||
> disallow using deprecated `tc` or `$tc` (Deprecated in Vue I18n 10.0.0, removed fully in Vue I18n 11.0.0) | ||
If you are migrating from Vue I18n v9 to v10, `tc` or `$tc` should be replaced with `t` or `$t`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports use of deprecated `tc` or `$tc` (Deprecated in Vue I18n 10.0.0, removed fully in Vue I18n 11.0.0) | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
<eslint-code-block> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<script> | ||
/* eslint @intlify/vue-i18n/no-deprecated-tc: 'error' */ | ||
</script> | ||
<template> | ||
<!-- ✗ BAD --> | ||
<p>{{ $tc('banana') }}</p> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
<eslint-code-block> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<script> | ||
/* eslint @intlify/vue-i18n/no-deprecated-tc: 'error' */ | ||
</script> | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<p>{{ $t('banana', 1) }}</p> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [Vue I18n > Breaking Changes in v10 - Deprecate tc and $tc for Legacy API mode](https://vue-i18n.intlify.dev/guide/migration/breaking10.html#deprecate-tc-and-tc-for-legacy-api-mode) | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in `@intlify/eslint-plugin-vue-i18n` v3.0.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/no-deprecated-tc.ts) | ||
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/no-deprecated-tc.ts) |
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,60 @@ | ||
/** | ||
* @author kazuya kawaguchi (a.k.a. kazupon) | ||
*/ | ||
import { defineTemplateBodyVisitor } from '../utils/index' | ||
import { createRule } from '../utils/rule' | ||
|
||
import type { RuleContext, RuleListener } from '../types' | ||
import type { AST as VAST } from 'vue-eslint-parser' | ||
|
||
function checkCallExpression( | ||
context: RuleContext, | ||
node: VAST.ESLintCallExpression | ||
) { | ||
const funcName = | ||
(node.callee.type === 'MemberExpression' && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name) || | ||
(node.callee.type === 'Identifier' && node.callee.name) || | ||
'' | ||
|
||
if (/^(\$tc|tc)$/.test(funcName)) { | ||
context.report({ | ||
node, | ||
message: `'${funcName}' is used, but it is deprecated. Use 't' or '$t' instead.` | ||
}) | ||
return | ||
} | ||
} | ||
|
||
function create(context: RuleContext): RuleListener { | ||
return defineTemplateBodyVisitor( | ||
context, | ||
{ | ||
CallExpression(node: VAST.ESLintCallExpression) { | ||
checkCallExpression(context, node) | ||
} | ||
}, | ||
{ | ||
CallExpression(node: VAST.ESLintCallExpression) { | ||
checkCallExpression(context, node) | ||
} | ||
} | ||
) | ||
} | ||
|
||
export = createRule({ | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'disallow using deprecated `tc` or `$tc` (Deprecated in Vue I18n 10.0.0, removed fully in Vue I18n 11.0.0)', | ||
category: 'Best Practices', | ||
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/no-deprecated-tc.html', | ||
recommended: false | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
create | ||
}) |
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,38 @@ | ||
/** | ||
* @author kazuya kawaguchi (a.k.a. kazupon) | ||
*/ | ||
import { RuleTester } from '../eslint-compat' | ||
import rule from '../../../lib/rules/no-deprecated-tc' | ||
import * as vueParser from 'vue-eslint-parser' | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { parser: vueParser, ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-deprecated-tc', rule as never, { | ||
valid: [], | ||
invalid: [ | ||
{ | ||
// tc key | ||
code: `tc('banana')`, | ||
errors: [`'tc' is used, but it is deprecated. Use 't' or '$t' instead.`] | ||
}, | ||
{ | ||
// $tc key | ||
code: `$tc('banana')`, | ||
errors: [`'$tc' is used, but it is deprecated. Use 't' or '$t' instead.`] | ||
}, | ||
{ | ||
// via i18n instance | ||
code: `i18n.tc('banana')`, | ||
errors: [`'tc' is used, but it is deprecated. Use 't' or '$t' instead.`] | ||
}, | ||
{ | ||
// using mustaches in template block | ||
code: `<template> | ||
<p>{{ $tc('banana') }}</p> | ||
</template>`, | ||
errors: [`'$tc' is used, but it is deprecated. Use 't' or '$t' instead.`] | ||
} | ||
] | ||
}) |