UNPKG

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