Skip to content

Commit ca4a9e0

Browse files
committed
Ensure Temporal prototypes aren't writable
This PR fixes #1965: 1. For Intl.DateTimeFormat, sets `writeable: false` on `prototype` because it's created using `function` (which has a writeable `prototype`, unlike `class` which is not writeable). 2. For Temporal classes, there's a Babel bug that causes prototypes to be writeable (babel/babel#2025) and this PR works around it.
1 parent 8fef7ce commit ca4a9e0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

polyfill/lib/intl.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ if ('formatRangeToParts' in IntlDateTimeFormat.prototype) {
127127

128128
DateTimeFormat.prototype = Object.create(IntlDateTimeFormat.prototype, properties);
129129

130+
Object.defineProperty(DateTimeFormat, 'prototype', {
131+
writable: false,
132+
enumerable: false,
133+
configurable: false
134+
});
135+
130136
function resolvedOptions() {
131137
return this[ORIGINAL].resolvedOptions();
132138
}

polyfill/lib/shim.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,28 @@ function copy(target, source) {
3939
}
4040
}
4141

42+
// Work around https://github.com/babel/babel/issues/2025.
43+
const types = [
44+
globalThis.Temporal.Instant,
45+
globalThis.Temporal.Calendar,
46+
globalThis.Temporal.PlainDate,
47+
globalThis.Temporal.PlainDateTime,
48+
globalThis.Temporal.Duration,
49+
globalThis.Temporal.PlainMonthDay,
50+
// globalThis.Temporal.Now, // plain object (not a constructor), so no `prototype`
51+
globalThis.Temporal.PlainTime,
52+
globalThis.Temporal.TimeZone,
53+
globalThis.Temporal.PlainYearMonth,
54+
globalThis.Temporal.ZonedDateTime
55+
];
56+
for (const type of types) {
57+
const descriptor = Object.getOwnPropertyDescriptor(type, 'prototype');
58+
if (descriptor.configurable || descriptor.enumerable || descriptor.writable) {
59+
descriptor.configurable = false;
60+
descriptor.enumerable = false;
61+
descriptor.writable = false;
62+
Object.defineProperty(type, 'prototype', descriptor);
63+
}
64+
}
65+
4266
export { Temporal, Intl, toTemporalInstant };

0 commit comments

Comments
 (0)