UNPKG

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