Skip to content

Fix crash with null value in no-raw-text rule #315

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
Apr 7, 2022
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
19 changes: 10 additions & 9 deletions lib/rules/no-duplicate-keys-in-locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,21 @@ function create(context: RuleContext): RuleListener {
}
return
}
const keyOtherValues = pathStack.otherDictionaries
.filter(dict => dict.dict[key] != null)
.map(dict => {
return {
value: dict.dict[key],
source: dict.source
}
})
const keyOtherValues = pathStack.otherDictionaries.map(dict => {
return {
value: dict.dict[key],
source: dict.source
}
})
const keyPath = [...pathStack.keyPath, key]
const keyPathStr = joinPath(...keyPath)
const nextOtherDictionaries: DictData[] = []
const reportFiles = []
for (const value of keyOtherValues) {
if (typeof value.value === 'string') {
if (value.value == null) {
continue
}
if (typeof value.value !== 'object') {
reportFiles.push(
'"' + getMessageFilepath(value.source.fullpath, context) + '"'
)
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-raw-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,10 @@ function extractMessageKeys(
): Iterable<string> {
for (const key of Object.keys(messages)) {
const value = messages[key]
if (typeof value === 'string') {
if (value == null) {
continue
}
if (typeof value !== 'object') {
if (targetValue === value) {
yield [...paths, key].join('.')
}
Expand Down
10 changes: 9 additions & 1 deletion lib/types/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export type I18nLocaleMessageValue = string | I18nLocaleMessageDictionary
export type I18nLocaleMessageValue =
| string
| null
| undefined
| number
| boolean
| symbol
| bigint
| I18nLocaleMessageDictionary

export type I18nLocaleMessageDictionary = {
[property: string]: I18nLocaleMessageValue
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/locale-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class LocaleMessages {
)
if (
localeMessages.some(last => {
return last && typeof last !== 'string' ? last[key] != null : false
return last && typeof last === 'object' ? last[key] != null : false
})
) {
// Hit the original key.
Expand All @@ -315,7 +315,7 @@ export class LocaleMessages {
targetPaths.push(path)
const values: I18nLocaleMessageValue[] = lasts
.map(last => {
return last && typeof last !== 'string' ? last[path] : undefined
return last && typeof last === 'object' ? last[path] : undefined
})
.filter((val): val is I18nLocaleMessageValue => val != null)

Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-duplicate-keys-in-locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ new RuleTester({
<template></template>
<script></script>`
},
{
filename: 'test.vue',
code: `
<i18n locale="en">{ "foo": null, "bar": 123 }</i18n>
<template>Hello!</template>`
},
...getTestCasesFromFixtures({
cwd: join(cwdRoot, './valid/vue-cli-format'),
localeDir: `./locales/*.{json,yaml,yml}`
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/no-raw-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,27 @@ tester.run('no-raw-text', rule as never, {
]
}
]
},
{
// null value
code: `
<i18n locale="en">{ "foo": null, "bar": 123 }</i18n>
<template>Hello!</template>`,
errors: [
{
message: "raw text 'Hello!' is used",
suggestions: [
{
desc: "Add the resource to the '<i18n>' block.",
output: `
<i18n locale="en">{
"Hello!": "Hello!",
"foo": null, "bar": 123 }</i18n>
<template>{{$t('Hello!')}}</template>`
}
]
}
]
}
]
})