import { Debug, DeprecationOptions, hasProperty, UnionToIntersection } from "./_namespaces/ts"; // The following are deprecations for the public API. Deprecated exports are removed from the compiler itself // and compatible implementations are added here, along with an appropriate deprecation warning using // the `@deprecated` JSDoc tag as well as the `Debug.deprecate` API. // // Deprecations fall into one of three categories: // // - "soft" - Soft deprecations are indicated with the `@deprecated` JSDoc Tag. // - "warn" - Warning deprecations are indicated with the `@deprecated` JSDoc Tag and a diagnostic message (assuming a compatible host). // - "error" - Error deprecations are either indicated with the `@deprecated` JSDoc tag and will throw a `TypeError` when invoked, or removed from the API entirely. // // Once we have determined enough time has passed after a deprecation has been marked as `"warn"` or `"error"`, it will be removed from the public API. /** * Defines a list of overloads by ordinal * * @internal */ export type OverloadDefinitions = { readonly [P in number]: (...args: any[]) => any; }; /** A function that returns the ordinal of the overload that matches the provided arguments */ type OverloadBinder = (args: OverloadParameters) => OverloadKeys | undefined; /** * Extracts the ordinals from an set of overload definitions. * * @internal */ export type OverloadKeys = Extract; /** * Extracts a union of the potential parameter lists for each overload. * * @internal */ export type OverloadParameters = Parameters<{ [P in OverloadKeys]: T[P]; }[OverloadKeys]>; // NOTE: the following doesn't work in TS 4.4 (the current LKG in main), so we have to use UnionToIntersection for now // type OverloadFunction any)[] = [], O = unknown> = // R["length"] extends keyof T ? OverloadFunction : // unknown extends O ? never : O; /** * Constructs an intersection of each overload in a set of overload definitions. * * @internal */ export type OverloadFunction = UnionToIntersection; /** * Maps each ordinal in a set of overload definitions to a function that can be used to bind its arguments. * * @internal */ export type OverloadBinders = { [P in OverloadKeys]: (args: OverloadParameters) => boolean | undefined; }; /** * Defines deprecations for specific overloads by ordinal. * * @internal */ export type OverloadDeprecations = { [P in OverloadKeys]?: DeprecationOptions; }; /** @internal */ export function createOverload(name: string, overloads: T, binder: OverloadBinders, deprecations?: OverloadDeprecations) { Object.defineProperty(call, "name", { ...Object.getOwnPropertyDescriptor(call, "name"), value: name }); if (deprecations) { for (const key of Object.keys(deprecations)) { const index = +key as (keyof T & number); if (!isNaN(index) && hasProperty(overloads, `${index}`)) { overloads[index] = Debug.deprecate(overloads[index], { ...deprecations[index], name }); } } } const bind = createBinder(overloads, binder); return call as OverloadFunction; function call(...args: OverloadParameters) { const index = bind(args); const fn = index !== undefined ? overloads[index] : undefined; if (typeof fn === "function") { return fn(...args); } throw new TypeError("Invalid arguments"); } } function createBinder(overloads: T, binder: OverloadBinders): OverloadBinder { return args => { for (let i = 0; hasProperty(overloads, `${i}`) && hasProperty(binder, `${i}`); i++) { const fn = binder[i]; if (fn(args)) { return i as OverloadKeys; } } }; } /** @internal */ export interface OverloadBuilder { overload(overloads: T): BindableOverloadBuilder; } /** @internal */ export interface BindableOverloadBuilder { bind(binder: OverloadBinders): BoundOverloadBuilder; } /** @internal */ export interface FinishableOverloadBuilder { finish(): OverloadFunction; } /** @internal */ export interface BoundOverloadBuilder extends FinishableOverloadBuilder { deprecate(deprecations: OverloadDeprecations): FinishableOverloadBuilder; } // NOTE: We only use this "builder" because we don't infer correctly when calling `createOverload` directly in < TS 4.7, // but lib is currently at TS 4.4. We can switch to directly calling `createOverload` when we update LKG in main. /** @internal */ export function buildOverload(name: string): OverloadBuilder { return { overload: overloads => ({ bind: binder => ({ finish: () => createOverload(name, overloads, binder), deprecate: deprecations => ({ finish: () => createOverload(name, overloads, binder, deprecations) }) }) }) }; }