UNPKG

16.3 kBTypeScriptView Raw
1import { ParserServices, TSESTree } from '../ts-estree';
2import { Scope } from './Scope';
3declare class TokenStore {
4 /**
5 * Checks whether any comments exist or not between the given 2 nodes.
6 * @param left The node to check.
7 * @param right The node to check.
8 * @returns `true` if one or more comments exist.
9 */
10 commentsExistBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token): boolean;
11 /**
12 * Gets all comment tokens directly after the given node or token.
13 * @param nodeOrToken The AST node or token to check for adjacent comment tokens.
14 * @returns An array of comments in occurrence order.
15 */
16 getCommentsAfter(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
17 /**
18 * Gets all comment tokens directly before the given node or token.
19 * @param nodeOrToken The AST node or token to check for adjacent comment tokens.
20 * @returns An array of comments in occurrence order.
21 */
22 getCommentsBefore(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
23 /**
24 * Gets all comment tokens inside the given node.
25 * @param node The AST node to get the comments for.
26 * @returns An array of comments in occurrence order.
27 */
28 getCommentsInside(node: TSESTree.Node): TSESTree.Comment[];
29 /**
30 * Gets the first token of the given node.
31 * @param node The AST node.
32 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
33 * @returns An object representing the token.
34 */
35 getFirstToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
36 /**
37 * Gets the first token between two non-overlapping nodes.
38 * @param left Node before the desired token range.
39 * @param right Node after the desired token range.
40 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
41 * @returns An object representing the token.
42 */
43 getFirstTokenBetween<T extends SourceCode.CursorWithSkipOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
44 /**
45 * Gets the first `count` tokens of the given node.
46 * @param node The AST node.
47 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
48 * @returns Tokens.
49 */
50 getFirstTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
51 /**
52 * Gets the first `count` tokens between two non-overlapping nodes.
53 * @param left Node before the desired token range.
54 * @param right Node after the desired token range.
55 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
56 * @returns Tokens between left and right.
57 */
58 getFirstTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
59 /**
60 * Gets the last token of the given node.
61 * @param node The AST node.
62 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
63 * @returns An object representing the token.
64 */
65 getLastToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
66 /**
67 * Gets the last token between two non-overlapping nodes.
68 * @param left Node before the desired token range.
69 * @param right Node after the desired token range.
70 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
71 * @returns An object representing the token.
72 */
73 getLastTokenBetween<T extends SourceCode.CursorWithSkipOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
74 /**
75 * Gets the last `count` tokens of the given node.
76 * @param node The AST node.
77 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
78 * @returns Tokens.
79 */
80 getLastTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
81 /**
82 * Gets the last `count` tokens between two non-overlapping nodes.
83 * @param left Node before the desired token range.
84 * @param right Node after the desired token range.
85 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
86 * @returns Tokens between left and right.
87 */
88 getLastTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
89 /**
90 * Gets the token that follows a given node or token.
91 * @param node The AST node or token.
92 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
93 * @returns An object representing the token.
94 */
95 getTokenAfter<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
96 /**
97 * Gets the token that precedes a given node or token.
98 * @param node The AST node or token.
99 * @param options The option object
100 * @returns An object representing the token.
101 */
102 getTokenBefore<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
103 /**
104 * Gets the token starting at the specified index.
105 * @param offset Index of the start of the token's range.
106 * @param option The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
107 * @returns The token starting at index, or null if no such token.
108 */
109 getTokenByRangeStart<T extends {
110 includeComments?: boolean;
111 }>(offset: number, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
112 /**
113 * Gets all tokens that are related to the given node.
114 * @param node The AST node.
115 * @param beforeCount The number of tokens before the node to retrieve.
116 * @param afterCount The number of tokens after the node to retrieve.
117 * @returns Array of objects representing tokens.
118 */
119 getTokens(node: TSESTree.Node, beforeCount?: number, afterCount?: number): TSESTree.Token[];
120 /**
121 * Gets all tokens that are related to the given node.
122 * @param node The AST node.
123 * @param options The option object. If this is a function then it's `options.filter`.
124 * @returns Array of objects representing tokens.
125 */
126 getTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[];
127 /**
128 * Gets the `count` tokens that follows a given node or token.
129 * @param node The AST node.
130 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
131 * @returns Tokens.
132 */
133 getTokensAfter<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
134 /**
135 * Gets the `count` tokens that precedes a given node or token.
136 * @param node The AST node.
137 * @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
138 * @returns Tokens.
139 */
140 getTokensBefore<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
141 /**
142 * Gets all of the tokens between two non-overlapping nodes.
143 * @param left Node before the desired token range.
144 * @param right Node after the desired token range.
145 * @param options The option object. If this is a function then it's `options.filter`.
146 * @returns Tokens between left and right.
147 */
148 getTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, padding?: T): SourceCode.ReturnTypeFromOptions<T>[];
149 /**
150 * Gets all of the tokens between two non-overlapping nodes.
151 * @param left Node before the desired token range.
152 * @param right Node after the desired token range.
153 * @param padding Number of extra tokens on either side of center.
154 * @returns Tokens between left and right.
155 */
156 getTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, padding?: number): SourceCode.ReturnTypeFromOptions<T>[];
157}
158declare class SourceCodeBase extends TokenStore {
159 /**
160 * Represents parsed source code.
161 * @param text The source code text.
162 * @param ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
163 */
164 constructor(text: string, ast: SourceCode.Program);
165 /**
166 * Represents parsed source code.
167 * @param config The config object.
168 */
169 constructor(config: SourceCode.SourceCodeConfig);
170 /**
171 * The parsed AST for the source code.
172 */
173 ast: SourceCode.Program;
174 /**
175 * Retrieves an array containing all comments in the source code.
176 * @returns An array of comment nodes.
177 */
178 getAllComments(): TSESTree.Comment[];
179 /**
180 * Gets all comments for the given node.
181 * @param node The AST node to get the comments for.
182 * @returns An object containing a leading and trailing array of comments indexed by their position.
183 */
184 getComments(node: TSESTree.Node): {
185 leading: TSESTree.Comment[];
186 trailing: TSESTree.Comment[];
187 };
188 /**
189 * Converts a (line, column) pair into a range index.
190 * @param loc A line/column location
191 * @returns The range index of the location in the file.
192 */
193 getIndexFromLoc(location: TSESTree.LineAndColumnData): number;
194 /**
195 * Gets the entire source text split into an array of lines.
196 * @returns The source text as an array of lines.
197 */
198 getLines(): string[];
199 /**
200 * Converts a source text index into a (line, column) pair.
201 * @param index The index of a character in a file
202 * @returns A {line, column} location object with a 0-indexed column
203 */
204 getLocFromIndex(index: number): TSESTree.LineAndColumnData;
205 /**
206 * Gets the deepest node containing a range index.
207 * @param index Range index of the desired node.
208 * @returns The node if found or `null` if not found.
209 */
210 getNodeByRangeIndex(index: number): TSESTree.Node | null;
211 /**
212 * Gets the source code for the given node.
213 * @param node The AST node to get the text for.
214 * @param beforeCount The number of characters before the node to retrieve.
215 * @param afterCount The number of characters after the node to retrieve.
216 * @returns The text representing the AST node.
217 */
218 getText(node?: TSESTree.Node, beforeCount?: number, afterCount?: number): string;
219 /**
220 * The flag to indicate that the source code has Unicode BOM.
221 */
222 hasBOM: boolean;
223 /**
224 * Determines if two nodes or tokens have at least one whitespace character
225 * between them. Order does not matter. Returns false if the given nodes or
226 * tokens overlap.
227 * This was added in v6.7.0.
228 * @since 6.7.0
229 * @param first The first node or token to check between.
230 * @param second The second node or token to check between.
231 * @returns True if there is a whitespace character between any of the tokens found between the two given nodes or tokens.
232 */
233 isSpaceBetween?(first: TSESTree.Token | TSESTree.Node, second: TSESTree.Token | TSESTree.Node): boolean;
234 /**
235 * Determines if two nodes or tokens have at least one whitespace character
236 * between them. Order does not matter. Returns false if the given nodes or
237 * tokens overlap.
238 * For backward compatibility, this method returns true if there are
239 * `JSXText` tokens that contain whitespace between the two.
240 * @param first The first node or token to check between.
241 * @param second The second node or token to check between.
242 * @returns {boolean} True if there is a whitespace character between
243 * any of the tokens found between the two given nodes or tokens.
244 * @deprecated in favor of isSpaceBetween
245 */
246 isSpaceBetweenTokens(first: TSESTree.Token, second: TSESTree.Token): boolean;
247 /**
248 * The source code split into lines according to ECMA-262 specification.
249 * This is done to avoid each rule needing to do so separately.
250 */
251 lines: string[];
252 /**
253 * The indexes in `text` that each line starts
254 */
255 lineStartIndices: number[];
256 /**
257 * The parser services of this source code.
258 */
259 parserServices: ParserServices;
260 /**
261 * The scope of this source code.
262 */
263 scopeManager: Scope.ScopeManager | null;
264 /**
265 * The original text source code. BOM was stripped from this text.
266 */
267 text: string;
268 /**
269 * All of the tokens and comments in the AST.
270 *
271 * TODO: rename to 'tokens'
272 */
273 tokensAndComments: TSESTree.Token[];
274 /**
275 * The visitor keys to traverse AST.
276 */
277 visitorKeys: SourceCode.VisitorKeys;
278 /**
279 * Split the source code into multiple lines based on the line delimiters.
280 * @param text Source code as a string.
281 * @returns Array of source code lines.
282 */
283 static splitLines(text: string): string[];
284}
285declare namespace SourceCode {
286 interface Program extends TSESTree.Program {
287 comments: TSESTree.Comment[];
288 tokens: TSESTree.Token[];
289 }
290 interface SourceCodeConfig {
291 /**
292 * The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
293 */
294 ast: Program;
295 /**
296 * The parser services.
297 */
298 parserServices: ParserServices | null;
299 /**
300 * The scope of this source code.
301 */
302 scopeManager: Scope.ScopeManager | null;
303 /**
304 * The source code text.
305 */
306 text: string;
307 /**
308 * The visitor keys to traverse AST.
309 */
310 visitorKeys: VisitorKeys | null;
311 }
312 interface VisitorKeys {
313 [nodeType: string]: string[];
314 }
315 type FilterPredicate = (token: TSESTree.Token) => boolean;
316 type ReturnTypeFromOptions<T> = T extends {
317 includeComments: true;
318 } ? TSESTree.Token : Exclude<TSESTree.Token, TSESTree.Comment>;
319 type CursorWithSkipOptions = number | FilterPredicate | {
320 /**
321 * The predicate function to choose tokens.
322 */
323 filter?: FilterPredicate;
324 /**
325 * The flag to iterate comments as well.
326 */
327 includeComments?: boolean;
328 /**
329 * The count of tokens the cursor skips.
330 */
331 skip?: number;
332 };
333 type CursorWithCountOptions = number | FilterPredicate | {
334 /**
335 * The predicate function to choose tokens.
336 */
337 filter?: FilterPredicate;
338 /**
339 * The flag to iterate comments as well.
340 */
341 includeComments?: boolean;
342 /**
343 * The maximum count of tokens the cursor iterates.
344 */
345 count?: number;
346 };
347}
348declare const SourceCode_base: typeof SourceCodeBase;
349declare class SourceCode extends SourceCode_base {
350}
351export { SourceCode };
352//# sourceMappingURL=SourceCode.d.ts.map
\No newline at end of file