Skip to content

Commit 36aad3a

Browse files
committed
fix(localize): use Object.hasOwn for placeholder lookup in translate
1 parent 731d665 commit 36aad3a

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎packages/localize/src/utils/src/translations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function translate(
6868
return [
6969
translation.messageParts,
7070
translation.placeholderNames.map((placeholder) => {
71-
if (message.substitutions.hasOwnProperty(placeholder)) {
71+
if (Object.hasOwn(message.substitutions, placeholder)) {
7272
return message.substitutions[placeholder];
7373
} else {
7474
throw new Error(

‎packages/localize/src/utils/test/translations_spec.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ describe('utils', () => {
137137
);
138138
});
139139

140+
it('should substitute a placeholder whose name shadows an Object.prototype member', () => {
141+
// A placeholder legitimately named `hasOwnProperty` puts that key on the
142+
// substitutions object, so calling it as a method would throw a TypeError.
143+
expect(
144+
doTranslate(
145+
{'abc{$hasOwnProperty}def': 'abc{$hasOwnProperty}def'},
146+
parts`abc${1 + 2}:hasOwnProperty:def`,
147+
),
148+
).toEqual(parts`abc${3}def`);
149+
});
150+
140151
it('(with identity translations) should render template literals as-is', () => {
141152
const translations = {
142153
'abc': 'abc',

0 commit comments

Comments
 (0)