Skip to content

Commit

Permalink
fix: no-missing-keys rule reports false positive with trailing dot
Browse files Browse the repository at this point in the history
For a translation key like $t('missing.') the parse function from core utilities returns undefined.

Previously this was turned into an empty array which caused all of those paths to be false positives.

If the given key can't be parsed as "path" properly, the correct handling is to treat it as a single item path with the provided string as the only item.

This also revealed another faulty test, where keypath="'hello'" was passing even though it should not.
  • Loading branch information
wolfgangwalther committed Jul 4, 2023
1 parent 95f6dd3 commit c34b663
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/utils/key-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export function joinPath(...paths: (string | number)[]): string {
}

export function parsePath(path: string): string[] {
return parse(path) || []
return parse(path) || [path]
}
9 changes: 8 additions & 1 deletion tests/lib/rules/no-missing-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ tester.run('no-missing-keys', rule as never, {
code: `
<i18n locale="en">{"hello": "hello"}</i18n>
<template>
<i18n-t keypath="'hello'"></i18n-t>
<i18n-t keypath="hello"></i18n-t>
</template>`
},
{
Expand Down Expand Up @@ -271,6 +271,13 @@ tester.run('no-missing-keys', rule as never, {
</template>`,
errors: [`'missing' does not exist in localization message resources`]
},
{
// missing ending with a dot
code: `$t('missing.')`,
errors: [
`'["missing."]' does not exist in localization message resources`
]
},
{
// nested basic
code: `$t('missing.path')`,
Expand Down

0 comments on commit c34b663

Please sign in to comment.