Skip to content

Commit 6f3ca25

Browse files
committed
Ensure Temporal prototypes aren't writable
Port of tc39/proposal-temporal#1974
1 parent e9c439b commit 6f3ca25

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,29 @@
88
import * as Temporal from './temporal';
99
import * as Intl from './intl';
1010
import { toTemporalInstant } from './legacydate';
11+
12+
// Work around https://github.com/babel/babel/issues/2025.
13+
const types = [
14+
Temporal.Instant,
15+
Temporal.Calendar,
16+
Temporal.PlainDate,
17+
Temporal.PlainDateTime,
18+
Temporal.Duration,
19+
Temporal.PlainMonthDay,
20+
// Temporal.Now, // plain object (not a constructor), so no `prototype`
21+
Temporal.PlainTime,
22+
Temporal.TimeZone,
23+
Temporal.PlainYearMonth,
24+
Temporal.ZonedDateTime
25+
];
26+
for (const type of types) {
27+
const descriptor = Object.getOwnPropertyDescriptor(type, 'prototype') as PropertyDescriptor;
28+
if (descriptor.configurable || descriptor.enumerable || descriptor.writable) {
29+
descriptor.configurable = false;
30+
descriptor.enumerable = false;
31+
descriptor.writable = false;
32+
Object.defineProperty(type, 'prototype', descriptor);
33+
}
34+
}
35+
1136
export { Temporal, Intl, toTemporalInstant };

lib/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Object.defineProperty(globalThis, 'Temporal', {
1212
enumerable: false,
1313
configurable: true
1414
});
15-
const globalTemporal = (globalThis as any).Temporal;
15+
const globalTemporal = (globalThis as unknown as { Temporal: typeof Temporal }).Temporal;
1616
copy(globalTemporal, Temporal);
1717
Object.defineProperty(globalTemporal, Symbol.toStringTag, {
1818
value: 'Temporal',

lib/intl.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ Object.defineProperty(DateTimeFormatImpl, 'name', {
174174
value: 'DateTimeFormat'
175175
});
176176

177-
export const DateTimeFormat = DateTimeFormatImpl as unknown as typeof Intl.DateTimeFormat;
178-
179177
DateTimeFormatImpl.supportedLocalesOf = function (
180178
locales: Params['supportedLocalesOf'][0],
181179
options: Params['supportedLocalesOf'][1]
@@ -199,6 +197,15 @@ if ('formatRangeToParts' in IntlDateTimeFormat.prototype) {
199197

200198
DateTimeFormatImpl.prototype = Object.create(IntlDateTimeFormat.prototype, properties);
201199

200+
// Ensure that the prototype isn't writeable.
201+
Object.defineProperty(DateTimeFormatImpl, 'prototype', {
202+
writable: false,
203+
enumerable: false,
204+
configurable: false
205+
});
206+
207+
export const DateTimeFormat = DateTimeFormatImpl as unknown as typeof Intl.DateTimeFormat;
208+
202209
function resolvedOptions(this: DateTimeFormatImpl): Return['resolvedOptions'] {
203210
return this[ORIGINAL].resolvedOptions();
204211
}

0 commit comments

Comments
 (0)