UNPKG

4.99 kBTypeScriptView Raw
1// Type definitions for esquery 1.5
2// Project: https://github.com/jrfeenst/esquery
3// Definitions by: cherryblossom000 <https://github.com/cherryblossom000>
4// Brad Zacher <https://github.com/bradzacher>
5// Brett Zamir <https://github.com/brettz9>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8import { Node } from 'estree';
9
10export as namespace esquery;
11
12export = query;
13
14/** Query the code AST using the selector string. */
15declare function query(ast: Node, selector: string): Node[];
16
17declare namespace query {
18 interface ESQueryOptions {
19 nodeTypeKey?: string;
20 visitorKeys?: { [nodeType: string]: readonly string[] };
21 fallback?: (node: Node) => string[];
22 matchClass?: (className: string, node: Node, ancestry: Node[]) => boolean;
23 }
24
25 /** Parse a selector and return its AST. */
26 function parse(selector: string): Selector;
27 /** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
28 function match(ast: Node, selector: Selector, options?: ESQueryOptions): Node[];
29 /** Given a `node` and its ancestors, determine if `node` is matched by `selector`. */
30 function matches(node: Node, selector: Selector, ancestry?: Node[], options?: ESQueryOptions): boolean;
31 /** Query the code AST using the selector string. */
32 function query(ast: Node, selector: string, options?: ESQueryOptions): Node[];
33
34 /** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
35 function traverse(
36 ast: Node,
37 selector: string,
38 visitor: (node: Node, parent: Node, ancestry: Node[]) => void,
39 options?: ESQueryOptions,
40 ): void;
41
42 //
43 // Unions
44 //
45 type Selector =
46 | Field
47 | Type
48 | Sequence
49 | Identifier
50 | Wildcard
51 | Attribute
52 | NthChild
53 | NthLastChild
54 | Descendant
55 | Child
56 | Sibling
57 | Adjacent
58 | Negation
59 | Matches
60 | Has
61 | Class;
62 type MultiSelector = Sequence | Negation | Matches | Has;
63 type BinarySelector = Descendant | Child | Sibling | Adjacent;
64 type NthSelector = NthChild | NthLastChild;
65 type SubjectSelector = NthSelector | BinarySelector | MultiSelector | Identifier | Wildcard | Attribute;
66 type Literal = StringLiteral | NumericLiteral;
67
68 //
69 // Base Atoms
70 //
71 interface Atom {
72 type: string;
73 }
74 interface SubjectSelectorAtom extends Atom {
75 subject?: boolean | undefined;
76 }
77 interface NthSelectorAtom extends SubjectSelectorAtom {
78 index: NumericLiteral;
79 }
80 interface BinarySelectorAtom extends SubjectSelectorAtom {
81 type: 'child' | 'sibling' | 'adjacent' | 'descendant';
82 left: SubjectSelector;
83 right: SubjectSelector;
84 }
85 interface MultiSelectorAtom extends SubjectSelectorAtom {
86 selectors: SubjectSelector[];
87 }
88 interface LiteralAtom extends Atom {
89 type: 'literal';
90 value: string | number;
91 }
92
93 //
94 // Literals
95 //
96 interface StringLiteral extends LiteralAtom {
97 value: string;
98 }
99 interface NumericLiteral extends LiteralAtom {
100 value: number;
101 }
102 interface RegExpLiteral extends Atom {
103 type: 'regexp';
104 value: RegExp;
105 }
106
107 //
108 // Atoms
109 //
110 interface Field extends Atom {
111 type: 'field';
112 name: string;
113 }
114 interface Type extends Atom {
115 type: 'type';
116 value: string;
117 }
118 interface Sequence extends MultiSelectorAtom {
119 type: 'compound';
120 }
121 interface Identifier extends SubjectSelectorAtom {
122 type: 'identifier';
123 value: string;
124 }
125 interface Wildcard extends SubjectSelectorAtom {
126 type: 'wildcard';
127 value: '*';
128 }
129 interface Attribute extends SubjectSelectorAtom {
130 type: 'attribute';
131 name: string;
132 operator?: '=' | '!=' | '>' | '<' | '>=' | '<=' | undefined;
133 value?: Literal | RegExpLiteral | Type | undefined;
134 }
135 interface NthChild extends NthSelectorAtom {
136 type: 'nth-child';
137 }
138 interface NthLastChild extends NthSelectorAtom {
139 type: 'nth-last-child';
140 }
141 interface Descendant extends BinarySelectorAtom {
142 type: 'descendant';
143 }
144 interface Child extends BinarySelectorAtom {
145 type: 'child';
146 }
147 interface Sibling extends BinarySelectorAtom {
148 type: 'sibling';
149 }
150 interface Adjacent extends BinarySelectorAtom {
151 type: 'adjacent';
152 }
153 interface Negation extends MultiSelectorAtom {
154 type: 'not';
155 }
156 interface Matches extends MultiSelectorAtom {
157 type: 'matches';
158 }
159 interface Has extends MultiSelectorAtom {
160 type: 'has';
161 }
162 interface Class extends Atom {
163 type: 'class';
164 name: 'declaration' | 'expression' | 'function' | 'pattern' | 'statement';
165 }
166}