Skip to content

Fix bug when using delimited-like path. #256

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

Merged
merged 2 commits into from
Nov 16, 2021
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
1 change: 1 addition & 0 deletions lib/rules/no-unused-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function getUsedKeysMap(
const usedKeysMap: UsedKeys = {}

for (const key of [...usedkeys, ...collectLinkedKeys(values, context)]) {
usedKeysMap[key] = {} // Set original key.
const paths = parsePath(key)
let map = usedKeysMap
while (paths.length) {
Expand Down
34 changes: 21 additions & 13 deletions lib/utils/locale-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,39 @@ export class LocaleMessages {
findMissingPath(key: string): string | null {
let missingPath: string[] = []
for (const locale of this.locales) {
const paths = parsePath(key)
const length = paths.length
let lasts: I18nLocaleMessageValue[] = this.localeMessages.map(lm =>
lm.getMessagesFromLocale(locale)
const localeMessages: I18nLocaleMessageValue[] = this.localeMessages.map(
lm => lm.getMessagesFromLocale(locale)
)
let i = 0
let missing = false
while (i < length) {
if (
localeMessages.some(last => {
return last && typeof last !== 'string' ? last[key] != null : false
})
) {
// Hit the original key.
return null
}

const paths = [...parsePath(key)]
let lasts = localeMessages
const targetPaths = []
while (paths.length) {
const path = paths.shift()!
targetPaths.push(path)
const values: I18nLocaleMessageValue[] = lasts
.map(last => {
return last && typeof last !== 'string' ? last[paths[i]] : undefined
return last && typeof last !== 'string' ? last[path] : undefined
})
.filter((val): val is I18nLocaleMessageValue => val != null)

if (values.length === 0) {
if (missingPath.length <= i) {
missingPath = paths.slice(0, i + 1)
if (missingPath.length <= targetPaths.length) {
missingPath = targetPaths
}
missing = true
break
}
lasts = values
i++
}
if (!missing) {
if (!missingPath.length) {
return null
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/no-missing-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ tester.run('no-missing-keys', rule as never, {
<template>
<i18n-t keypath="'hello'"></i18n-t>
</template>`
},
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"Usage: $0 <command> [options]": "Usage: $0 <command> [options]"
}
</i18n>
<script>
t('Usage: $0 <command> [options]')
</script>`
},
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"foo.bar": "Message"
}
</i18n>
<script>
t('foo.bar')
</script>`
}
]
),
Expand Down Expand Up @@ -217,6 +241,13 @@ tester.run('no-missing-keys', rule as never, {
code: `$t('missing.path')`,
errors: [`'missing' does not exist in localization message resources`]
},
{
// nested missing
code: `$t('messages.missing')`,
errors: [
`'messages.missing' does not exist in localization message resources`
]
},
{
// nested missing
code: `$t('messages.missing')`,
Expand Down
45 changes: 44 additions & 1 deletion tests/lib/rules/no-unused-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,50 @@ new RuleTester({
]
})
]
: [])
: []),
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"Usage: $0 <command> [options]": "Usage: $0 <command> [options]"
}
</i18n>
<script>
t('Usage: $0 <command> [options]')
</script>`
},
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"foo.bar": "Message"
}
</i18n>
<script>
t('foo.bar')
</script>`
},
{
// https://github.com/intlify/eslint-plugin-vue-i18n/issues/260
filename: 'test.vue',
code: `
<i18n locale="en">
{
"hello {name}.": "hello {name}!"
}
</i18n>
<script>
export default {
methods: {
fn () {
this.$i18n.t('hello {name}.', { name: 'DIO' })
}
}
}
</script>`
}
],
invalid: [
{
Expand Down