Skip to content

Commit 905c9ed

Browse files
committed
Fix bug when using delimited-like path.
1 parent 6246120 commit 905c9ed

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

lib/rules/no-unused-keys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function getUsedKeysMap(
5555
const usedKeysMap: UsedKeys = {}
5656

5757
for (const key of [...usedkeys, ...collectLinkedKeys(values, context)]) {
58+
usedKeysMap[key] = {} // Set original key.
5859
const paths = parsePath(key)
5960
let map = usedKeysMap
6061
while (paths.length) {

lib/utils/locale-messages.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,31 +253,39 @@ export class LocaleMessages {
253253
findMissingPath(key: string): string | null {
254254
let missingPath: string[] = []
255255
for (const locale of this.locales) {
256-
const paths = parsePath(key)
257-
const length = paths.length
258-
let lasts: I18nLocaleMessageValue[] = this.localeMessages.map(lm =>
259-
lm.getMessagesFromLocale(locale)
256+
const localeMessages: I18nLocaleMessageValue[] = this.localeMessages.map(
257+
lm => lm.getMessagesFromLocale(locale)
260258
)
261-
let i = 0
262-
let missing = false
263-
while (i < length) {
259+
if (
260+
localeMessages.some(last => {
261+
return last && typeof last !== 'string' ? last[key] != null : false
262+
})
263+
) {
264+
// Hit the original key.
265+
return null
266+
}
267+
268+
const paths = [...parsePath(key)]
269+
let lasts = localeMessages
270+
const targetPaths = []
271+
while (paths.length) {
272+
const path = paths.shift()!
273+
targetPaths.push(path)
264274
const values: I18nLocaleMessageValue[] = lasts
265275
.map(last => {
266-
return last && typeof last !== 'string' ? last[paths[i]] : undefined
276+
return last && typeof last !== 'string' ? last[path] : undefined
267277
})
268278
.filter((val): val is I18nLocaleMessageValue => val != null)
269279

270280
if (values.length === 0) {
271-
if (missingPath.length <= i) {
272-
missingPath = paths.slice(0, i + 1)
281+
if (missingPath.length <= targetPaths.length) {
282+
missingPath = targetPaths
273283
}
274-
missing = true
275284
break
276285
}
277286
lasts = values
278-
i++
279287
}
280-
if (!missing) {
288+
if (!missingPath.length) {
281289
return null
282290
}
283291
}

tests/lib/rules/no-missing-keys.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ tester.run('no-missing-keys', rule as never, {
161161
<template>
162162
<div id="app"></div>
163163
</template>`
164+
},
165+
{
166+
filename: 'test.vue',
167+
code: `
168+
<i18n locale="en">
169+
{
170+
"Usage: $0 <command> [options]": "Usage: $0 <command> [options]"
171+
}
172+
</i18n>
173+
<script>
174+
t('Usage: $0 <command> [options]')
175+
</script>`
176+
},
177+
{
178+
filename: 'test.vue',
179+
code: `
180+
<i18n locale="en">
181+
{
182+
"foo.bar": "Message"
183+
}
184+
</i18n>
185+
<script>
186+
t('foo.bar')
187+
</script>`
164188
}
165189
]
166190
),
@@ -209,6 +233,13 @@ tester.run('no-missing-keys', rule as never, {
209233
code: `$t('missing.path')`,
210234
errors: [`'missing' does not exist in localization message resources`]
211235
},
236+
{
237+
// nested missing
238+
code: `$t('messages.missing')`,
239+
errors: [
240+
`'messages.missing' does not exist in localization message resources`
241+
]
242+
},
212243
{
213244
// nested missing
214245
code: `$t('messages.missing')`,

tests/lib/rules/no-unused-keys.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,31 @@ new RuleTester({
148148
]
149149
})
150150
]
151-
: [])
151+
: []),
152+
{
153+
filename: 'test.vue',
154+
code: `
155+
<i18n locale="en">
156+
{
157+
"Usage: $0 <command> [options]": "Usage: $0 <command> [options]"
158+
}
159+
</i18n>
160+
<script>
161+
t('Usage: $0 <command> [options]')
162+
</script>`
163+
},
164+
{
165+
filename: 'test.vue',
166+
code: `
167+
<i18n locale="en">
168+
{
169+
"foo.bar": "Message"
170+
}
171+
</i18n>
172+
<script>
173+
t('foo.bar')
174+
</script>`
175+
}
152176
],
153177
invalid: [
154178
{

0 commit comments

Comments
 (0)