UNPKG

1.57 kBTypeScriptView Raw
1import { SourceSpan } from '../../source/span';
2export interface BaseNodeFields {
3 loc: SourceSpan;
4}
5/**
6 * This is a convenience function for creating ASTv2 nodes, with an optional name and the node's
7 * options.
8 *
9 * ```ts
10 * export class HtmlText extends node('HtmlText').fields<{ chars: string }>() {}
11 * ```
12 *
13 * This creates a new ASTv2 node with the name `'HtmlText'` and one field `chars: string` (in
14 * addition to a `loc: SourceOffsets` field, which all nodes have).
15 *
16 * ```ts
17 * export class Args extends node().fields<{
18 * positional: PositionalArguments;
19 * named: NamedArguments
20 * }>() {}
21 * ```
22 *
23 * This creates a new un-named ASTv2 node with two fields (`positional: Positional` and `named:
24 * Named`, in addition to the generic `loc: SourceOffsets` field).
25 *
26 * Once you create a node using `node`, it is instantiated with all of its fields (including `loc`):
27 *
28 * ```ts
29 * new HtmlText({ loc: offsets, chars: someString });
30 * ```
31 */
32export declare function node(): {
33 fields<Fields extends object>(): NodeConstructor<Fields & BaseNodeFields>;
34};
35export declare function node<T extends string>(name: T): {
36 fields<Fields extends object>(): TypedNodeConstructor<T, Fields & BaseNodeFields>;
37};
38export interface NodeConstructor<Fields> {
39 new (fields: Fields): Readonly<Fields>;
40}
41declare type TypedNode<T extends string, Fields> = {
42 type: T;
43} & Readonly<Fields>;
44export interface TypedNodeConstructor<T extends string, Fields> {
45 new (options: Fields): TypedNode<T, Fields>;
46}
47export {};
48//# sourceMappingURL=node.d.ts.map
\No newline at end of file