Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2a4f582

Browse filesBrowse files
arshsmith1mattrbeck
authored andcommitted
fix(localize): use Object.hasOwn for placeholder lookup in translate
`translate()` looked up substitutions with `message.substitutions.hasOwnProperty(placeholder)`. A message whose placeholder is named `hasOwnProperty` stores that key on the plain substitutions object, shadowing the method, so the lookup calls the substitution value and throws a TypeError. Use `Object.hasOwn` instead, which resolves through `Object` and is unaffected by the shadowed key, matching the recent `I18nSelectPipe` fix.
1 parent 12fe700 commit 2a4f582
Copy full SHA for 2a4f582

2 files changed

+12-1Lines changed: 12 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/localize/src/utils/src/translations.ts
+1-1Lines 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(
Collapse file

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

Copy file name to clipboardExpand all lines: packages/localize/src/utils/test/translations_spec.ts
+11Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.