UNPKG

7.05 kBTypeScriptView Raw
1export as namespace regeneratorRuntime;
2
3/**
4 * The implementation of the generator.
5 */
6export type InnerFunction<T = undefined, TYield = unknown, TReturn = unknown, TNext = unknown> = (
7 this: T,
8 context: Context<TYield, TReturn, TNext>,
9) => unknown;
10
11export type ContextLocation = number | "end";
12export type CompletionType = "normal" | "return" | "throw" | "break" | "continue";
13
14// prettier-ignore
15export type TryLocationsList = ReadonlyArray<
16 | readonly [number, number]
17 | readonly [number, number | undefined, number, ContextLocation]
18>;
19
20export interface CompletionRecord {
21 type: CompletionType;
22 arg: unknown;
23}
24
25export interface TryEntry {
26 readonly tryLoc: number;
27 readonly catchLoc?: number | undefined;
28 readonly finallyLoc?: number | undefined;
29 readonly afterLoc?: ContextLocation | undefined;
30 completion?: CompletionRecord | undefined;
31}
32
33export interface DelegatedIterator {
34 iterator: Iterator<unknown, unknown, unknown>;
35}
36
37export interface Context<TYield = unknown, TReturn = unknown, TNext = unknown> {
38 readonly tryEntries: readonly [
39 { readonly tryLoc: "root" } & Omit<TryEntry, "tryLoc">,
40 ...TryEntry[],
41 ];
42
43 /**
44 * The value passed to `next()`.
45 */
46 sent: TNext;
47
48 /**
49 * The label of the previous location, needs to be set to `next` at the start of user code.
50 */
51 prev: unknown;
52
53 /**
54 * The label of the next location, is set to `'end'` when the generator needs to close abruptly.
55 */
56 next: number | "end";
57
58 /**
59 * Whether the generator has finished.
60 */
61 done: boolean;
62
63 /**
64 * The return value, set by `abrupt("return")`.
65 */
66 rval: TReturn;
67
68 /**
69 * If truthy, then it contains information about the currently `yield*` delegated iterator.
70 */
71 delegate: DelegatedIterator | undefined;
72
73 /**
74 * The generator method.
75 */
76 method: "next" | "return" | "throw";
77
78 /**
79 * The argument passed to the generator method.
80 */
81 arg: unknown;
82
83 reset(skipTempReset?: boolean): void;
84
85 /**
86 * Ends the iteration.
87 */
88 stop(): TReturn;
89
90 /**
91 * Dispatches an exception to `innerFn`
92 *
93 * @param exception The exception to dispatch.
94 */
95 dispatchException(exception: unknown): boolean;
96
97 /**
98 * @param type The completion type.
99 * @param rval The return value.
100 */
101 abrupt(type: "return", rval?: TReturn): unknown;
102
103 /**
104 * @param type The completion type.
105 * @param exception The exception to throw.
106 */
107 abrupt(type: "throw", exception?: unknown): never;
108
109 /**
110 * @param type The completion type.
111 * @param nextLoc The location label to resume iteration at.
112 */
113 abrupt(type: "break" | "continue", nextLoc: number): unknown;
114
115 /**
116 * @param type The completion type.
117 * @param arg The [[Value]] or [[Target]] of the completion record.
118 */
119 abrupt(type: CompletionType, arg?: unknown): unknown;
120
121 /**
122 * @param record The completion record.
123 * @param afterLoc The location to resume the generator at, only used by normal completions.
124 */
125 complete(record: Readonly<CompletionRecord>, afterLoc?: ContextLocation): unknown;
126
127 /**
128 * Used to signify the end of a finally block.
129 *
130 * @param finallyLoc The label of the beginning of the finally block.
131 */
132 finish(finallyLoc: number): unknown;
133
134 /**
135 * Used to obtain the exception that was thrown in the associated try block.
136 *
137 * @param tryLoc The label of the beginning of the try block.
138 */
139 catch(tryLoc: number): unknown;
140
141 /**
142 * @param iterable The iterable to delegate to.
143 * @param resultName The name of the property to assign to on this context.
144 * @param nextLoc The label of the location where to resume iteration.
145 */
146 delegateYield(
147 iterable: { [Symbol.iterator](): Iterator<TYield, unknown, unknown> },
148 resultName: string,
149 nextLoc: ContextLocation,
150 ): unknown;
151
152 [
153 /**
154 * Expects properties to match `/^t[+-]?\d*(?:(?<=\d)\.\d*|\.\d+)?(?:e[+-]?\d+)?$/`.
155 */
156 temp: string
157 ]: any;
158}
159
160export function wrap<T = undefined, TYield = unknown, TReturn = unknown, TNext = unknown>(
161 innerFn: InnerFunction<T, TYield, TReturn, TNext>,
162 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
163 outerFn?: Function | null,
164 self?: T,
165 tryLocsList?: TryLocationsList,
166): Generator<TYield, TReturn, TNext>;
167
168export interface ResolvablePromiseConstructorLike extends PromiseConstructorLike {
169 resolve<T = void>(value?: T): PromiseLike<T>;
170}
171
172export class AsyncIterator<TYield = unknown, TReturn = unknown, TNext = unknown>
173 implements globalThis.AsyncIterator<TYield, TReturn, TNext>
174{
175 constructor(
176 generator: Generator<
177 TYield | PromiseLike<TYield> | awrap<unknown>,
178 TReturn | PromiseLike<TReturn> | awrap<unknown>
179 >,
180 PromiseImpl: ResolvablePromiseConstructorLike,
181 );
182
183 // NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
184 next(...args: [] | [TNext]): Promise<IteratorResult<TYield, TReturn>>;
185 return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<TYield, TReturn>>;
186 throw(e: any): Promise<IteratorResult<TYield, TReturn>>;
187
188 [Symbol.asyncIterator](): this;
189}
190
191export function async<T = undefined, TYield = unknown, TReturn = unknown>(
192 innerFn: InnerFunction<T, TYield, TReturn>,
193 outerFn: GeneratorFunction,
194 self?: T,
195 tryLocsList?: TryLocationsList,
196 PromiseImpl?: ResolvablePromiseConstructorLike,
197): AsyncIterator<
198 TYield extends PromiseLike<infer Await> ? Await : Exclude<TYield, awrap<unknown>>,
199 TReturn extends PromiseLike<infer Await> ? Await : Exclude<TReturn, awrap<unknown>>
200>;
201export function async<T = undefined, TReturn = unknown>(
202 innerFn: InnerFunction<T, unknown, TReturn>,
203 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
204 outerFn?: Function | null,
205 self?: T,
206 tryLocsList?: TryLocationsList,
207 PromiseImpl?: ResolvablePromiseConstructorLike,
208): Promise<TReturn extends PromiseLike<infer Await> ? Await : TReturn>;
209
210export function awrap<V>(arg: V): awrap<V>;
211export class awrap<V> {
212 constructor(arg: V);
213
214 // Used to tell TypeScript that this class is to be treated as a nominal type:
215 private readonly "#private";
216
217 readonly __await: V;
218}
219
220export function isGeneratorFunction(func: unknown): func is GeneratorFunction;
221
222export function keys(object: {}): () => IteratorResult<string, undefined>;
223
224// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
225export function mark<F extends Function>(genFun: F): F & GeneratorFunction;
226
227export function values<I extends Iterator<unknown, unknown, unknown>>(iterable: { [Symbol.iterator](): I }): I;
228export function values<T>(iterableOrArrayLike: Iterable<T> | ArrayLike<T>): Iterator<T, unknown, unknown>;