1 import { ParserServices, TSESTree } from '../ts-estree';
\r
2 import { Scope } from './Scope';
\r
3 declare class TokenStore {
\r
5 * Checks whether any comments exist or not between the given 2 nodes.
\r
6 * @param left The node to check.
\r
7 * @param right The node to check.
\r
8 * @returns `true` if one or more comments exist.
\r
10 commentsExistBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token): boolean;
\r
12 * Gets all comment tokens directly after the given node or token.
\r
13 * @param nodeOrToken The AST node or token to check for adjacent comment tokens.
\r
14 * @returns An array of comments in occurrence order.
\r
16 getCommentsAfter(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
\r
18 * Gets all comment tokens directly before the given node or token.
\r
19 * @param nodeOrToken The AST node or token to check for adjacent comment tokens.
\r
20 * @returns An array of comments in occurrence order.
\r
22 getCommentsBefore(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[];
\r
24 * Gets all comment tokens inside the given node.
\r
25 * @param node The AST node to get the comments for.
\r
26 * @returns An array of comments in occurrence order.
\r
28 getCommentsInside(node: TSESTree.Node): TSESTree.Comment[];
\r
30 * Gets the first token of the given node.
\r
31 * @param node The AST node.
\r
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`.
\r
33 * @returns An object representing the token.
\r
35 getFirstToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
\r
37 * Gets the first token between two non-overlapping nodes.
\r
38 * @param left Node before the desired token range.
\r
39 * @param right Node after the desired token range.
\r
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`.
\r
41 * @returns An object representing the token.
\r
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;
\r
45 * Gets the first `count` tokens of the given node.
\r
46 * @param node The AST node.
\r
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`.
\r
50 getFirstTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
\r
52 * Gets the first `count` tokens between two non-overlapping nodes.
\r
53 * @param left Node before the desired token range.
\r
54 * @param right Node after the desired token range.
\r
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`.
\r
56 * @returns Tokens between left and right.
\r
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>[];
\r
60 * Gets the last token of the given node.
\r
61 * @param node The AST node.
\r
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`.
\r
63 * @returns An object representing the token.
\r
65 getLastToken<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
\r
67 * Gets the last token between two non-overlapping nodes.
\r
68 * @param left Node before the desired token range.
\r
69 * @param right Node after the desired token range.
\r
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`.
\r
71 * @returns An object representing the token.
\r
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;
\r
75 * Gets the last `count` tokens of the given node.
\r
76 * @param node The AST node.
\r
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`.
\r
80 getLastTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
\r
82 * Gets the last `count` tokens between two non-overlapping nodes.
\r
83 * @param left Node before the desired token range.
\r
84 * @param right Node after the desired token range.
\r
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`.
\r
86 * @returns Tokens between left and right.
\r
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>[];
\r
90 * Gets the token that follows a given node or token.
\r
91 * @param node The AST node or token.
\r
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`.
\r
93 * @returns An object representing the token.
\r
95 getTokenAfter<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
\r
97 * Gets the token that precedes a given node or token.
\r
98 * @param node The AST node or token.
\r
99 * @param options The option object
\r
100 * @returns An object representing the token.
\r
102 getTokenBefore<T extends SourceCode.CursorWithSkipOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
\r
104 * Gets the token starting at the specified index.
\r
105 * @param offset Index of the start of the token's range.
\r
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`.
\r
107 * @returns The token starting at index, or null if no such token.
\r
109 getTokenByRangeStart<T extends {
\r
110 includeComments?: boolean;
\r
111 }>(offset: number, options?: T): SourceCode.ReturnTypeFromOptions<T> | null;
\r
113 * Gets all tokens that are related to the given node.
\r
114 * @param node The AST node.
\r
115 * @param beforeCount The number of tokens before the node to retrieve.
\r
116 * @param afterCount The number of tokens after the node to retrieve.
\r
117 * @returns Array of objects representing tokens.
\r
119 getTokens(node: TSESTree.Node, beforeCount?: number, afterCount?: number): TSESTree.Token[];
\r
121 * Gets all tokens that are related to the given node.
\r
122 * @param node The AST node.
\r
123 * @param options The option object. If this is a function then it's `options.filter`.
\r
124 * @returns Array of objects representing tokens.
\r
126 getTokens<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[];
\r
128 * Gets the `count` tokens that follows a given node or token.
\r
129 * @param node The AST node.
\r
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`.
\r
133 getTokensAfter<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
\r
135 * Gets the `count` tokens that precedes a given node or token.
\r
136 * @param node The AST node.
\r
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`.
\r
140 getTokensBefore<T extends SourceCode.CursorWithCountOptions>(node: TSESTree.Node | TSESTree.Token | TSESTree.Comment, options?: T): SourceCode.ReturnTypeFromOptions<T>[];
\r
142 * Gets all of the tokens between two non-overlapping nodes.
\r
143 * @param left Node before the desired token range.
\r
144 * @param right Node after the desired token range.
\r
145 * @param options The option object. If this is a function then it's `options.filter`.
\r
146 * @returns Tokens between left and right.
\r
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>[];
\r
150 * Gets all of the tokens between two non-overlapping nodes.
\r
151 * @param left Node before the desired token range.
\r
152 * @param right Node after the desired token range.
\r
153 * @param padding Number of extra tokens on either side of center.
\r
154 * @returns Tokens between left and right.
\r
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>[];
\r
158 declare class SourceCodeBase extends TokenStore {
\r
160 * Represents parsed source code.
\r
161 * @param text The source code text.
\r
162 * @param ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
\r
164 constructor(text: string, ast: SourceCode.Program);
\r
166 * Represents parsed source code.
\r
167 * @param config The config object.
\r
169 constructor(config: SourceCode.SourceCodeConfig);
\r
171 * The parsed AST for the source code.
\r
173 ast: SourceCode.Program;
\r
175 * Retrieves an array containing all comments in the source code.
\r
176 * @returns An array of comment nodes.
\r
178 getAllComments(): TSESTree.Comment[];
\r
180 * Gets all comments for the given node.
\r
181 * @param node The AST node to get the comments for.
\r
182 * @returns An object containing a leading and trailing array of comments indexed by their position.
\r
184 getComments(node: TSESTree.Node): {
\r
185 leading: TSESTree.Comment[];
\r
186 trailing: TSESTree.Comment[];
\r
189 * Converts a (line, column) pair into a range index.
\r
190 * @param loc A line/column location
\r
191 * @returns The range index of the location in the file.
\r
193 getIndexFromLoc(location: TSESTree.LineAndColumnData): number;
\r
195 * Gets the entire source text split into an array of lines.
\r
196 * @returns The source text as an array of lines.
\r
198 getLines(): string[];
\r
200 * Converts a source text index into a (line, column) pair.
\r
201 * @param index The index of a character in a file
\r
202 * @returns A {line, column} location object with a 0-indexed column
\r
204 getLocFromIndex(index: number): TSESTree.LineAndColumnData;
\r
206 * Gets the deepest node containing a range index.
\r
207 * @param index Range index of the desired node.
\r
208 * @returns The node if found or `null` if not found.
\r
210 getNodeByRangeIndex(index: number): TSESTree.Node | null;
\r
212 * Gets the source code for the given node.
\r
213 * @param node The AST node to get the text for.
\r
214 * @param beforeCount The number of characters before the node to retrieve.
\r
215 * @param afterCount The number of characters after the node to retrieve.
\r
216 * @returns The text representing the AST node.
\r
218 getText(node?: TSESTree.Node, beforeCount?: number, afterCount?: number): string;
\r
220 * The flag to indicate that the source code has Unicode BOM.
\r
224 * Determines if two nodes or tokens have at least one whitespace character
\r
225 * between them. Order does not matter. Returns false if the given nodes or
\r
227 * This was added in v6.7.0.
\r
229 * @param first The first node or token to check between.
\r
230 * @param second The second node or token to check between.
\r
231 * @returns True if there is a whitespace character between any of the tokens found between the two given nodes or tokens.
\r
233 isSpaceBetween?(first: TSESTree.Token | TSESTree.Comment | TSESTree.Node, second: TSESTree.Token | TSESTree.Comment | TSESTree.Node): boolean;
\r
235 * Determines if two nodes or tokens have at least one whitespace character
\r
236 * between them. Order does not matter. Returns false if the given nodes or
\r
238 * For backward compatibility, this method returns true if there are
\r
239 * `JSXText` tokens that contain whitespace between the two.
\r
240 * @param first The first node or token to check between.
\r
241 * @param second The second node or token to check between.
\r
242 * @returns {boolean} True if there is a whitespace character between
\r
243 * any of the tokens found between the two given nodes or tokens.
\r
244 * @deprecated in favor of isSpaceBetween
\r
246 isSpaceBetweenTokens(first: TSESTree.Token, second: TSESTree.Token): boolean;
\r
248 * The source code split into lines according to ECMA-262 specification.
\r
249 * This is done to avoid each rule needing to do so separately.
\r
253 * The indexes in `text` that each line starts
\r
255 lineStartIndices: number[];
\r
257 * The parser services of this source code.
\r
259 parserServices: ParserServices;
\r
261 * The scope of this source code.
\r
263 scopeManager: Scope.ScopeManager | null;
\r
265 * The original text source code. BOM was stripped from this text.
\r
269 * All of the tokens and comments in the AST.
\r
271 tokensAndComments: (TSESTree.Comment | TSESTree.Token)[];
\r
273 * The visitor keys to traverse AST.
\r
275 visitorKeys: SourceCode.VisitorKeys;
\r
277 * Split the source code into multiple lines based on the line delimiters.
\r
278 * @param text Source code as a string.
\r
279 * @returns Array of source code lines.
\r
281 static splitLines(text: string): string[];
\r
283 declare namespace SourceCode {
\r
284 interface Program extends TSESTree.Program {
\r
285 comments: TSESTree.Comment[];
\r
286 tokens: TSESTree.Token[];
\r
288 interface SourceCodeConfig {
\r
290 * The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
\r
294 * The parser services.
\r
296 parserServices: ParserServices | null;
\r
298 * The scope of this source code.
\r
300 scopeManager: Scope.ScopeManager | null;
\r
302 * The source code text.
\r
306 * The visitor keys to traverse AST.
\r
308 visitorKeys: VisitorKeys | null;
\r
310 interface VisitorKeys {
\r
311 [nodeType: string]: string[];
\r
313 type FilterPredicate = (tokenOrComment: TSESTree.Token | TSESTree.Comment) => boolean;
\r
314 type ReturnTypeFromOptions<T> = T extends {
\r
315 includeComments: true;
\r
316 } ? TSESTree.Token : Exclude<TSESTree.Token, TSESTree.Comment>;
\r
317 type CursorWithSkipOptions = number | FilterPredicate | {
\r
319 * The predicate function to choose tokens.
\r
321 filter?: FilterPredicate;
\r
323 * The flag to iterate comments as well.
\r
325 includeComments?: boolean;
\r
327 * The count of tokens the cursor skips.
\r
331 type CursorWithCountOptions = number | FilterPredicate | {
\r
333 * The predicate function to choose tokens.
\r
335 filter?: FilterPredicate;
\r
337 * The flag to iterate comments as well.
\r
339 includeComments?: boolean;
\r
341 * The maximum count of tokens the cursor iterates.
\r
346 declare const SourceCode_base: typeof SourceCodeBase;
\r
347 declare class SourceCode extends SourceCode_base {
\r
349 export { SourceCode };
\r
350 //# sourceMappingURL=SourceCode.d.ts.map
\r