Skip to content

Commit dd1db9e

Browse files
authored
Fix crash with null value in no-raw-text rule (#315)
* Fix crash with null value in no-raw-text rule * fix
1 parent 84d59fb commit dd1db9e

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

lib/rules/no-duplicate-keys-in-locale.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,21 @@ function create(context: RuleContext): RuleListener {
113113
}
114114
return
115115
}
116-
const keyOtherValues = pathStack.otherDictionaries
117-
.filter(dict => dict.dict[key] != null)
118-
.map(dict => {
119-
return {
120-
value: dict.dict[key],
121-
source: dict.source
122-
}
123-
})
116+
const keyOtherValues = pathStack.otherDictionaries.map(dict => {
117+
return {
118+
value: dict.dict[key],
119+
source: dict.source
120+
}
121+
})
124122
const keyPath = [...pathStack.keyPath, key]
125123
const keyPathStr = joinPath(...keyPath)
126124
const nextOtherDictionaries: DictData[] = []
127125
const reportFiles = []
128126
for (const value of keyOtherValues) {
129-
if (typeof value.value === 'string') {
127+
if (value.value == null) {
128+
continue
129+
}
130+
if (typeof value.value !== 'object') {
130131
reportFiles.push(
131132
'"' + getMessageFilepath(value.source.fullpath, context) + '"'
132133
)

lib/rules/no-raw-text.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,10 @@ function extractMessageKeys(
662662
): Iterable<string> {
663663
for (const key of Object.keys(messages)) {
664664
const value = messages[key]
665-
if (typeof value === 'string') {
665+
if (value == null) {
666+
continue
667+
}
668+
if (typeof value !== 'object') {
666669
if (targetValue === value) {
667670
yield [...paths, key].join('.')
668671
}

lib/types/i18n.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
export type I18nLocaleMessageValue = string | I18nLocaleMessageDictionary
1+
export type I18nLocaleMessageValue =
2+
| string
3+
| null
4+
| undefined
5+
| number
6+
| boolean
7+
| symbol
8+
| bigint
9+
| I18nLocaleMessageDictionary
210

311
export type I18nLocaleMessageDictionary = {
412
[property: string]: I18nLocaleMessageValue

lib/utils/locale-messages.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class LocaleMessages {
300300
)
301301
if (
302302
localeMessages.some(last => {
303-
return last && typeof last !== 'string' ? last[key] != null : false
303+
return last && typeof last === 'object' ? last[key] != null : false
304304
})
305305
) {
306306
// Hit the original key.
@@ -316,7 +316,7 @@ export class LocaleMessages {
316316
targetPaths.push(path)
317317
const values: I18nLocaleMessageValue[] = lasts
318318
.map(last => {
319-
return last && typeof last !== 'string' ? last[path] : undefined
319+
return last && typeof last === 'object' ? last[path] : undefined
320320
})
321321
.filter((val): val is I18nLocaleMessageValue => val != null)
322322

tests/lib/rules/no-duplicate-keys-in-locale.ts

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ new RuleTester({
7676
<template></template>
7777
<script></script>`
7878
},
79+
{
80+
filename: 'test.vue',
81+
code: `
82+
<i18n locale="en">{ "foo": null, "bar": 123 }</i18n>
83+
<template>Hello!</template>`
84+
},
7985
...getTestCasesFromFixtures({
8086
cwd: join(cwdRoot, './valid/vue-cli-format'),
8187
localeDir: `./locales/*.{json,yaml,yml}`

tests/lib/rules/no-raw-text.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,27 @@ tester.run('no-raw-text', rule as never, {
15971597
]
15981598
}
15991599
]
1600+
},
1601+
{
1602+
// null value
1603+
code: `
1604+
<i18n locale="en">{ "foo": null, "bar": 123 }</i18n>
1605+
<template>Hello!</template>`,
1606+
errors: [
1607+
{
1608+
message: "raw text 'Hello!' is used",
1609+
suggestions: [
1610+
{
1611+
desc: "Add the resource to the '<i18n>' block.",
1612+
output: `
1613+
<i18n locale="en">{
1614+
"Hello!": "Hello!",
1615+
"foo": null, "bar": 123 }</i18n>
1616+
<template>{{$t('Hello!')}}</template>`
1617+
}
1618+
]
1619+
}
1620+
]
16001621
}
16011622
]
16021623
})

0 commit comments

Comments
 (0)