Skip to content
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
8 changes: 6 additions & 2 deletions packages/core-base/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction, isObject } from '@intlify/shared'
import { hasOwn, isFunction, isObject } from '@intlify/shared'
import { AST_NODE_PROPS_KEYS, isMessageAST } from './ast'

/** @VueI18nGeneral */
Expand Down Expand Up @@ -335,7 +335,8 @@ export function resolveValue(obj: unknown, path: Path): PathValue {

// resolve path value
const len = hit.length
let last = obj

let last: any = obj
let i = 0
while (i < len) {
const key = hit[i]
Expand All @@ -350,6 +351,9 @@ export function resolveValue(obj: unknown, path: Path): PathValue {
if (!isObject(last)) {
return null
}
if (!hasOwn(last as object, key)) {
return null
}
const val = last[key]
if (val === undefined) {
return null
Expand Down
33 changes: 33 additions & 0 deletions packages/core-base/test/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,39 @@ describe('resolveValue', () => {
expect(resolveValue({ 'test.link': 'world' }, 'test.link')).toEqual(null)
})

test('Object.prototype built-in key paths (issue #1838)', () => {
const builtins = [
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'toLocaleString',
'toString',
'valueOf',
'__proto__'
]

// top-level: builtin name as key with value defined -> should resolve
for (const k of builtins) {
expect(resolveValue({ [k]: 'hi' }, k)).toEqual('hi')
}

// top-level: builtin name as key, NOT defined -> should return null
for (const k of builtins) {
expect(resolveValue({}, k)).toEqual(null)
}

// nested: a.<builtin>.c with value defined -> should resolve
for (const k of builtins) {
expect(resolveValue({ a: { [k]: { c: 'hi' } } }, `a.${k}.c`)).toEqual('hi')
}

// nested: a.<builtin>.c without value -> should return null
for (const k of builtins) {
expect(resolveValue({ a: {} }, `a.${k}.c`)).toEqual(null)
}
})

test('ast', () => {
expect(resolveValue(ast, 'language')).toEqual(ast.language)
expect(resolveValue(ast, 'product')).toEqual(ast.product)
Expand Down
Loading