-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @intlify/vue-i18n/no-i18n-t-path-prop rule (#167)
- Loading branch information
Showing
5 changed files
with
215 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
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,68 @@ | ||
--- | ||
title: '@intlify/vue-i18n/no-i18n-t-path-prop' | ||
description: disallow using `path` prop with `<i18n-t>` | ||
--- | ||
|
||
# @intlify/vue-i18n/no-i18n-t-path-prop | ||
|
||
> disallow using `path` prop with `<i18n-t>` | ||
- :black_nib:️ The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule. | ||
|
||
You cannot use `path` prop with `<i18n-t>` component. Perhaps it's an old habit mistake. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports use of `path` prop with `<i18n-t>` component. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
<eslint-code-block fix> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<script> | ||
/* eslint @intlify/vue-i18n/no-i18n-t-path-prop: 'error' */ | ||
</script> | ||
<template> | ||
<div class="app"> | ||
<!-- ✗ BAD --> | ||
<i18n-t path="message.greeting" /> | ||
</div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
<eslint-code-block fix> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<script> | ||
/* eslint @intlify/vue-i18n/no-i18n-t-path-prop: 'error' */ | ||
</script> | ||
<template> | ||
<div class="app"> | ||
<!-- ✓ GOOD --> | ||
<i18n-t keypath="message.greeting" /> | ||
<!-- ✓ GOOD --> | ||
<i18n path="message.greeting" /> | ||
</div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [Vue I18n > Breaking Changes - Rename to `keypath` prop from `path` prop](https://vue-i18n.intlify.dev/guide/migration/breaking.html#rename-to-keypath-prop-from-path-prop) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/no-i18n-t-path-prop.ts) | ||
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/no-i18n-t-path-prop.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,53 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
import { | ||
defineTemplateBodyVisitor, | ||
getAttribute, | ||
getDirective | ||
} from '../utils/index' | ||
import type { RuleContext, RuleListener } from '../types' | ||
import type { AST as VAST } from 'vue-eslint-parser' | ||
|
||
function create(context: RuleContext): RuleListener { | ||
return defineTemplateBodyVisitor(context, { | ||
VElement(node: VAST.VElement) { | ||
if (node.name !== 'i18n-t') { | ||
return | ||
} | ||
const pathProp = | ||
getAttribute(node, 'path') || getDirective(node, 'bind', 'path') | ||
if (pathProp) { | ||
context.report({ | ||
node: pathProp.key, | ||
messageId: 'disallow', | ||
fix(fixer) { | ||
if (pathProp.directive) { | ||
return fixer.replaceText(pathProp.key.argument!, 'keypath') | ||
} else { | ||
return fixer.replaceText(pathProp.key, 'keypath') | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
export = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow using `path` prop with `<i18n-t>`', | ||
category: 'Recommended', | ||
recommended: false | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
disallow: | ||
'Cannot use `path` prop with `<i18n-t>` component. Use `keypath` prop instead.' | ||
} | ||
}, | ||
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,91 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
import { RuleTester } from 'eslint' | ||
import rule = require('../../../lib/rules/no-i18n-t-path-prop') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-i18n-t-path-prop', rule as never, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<i18n-t keypath="message.greeting" /> | ||
</template> | ||
` | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<i18n path="message.greeting" /> | ||
</template> | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<i18n-t path="message.greeting" /> | ||
</template> | ||
`, | ||
output: ` | ||
<template> | ||
<i18n-t keypath="message.greeting" /> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Cannot use `path` prop with `<i18n-t>` component. Use `keypath` prop instead.', | ||
line: 3, | ||
column: 17 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<i18n-t :path="messageKey" /> | ||
</template> | ||
`, | ||
output: ` | ||
<template> | ||
<i18n-t :keypath="messageKey" /> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Cannot use `path` prop with `<i18n-t>` component. Use `keypath` prop instead.', | ||
line: 3, | ||
column: 17 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<i18n-t v-bind:path="messageKey" /> | ||
</template> | ||
`, | ||
output: ` | ||
<template> | ||
<i18n-t v-bind:keypath="messageKey" /> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Cannot use `path` prop with `<i18n-t>` component. Use `keypath` prop instead.', | ||
line: 3, | ||
column: 17 | ||
} | ||
] | ||
} | ||
] | ||
}) |