1 import { ParserServices, TSESTree } from '../ts-estree';
2 import { Scope } from './Scope';
3 declare class TokenStore {
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.
10 commentsExistBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token): boolean;
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.
16 getCommentsAfter(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
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.
22 getCommentsBefore(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
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.
28 getCommentsInside(node: TSESTree.Node): TSESTree.Comment[];
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.
35 getFirstToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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.
43 getFirstTokenBetween<T extends SourceCode.CursorWithSkipOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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`.
50 getFirstTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
58 getFirstTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
65 getLastToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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.
73 getLastTokenBetween<T extends SourceCode.CursorWithSkipOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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`.
80 getLastTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
88 getLastTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
95 getTokenAfter<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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.
102 getTokenBefore<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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.
109 getTokenByRangeStart<T extends {
110 includeComments?: boolean;
111 }>(offset: number, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
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.
119 getTokens(node: TSESTree.Node, beforeCount?: number, afterCount?: number): TSESTree.Token[];
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.
126 getTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[];
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`.
133 getTokensAfter<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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`.
140 getTokensBefore<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
148 getTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, padding?: T): SourceCode.ReturnTypeFromOptions<T>[];
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.
156 getTokensBetween<T extends SourceCode.CursorWithCountOptions>(left: TSESTree.Node | TSESTree.Token | TSESTree.Comment, right: TSESTree.Node | TSESTree.Token | TSESTree.Comment, padding?: number): SourceCode.ReturnTypeFromOptions<T>[];
158 declare class SourceCodeBase extends TokenStore {
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.
164 constructor(text: string, ast: SourceCode.Program);
166 * Represents parsed source code.
167 * @param config The config object.
169 constructor(config: SourceCode.SourceCodeConfig);
171 * The parsed AST for the source code.
173 ast: SourceCode.Program;
175 * Retrieves an array containing all comments in the source code.
176 * @returns An array of comment nodes.
178 getAllComments(): TSESTree.Comment[];
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.
184 getComments(node: TSESTree.Node): {
185 leading: TSESTree.Comment[];
186 trailing: TSESTree.Comment[];
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.
193 getIndexFromLoc(location: TSESTree.LineAndColumnData): number;
195 * Gets the entire source text split into an array of lines.
196 * @returns The source text as an array of lines.
198 getLines(): string[];
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
204 getLocFromIndex(index: number): TSESTree.LineAndColumnData;
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.
210 getNodeByRangeIndex(index: number): TSESTree.Node | null;
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.
218 getText(node?: TSESTree.Node, beforeCount?: number, afterCount?: number): string;
220 * The flag to indicate that the source code has Unicode BOM.
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
227 * This was added in v6.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.
233 isSpaceBetween?(first: TSESTree.Token | TSESTree.Comment | TSESTree.Node, second: TSESTree.Token | TSESTree.Comment | TSESTree.Node): boolean;
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
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
246 isSpaceBetweenTokens(first: TSESTree.Token, second: TSESTree.Token): boolean;
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.
253 * The indexes in `text` that each line starts
255 lineStartIndices: number[];
257 * The parser services of this source code.
259 parserServices: ParserServices;
261 * The scope of this source code.
263 scopeManager: Scope.ScopeManager | null;
265 * The original text source code. BOM was stripped from this text.
269 * All of the tokens and comments in the AST.
271 tokensAndComments: (TSESTree.Comment | TSESTree.Token)[];
273 * The visitor keys to traverse AST.
275 visitorKeys: SourceCode.VisitorKeys;
277 * Split the source code into multiple lines based on the line delimiters.
278 * @param text Source code as a string.
279 * @returns Array of source code lines.
281 static splitLines(text: string): string[];
283 declare namespace SourceCode {
284 interface Program extends TSESTree.Program {
285 comments: TSESTree.Comment[];
286 tokens: TSESTree.Token[];
288 interface SourceCodeConfig {
290 * The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
294 * The parser services.
296 parserServices: ParserServices | null;
298 * The scope of this source code.
300 scopeManager: Scope.ScopeManager | null;
302 * The source code text.
306 * The visitor keys to traverse AST.
308 visitorKeys: VisitorKeys | null;
310 interface VisitorKeys {
311 [nodeType: string]: string[];
313 type FilterPredicate = (tokenOrComment: TSESTree.Token | TSESTree.Comment) => boolean;
314 type ReturnTypeFromOptions<T> = T extends {
315 includeComments: true;
316 } ? TSESTree.Token : Exclude<TSESTree.Token, TSESTree.Comment>;
317 type CursorWithSkipOptions = number | FilterPredicate | {
319 * The predicate function to choose tokens.
321 filter?: FilterPredicate;
323 * The flag to iterate comments as well.
325 includeComments?: boolean;
327 * The count of tokens the cursor skips.
331 type CursorWithCountOptions = number | FilterPredicate | {
333 * The predicate function to choose tokens.
335 filter?: FilterPredicate;
337 * The flag to iterate comments as well.
339 includeComments?: boolean;
341 * The maximum count of tokens the cursor iterates.
346 declare const SourceCode_base: typeof SourceCodeBase;
347 declare class SourceCode extends SourceCode_base {
349 export { SourceCode };
350 //# sourceMappingURL=SourceCode.d.ts.map