Skip to content

Commit 5b1bc5e

Browse files
authored
Type the intrinsics container, and remove usage of ESGetIntrinsic (#54)
1 parent 41ab6bc commit 5b1bc5e

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

lib/intrinsicclass.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
import ESGetIntrinsic from 'es-abstract/GetIntrinsic.js';
1+
import { Temporal } from '.';
22

33
import { DEBUG } from './debug';
4-
const INTRINSICS = {};
54

6-
const customUtilInspectFormatters = {
5+
type TemporalIntrinsics = Omit<typeof Temporal, 'Now'>;
6+
type TemporalIntrinsicRegistrations = {
7+
[key in keyof TemporalIntrinsics as `Temporal.${key}`]: TemporalIntrinsics[key];
8+
};
9+
type TemporalIntrinsicPrototypeRegistrations = {
10+
[key in keyof TemporalIntrinsics as `Temporal.${key}.prototype`]: TemporalIntrinsics[key]['prototype'];
11+
};
12+
type TemporalIntrinsicRegisteredKeys = {
13+
[key in keyof TemporalIntrinsicRegistrations as `%${key}%`]: TemporalIntrinsicRegistrations[key];
14+
};
15+
16+
interface StandaloneIntrinsics {
17+
'Temporal.Calendar.from': typeof Temporal.Calendar.from;
18+
'Temporal.TimeZone.prototype.getOffsetNanosecondsFor': typeof Temporal.TimeZone.prototype.getOffsetNanosecondsFor;
19+
}
20+
type RegisteredStandaloneIntrinsics = { [key in keyof StandaloneIntrinsics as `%${key}%`]: StandaloneIntrinsics[key] };
21+
const INTRINSICS: Partial<TemporalIntrinsicRegisteredKeys & RegisteredStandaloneIntrinsics> = {};
22+
23+
type customFormatFunction<T> = (
24+
this: T,
25+
depth: number,
26+
options: { stylize: (value: unknown, type: 'number' | 'special') => string }
27+
) => string;
28+
const customUtilInspectFormatters: Partial<
29+
{ [key in keyof TemporalIntrinsicRegistrations]: customFormatFunction<TemporalIntrinsicRegistrations[key]> }
30+
> = {
731
['Temporal.Duration'](depth, options) {
832
const descr = options.stylize(`${this[Symbol.toStringTag]} <${this}>`, 'special');
933
if (depth < 1) return descr;
@@ -30,7 +54,10 @@ function defaultUtilInspectFormatter(this: any, depth, options) {
3054
return options.stylize(`${this[Symbol.toStringTag]} <${this}>`, 'special');
3155
}
3256

33-
export function MakeIntrinsicClass(Class, name) {
57+
export function MakeIntrinsicClass(
58+
Class: TemporalIntrinsicRegistrations[typeof name],
59+
name: keyof TemporalIntrinsicRegistrations
60+
) {
3461
Object.defineProperty(Class.prototype, Symbol.toStringTag, {
3562
value: name,
3663
writable: false,
@@ -62,12 +89,25 @@ export function MakeIntrinsicClass(Class, name) {
6289
DefineIntrinsic(`${name}.prototype`, Class.prototype);
6390
}
6491

65-
export function DefineIntrinsic(name, value) {
92+
type IntrinsicDefinitionKeys =
93+
| keyof TemporalIntrinsicRegistrations
94+
| keyof TemporalIntrinsicPrototypeRegistrations
95+
| keyof StandaloneIntrinsics;
96+
export function DefineIntrinsic<KeyT extends keyof TemporalIntrinsicRegistrations>(
97+
name: KeyT,
98+
value: TemporalIntrinsicRegistrations[KeyT]
99+
);
100+
export function DefineIntrinsic<KeyT extends keyof TemporalIntrinsicPrototypeRegistrations>(
101+
name: KeyT,
102+
value: TemporalIntrinsicPrototypeRegistrations[KeyT]
103+
);
104+
export function DefineIntrinsic<KeyT extends keyof StandaloneIntrinsics>(name: KeyT, value: StandaloneIntrinsics[KeyT]);
105+
export function DefineIntrinsic<KeyT>(name: KeyT, value: never);
106+
export function DefineIntrinsic<KeyT extends IntrinsicDefinitionKeys>(name: KeyT, value: unknown) {
66107
const key = `%${name}%`;
67108
if (INTRINSICS[key] !== undefined) throw new Error(`intrinsic ${name} already exists`);
68109
INTRINSICS[key] = value;
69110
}
70-
71-
export function GetIntrinsic(intrinsic) {
72-
return intrinsic in INTRINSICS ? INTRINSICS[intrinsic] : ESGetIntrinsic(intrinsic);
111+
export function GetIntrinsic<KeyT extends keyof typeof INTRINSICS>(intrinsic: KeyT): typeof INTRINSICS[KeyT] {
112+
return INTRINSICS[intrinsic];
73113
}

0 commit comments

Comments
 (0)