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