Skip to content
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

Handle toString; add tests for other builtins #1707

Draft
wants to merge 1 commit into
base: v8.x
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ export default class VueI18n {
if (!message || !key) { return false }
if (!isNull(this._path.getPathValue(message, key))) { return true }
// fallback for flat key
if (message[key]) { return true }
return false
return Object.prototype.hasOwnProperty.call(message, key)
}

if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
Expand Down
3 changes: 3 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ export default class I18nPath {
let last: any = obj
let i: number = 0
while (i < length) {
if (!Object.prototype.hasOwnProperty.call(last, paths[i])) {
return null
}
const value: any = last[paths[i]]
if (value === undefined || value === null) {
return null
Expand Down
83 changes: 83 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,89 @@ describe('basic', () => {
assert(vm.$te('message.hello', 'xx') === false)
})
})

describe('builtin property handling', () => {
[
// '__proto__', // TODO: see https://github.com/kazupon/vue-i18n/issues/1706
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'toLocaleString',
'toString',
'valueOf'
].forEach(k => {
describe('top-level props', () => {
describe('existing key', () => {
let vm;
beforeEach(() => {
const i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
messages: {
en: {
[k]: 'i exist'
},
ja: {
[k]: 'i exist (ja)'
},
},
modifiers: {
custom: str => str.replace(/[aeiou]/g, 'x')
}
})
vm = new Vue({ i18n })
})

it('should return true', () => {
assert(vm.$te(k) === true)
})

it('should return true with locale', () => {
assert(vm.$te(k, 'ja') === true)
})
})

describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue({ i18n })
assert(vm.$te(k) === false)
})

it('should return false with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(k, 'ja') === false)
})
})
})

describe('deep props', () => {
describe('existing key', () => {
it('should return true', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.existing.${k}`) === true)
})

it('should return true with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.existing.${k}`, 'ja') === true)
})
})

describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.missing.${k}`) === false)
})

it('should return false with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.missing.${k}`, 'ja') === false)
})
})
})
})
})
})

describe('i18n#locale', () => {
Expand Down
32 changes: 29 additions & 3 deletions test/unit/fixture/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
en: {
message: {
hello: 'the world',
Expand Down Expand Up @@ -70,7 +70,20 @@ export default {
]
],
issues: {
arrayBugs: ['bug1', 'bug2']
arrayBugs: ['bug1', 'bug2'],
builtins: {
existing: {
// '__proto__': 'i exist', // TODO
'constructor': 'i exist',
'hasOwnProperty': 'i exist',
'isPrototypeOf': 'i exist',
'propertyIsEnumerable': 'i exist',
'toLocaleString': 'i exist',
'toString': 'i exist',
'valueOf': 'i exist'
},
missing: {}
}
},
'foo.bar.buz': 'hello flat key!'
},
Expand Down Expand Up @@ -111,7 +124,20 @@ export default {
]
],
issues: {
arrayBugs: ['バグ1', 'バグ2']
arrayBugs: ['バグ1', 'バグ2'],
builtins: {
existing: {
// '__proto__': 'i exist (ja)', // TODO
'constructor': 'i exist (ja)',
'hasOwnProperty': 'i exist (ja)',
'isPrototypeOf': 'i exist (ja)',
'propertyIsEnumerable': 'i exist (ja)',
'toLocaleString': 'i exist (ja)',
'toString': 'i exist (ja)',
'valueOf': 'i exist (ja)'
},
missing: {}
}
},
'foo.bar.buz': 'こんにちは、フラットなキーさん!'
}
Expand Down