Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives of key used in <i18n> component in no-unused-keys rule. #92

Merged
merged 2 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
],
"eslint.alwaysShowStatus": true,
"eslint.packageManager": "yarn",
"eslint.run": "onSave"
"eslint.run": "onSave",
"npm.packageManager": "yarn"
}
39 changes: 34 additions & 5 deletions lib/utils/collect-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const debug = require('debug')('eslint-plugin-vue-i18n:collect-keys')
* @typedef {import('vue-eslint-parser').AST.Node} Node
* @typedef {import('vue-eslint-parser').AST.ESLintCallExpression} CallExpression
* @typedef {import('vue-eslint-parser').AST.VDirective} VDirective
* @typedef {import('vue-eslint-parser').AST.VAttribute} VAttribute
*/

/**
Expand Down Expand Up @@ -51,6 +52,17 @@ function getKeyFromVDirective (node) {
}
}

/**
* @param {VAttribute} node
*/
function getKeyFromI18nComponent (node) {
if (node.value && node.value.type === 'VLiteral') {
return node.value.value
} else {
return null
}
}

function getParser (parser) {
if (parser) {
try {
Expand Down Expand Up @@ -132,11 +144,28 @@ function collectKeysFromAST (node, visitorKeys) {
*/
function enterNode (node) {
if (node.type === 'VAttribute') {
if (node.directive && (node.key.name.name === 't' || node.key.name === 't' /* parser v5 */)) {
debug("call VAttribute[directive=true][key.name.name='t'] handling ...")
const key = getKeyFromVDirective(node)
if (key) {
results.add(key)
if (node.directive) {
if (node.key.name.name === 't' || node.key.name === 't' /* parser v5 */) {
debug("call VAttribute[directive=true][key.name.name='t'] handling ...")
const key = getKeyFromVDirective(node)
if (key) {
results.add(key)
}
}
} else {
if (
node.key.name === 'path' &&
(
node.parent.parent.name === 'i18n' ||
node.parent.parent.name === 'i18n-t'
)
) {
debug("call VElement:matches([name=i18n], [name=i18n-t]) > VStartTag > VAttribute[key.name='path'] handling ...")

const key = getKeyFromI18nComponent(node)
if (key) {
results.add(key)
}
}
}
} else if (node.type === 'CallExpression') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"link": "@:messages.hello"
},
"hello_dio": "hello underscore DIO!",
"hello {name}": "hello {name}!"
"hello {name}": "hello {name}!",
"term": "I accept xxx {0}."
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"link": "@:messages.hello"
},
"hello_dio": "こんにちは、アンダースコア DIO!",
"hello {name}": "こんにちは、{name}!"
"hello {name}": "こんにちは、{name}!",
"term": "私は xxx の{0}に同意します。"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<template>
<div id="app">
<p v-t="'hello_dio'">{{ $t('messages.link') }}</p>
<i18n path="term" tag="label" for="tos">
<a :href="url" target="_blank">{{ $t('tos') }}</a>
</i18n>
</div>
</template>

Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/no-unused-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ new RuleTester({
}
}
</script>`
}, {
// <i18n> component
filename: 'test.vue',
code: `
<template>
<i18n path="message_key" tag="p" />
</template>
<i18n>
{
"en": {
"message_key": "hi"
}
}
</i18n>
`
}, {
// <i18n-t> component
filename: 'test.vue',
code: `
<template>
<i18n-t path="message_key" tag="p" />
</template>
<i18n>
{
"en": {
"message_key": "hi"
}
}
</i18n>
`
}],
invalid: [{
// sfc supports
Expand Down