UNPKG

39.3 kBTypeScriptView Raw
1// Type definitions for eslint 8.4
2// Project: https://eslint.org
3// Definitions by: Pierre-Marie Dartus <https://github.com/pmdartus>
4// Jed Fox <https://github.com/j-f1>
5// Saad Quadri <https://github.com/saadq>
6// Jason Kwok <https://github.com/JasonHK>
7// Brad Zacher <https://github.com/bradzacher>
8// JounQin <https://github.com/JounQin>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10
11/// <reference path="helpers.d.ts" />
12
13import * as ESTree from "estree";
14import { JSONSchema4 } from "json-schema";
15
16export namespace AST {
17 type TokenType =
18 | "Boolean"
19 | "Null"
20 | "Identifier"
21 | "Keyword"
22 | "Punctuator"
23 | "JSXIdentifier"
24 | "JSXText"
25 | "Numeric"
26 | "String"
27 | "RegularExpression";
28
29 interface Token {
30 type: TokenType;
31 value: string;
32 range: Range;
33 loc: SourceLocation;
34 }
35
36 interface SourceLocation {
37 start: ESTree.Position;
38 end: ESTree.Position;
39 }
40
41 type Range = [number, number];
42
43 interface Program extends ESTree.Program {
44 comments: ESTree.Comment[];
45 tokens: Token[];
46 loc: SourceLocation;
47 range: Range;
48 }
49}
50
51export namespace Scope {
52 interface ScopeManager {
53 scopes: Scope[];
54 globalScope: Scope | null;
55
56 acquire(node: ESTree.Node, inner?: boolean): Scope | null;
57
58 getDeclaredVariables(node: ESTree.Node): Variable[];
59 }
60
61 interface Scope {
62 type:
63 | "block"
64 | "catch"
65 | "class"
66 | "for"
67 | "function"
68 | "function-expression-name"
69 | "global"
70 | "module"
71 | "switch"
72 | "with"
73 | "TDZ";
74 isStrict: boolean;
75 upper: Scope | null;
76 childScopes: Scope[];
77 variableScope: Scope;
78 block: ESTree.Node;
79 variables: Variable[];
80 set: Map<string, Variable>;
81 references: Reference[];
82 through: Reference[];
83 functionExpressionScope: boolean;
84 }
85
86 interface Variable {
87 name: string;
88 scope: Scope;
89 identifiers: ESTree.Identifier[];
90 references: Reference[];
91 defs: Definition[];
92 }
93
94 interface Reference {
95 identifier: ESTree.Identifier;
96 from: Scope;
97 resolved: Variable | null;
98 writeExpr: ESTree.Node | null;
99 init: boolean;
100
101 isWrite(): boolean;
102
103 isRead(): boolean;
104
105 isWriteOnly(): boolean;
106
107 isReadOnly(): boolean;
108
109 isReadWrite(): boolean;
110 }
111
112 type DefinitionType =
113 | { type: "CatchClause"; node: ESTree.CatchClause; parent: null }
114 | { type: "ClassName"; node: ESTree.ClassDeclaration | ESTree.ClassExpression; parent: null }
115 | { type: "FunctionName"; node: ESTree.FunctionDeclaration | ESTree.FunctionExpression; parent: null }
116 | { type: "ImplicitGlobalVariable"; node: ESTree.Program; parent: null }
117 | {
118 type: "ImportBinding";
119 node: ESTree.ImportSpecifier | ESTree.ImportDefaultSpecifier | ESTree.ImportNamespaceSpecifier;
120 parent: ESTree.ImportDeclaration;
121 }
122 | {
123 type: "Parameter";
124 node: ESTree.FunctionDeclaration | ESTree.FunctionExpression | ESTree.ArrowFunctionExpression;
125 parent: null;
126 }
127 | { type: "TDZ"; node: any; parent: null }
128 | { type: "Variable"; node: ESTree.VariableDeclarator; parent: ESTree.VariableDeclaration };
129
130 type Definition = DefinitionType & { name: ESTree.Identifier };
131}
132
133//#region SourceCode
134
135export class SourceCode {
136 text: string;
137 ast: AST.Program;
138 lines: string[];
139 hasBOM: boolean;
140 parserServices: SourceCode.ParserServices;
141 scopeManager: Scope.ScopeManager;
142 visitorKeys: SourceCode.VisitorKeys;
143
144 constructor(text: string, ast: AST.Program);
145 constructor(config: SourceCode.Config);
146
147 static splitLines(text: string): string[];
148
149 getText(node?: ESTree.Node, beforeCount?: number, afterCount?: number): string;
150
151 getLines(): string[];
152
153 getAllComments(): ESTree.Comment[];
154
155 getComments(node: ESTree.Node): { leading: ESTree.Comment[]; trailing: ESTree.Comment[] };
156
157 getJSDocComment(node: ESTree.Node): ESTree.Comment | null;
158
159 getNodeByRangeIndex(index: number): ESTree.Node | null;
160
161 isSpaceBetweenTokens(first: AST.Token, second: AST.Token): boolean;
162
163 getLocFromIndex(index: number): ESTree.Position;
164
165 getIndexFromLoc(location: ESTree.Position): number;
166
167 // Inherited methods from TokenStore
168 // ---------------------------------
169
170 getTokenByRangeStart(offset: number, options?: { includeComments: false }): AST.Token | null;
171 getTokenByRangeStart(offset: number, options: { includeComments: boolean }): AST.Token | ESTree.Comment | null;
172
173 getFirstToken: SourceCode.UnaryNodeCursorWithSkipOptions;
174
175 getFirstTokens: SourceCode.UnaryNodeCursorWithCountOptions;
176
177 getLastToken: SourceCode.UnaryNodeCursorWithSkipOptions;
178
179 getLastTokens: SourceCode.UnaryNodeCursorWithCountOptions;
180
181 getTokenBefore: SourceCode.UnaryCursorWithSkipOptions;
182
183 getTokensBefore: SourceCode.UnaryCursorWithCountOptions;
184
185 getTokenAfter: SourceCode.UnaryCursorWithSkipOptions;
186
187 getTokensAfter: SourceCode.UnaryCursorWithCountOptions;
188
189 getFirstTokenBetween: SourceCode.BinaryCursorWithSkipOptions;
190
191 getFirstTokensBetween: SourceCode.BinaryCursorWithCountOptions;
192
193 getLastTokenBetween: SourceCode.BinaryCursorWithSkipOptions;
194
195 getLastTokensBetween: SourceCode.BinaryCursorWithCountOptions;
196
197 getTokensBetween: SourceCode.BinaryCursorWithCountOptions;
198
199 getTokens: ((node: ESTree.Node, beforeCount?: number, afterCount?: number) => AST.Token[]) &
200 SourceCode.UnaryNodeCursorWithCountOptions;
201
202 commentsExistBetween(
203 left: ESTree.Node | AST.Token | ESTree.Comment,
204 right: ESTree.Node | AST.Token | ESTree.Comment,
205 ): boolean;
206
207 getCommentsBefore(nodeOrToken: ESTree.Node | AST.Token): ESTree.Comment[];
208
209 getCommentsAfter(nodeOrToken: ESTree.Node | AST.Token): ESTree.Comment[];
210
211 getCommentsInside(node: ESTree.Node): ESTree.Comment[];
212}
213
214export namespace SourceCode {
215 interface Config {
216 text: string;
217 ast: AST.Program;
218 parserServices?: ParserServices | undefined;
219 scopeManager?: Scope.ScopeManager | undefined;
220 visitorKeys?: VisitorKeys | undefined;
221 }
222
223 type ParserServices = any;
224
225 interface VisitorKeys {
226 [nodeType: string]: string[];
227 }
228
229 interface UnaryNodeCursorWithSkipOptions {
230 <T extends AST.Token>(
231 node: ESTree.Node,
232 options:
233 | ((token: AST.Token) => token is T)
234 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; skip?: number | undefined },
235 ): T | null;
236 <T extends AST.Token | ESTree.Comment>(
237 node: ESTree.Node,
238 options: {
239 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
240 includeComments: boolean;
241 skip?: number | undefined;
242 },
243 ): T | null;
244 (
245 node: ESTree.Node,
246 options?:
247 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; skip?: number | undefined }
248 | ((token: AST.Token) => boolean)
249 | number,
250 ): AST.Token | null;
251 (
252 node: ESTree.Node,
253 options: {
254 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
255 includeComments: boolean;
256 skip?: number | undefined;
257 },
258 ): AST.Token | ESTree.Comment | null;
259 }
260
261 interface UnaryNodeCursorWithCountOptions {
262 <T extends AST.Token>(
263 node: ESTree.Node,
264 options:
265 | ((token: AST.Token) => token is T)
266 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; count?: number | undefined },
267 ): T[];
268 <T extends AST.Token | ESTree.Comment>(
269 node: ESTree.Node,
270 options: {
271 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
272 includeComments: boolean;
273 count?: number | undefined;
274 },
275 ): T[];
276 (
277 node: ESTree.Node,
278 options?:
279 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; count?: number | undefined }
280 | ((token: AST.Token) => boolean)
281 | number,
282 ): AST.Token[];
283 (
284 node: ESTree.Node,
285 options: {
286 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
287 includeComments: boolean;
288 count?: number | undefined;
289 },
290 ): Array<AST.Token | ESTree.Comment>;
291 }
292
293 interface UnaryCursorWithSkipOptions {
294 <T extends AST.Token>(
295 node: ESTree.Node | AST.Token | ESTree.Comment,
296 options:
297 | ((token: AST.Token) => token is T)
298 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; skip?: number | undefined },
299 ): T | null;
300 <T extends AST.Token | ESTree.Comment>(
301 node: ESTree.Node | AST.Token | ESTree.Comment,
302 options: {
303 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
304 includeComments: boolean;
305 skip?: number | undefined;
306 },
307 ): T | null;
308 (
309 node: ESTree.Node | AST.Token | ESTree.Comment,
310 options?:
311 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; skip?: number | undefined }
312 | ((token: AST.Token) => boolean)
313 | number,
314 ): AST.Token | null;
315 (
316 node: ESTree.Node | AST.Token | ESTree.Comment,
317 options: {
318 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
319 includeComments: boolean;
320 skip?: number | undefined;
321 },
322 ): AST.Token | ESTree.Comment | null;
323 }
324
325 interface UnaryCursorWithCountOptions {
326 <T extends AST.Token>(
327 node: ESTree.Node | AST.Token | ESTree.Comment,
328 options:
329 | ((token: AST.Token) => token is T)
330 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; count?: number | undefined },
331 ): T[];
332 <T extends AST.Token | ESTree.Comment>(
333 node: ESTree.Node | AST.Token | ESTree.Comment,
334 options: {
335 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
336 includeComments: boolean;
337 count?: number | undefined;
338 },
339 ): T[];
340 (
341 node: ESTree.Node | AST.Token | ESTree.Comment,
342 options?:
343 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; count?: number | undefined }
344 | ((token: AST.Token) => boolean)
345 | number,
346 ): AST.Token[];
347 (
348 node: ESTree.Node | AST.Token | ESTree.Comment,
349 options: {
350 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
351 includeComments: boolean;
352 count?: number | undefined;
353 },
354 ): Array<AST.Token | ESTree.Comment>;
355 }
356
357 interface BinaryCursorWithSkipOptions {
358 <T extends AST.Token>(
359 left: ESTree.Node | AST.Token | ESTree.Comment,
360 right: ESTree.Node | AST.Token | ESTree.Comment,
361 options:
362 | ((token: AST.Token) => token is T)
363 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; skip?: number | undefined },
364 ): T | null;
365 <T extends AST.Token | ESTree.Comment>(
366 left: ESTree.Node | AST.Token | ESTree.Comment,
367 right: ESTree.Node | AST.Token | ESTree.Comment,
368 options: {
369 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
370 includeComments: boolean;
371 skip?: number | undefined;
372 },
373 ): T | null;
374 (
375 left: ESTree.Node | AST.Token | ESTree.Comment,
376 right: ESTree.Node | AST.Token | ESTree.Comment,
377 options?:
378 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; skip?: number | undefined }
379 | ((token: AST.Token) => boolean)
380 | number,
381 ): AST.Token | null;
382 (
383 left: ESTree.Node | AST.Token | ESTree.Comment,
384 right: ESTree.Node | AST.Token | ESTree.Comment,
385 options: {
386 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
387 includeComments: boolean;
388 skip?: number | undefined;
389 },
390 ): AST.Token | ESTree.Comment | null;
391 }
392
393 interface BinaryCursorWithCountOptions {
394 <T extends AST.Token>(
395 left: ESTree.Node | AST.Token | ESTree.Comment,
396 right: ESTree.Node | AST.Token | ESTree.Comment,
397 options:
398 | ((token: AST.Token) => token is T)
399 | { filter: (token: AST.Token) => token is T; includeComments?: false | undefined; count?: number | undefined },
400 ): T[];
401 <T extends AST.Token | ESTree.Comment>(
402 left: ESTree.Node | AST.Token | ESTree.Comment,
403 right: ESTree.Node | AST.Token | ESTree.Comment,
404 options: {
405 filter: (tokenOrComment: AST.Token | ESTree.Comment) => tokenOrComment is T;
406 includeComments: boolean;
407 count?: number | undefined;
408 },
409 ): T[];
410 (
411 left: ESTree.Node | AST.Token | ESTree.Comment,
412 right: ESTree.Node | AST.Token | ESTree.Comment,
413 options?:
414 | { filter?: ((token: AST.Token) => boolean) | undefined; includeComments?: false | undefined; count?: number | undefined }
415 | ((token: AST.Token) => boolean)
416 | number,
417 ): AST.Token[];
418 (
419 left: ESTree.Node | AST.Token | ESTree.Comment,
420 right: ESTree.Node | AST.Token | ESTree.Comment,
421 options: {
422 filter?: ((token: AST.Token | ESTree.Comment) => boolean) | undefined;
423 includeComments: boolean;
424 count?: number | undefined;
425 },
426 ): Array<AST.Token | ESTree.Comment>;
427 }
428}
429
430//#endregion
431
432export namespace Rule {
433 interface RuleModule {
434 create(context: RuleContext): RuleListener;
435 meta?: RuleMetaData | undefined;
436 }
437
438 type NodeTypes = ESTree.Node["type"];
439 interface NodeListener {
440 ArrayExpression?: ((node: ESTree.ArrayExpression & NodeParentExtension) => void) | undefined;
441 ArrayPattern?: ((node: ESTree.ArrayPattern & NodeParentExtension) => void) | undefined;
442 ArrowFunctionExpression?: ((node: ESTree.ArrowFunctionExpression & NodeParentExtension) => void) | undefined;
443 AssignmentExpression?: ((node: ESTree.AssignmentExpression & NodeParentExtension) => void) | undefined;
444 AssignmentPattern?: ((node: ESTree.AssignmentPattern & NodeParentExtension) => void) | undefined;
445 AwaitExpression?: ((node: ESTree.AwaitExpression & NodeParentExtension) => void) | undefined;
446 BinaryExpression?: ((node: ESTree.BinaryExpression & NodeParentExtension) => void) | undefined;
447 BlockStatement?: ((node: ESTree.BlockStatement & NodeParentExtension) => void) | undefined;
448 BreakStatement?: ((node: ESTree.BreakStatement & NodeParentExtension) => void) | undefined;
449 CallExpression?: ((node: ESTree.CallExpression & NodeParentExtension) => void) | undefined;
450 CatchClause?: ((node: ESTree.CatchClause & NodeParentExtension) => void) | undefined;
451 ChainExpression?: ((node: ESTree.ChainExpression & NodeParentExtension) => void) | undefined;
452 ClassBody?: ((node: ESTree.ClassBody & NodeParentExtension) => void) | undefined;
453 ClassDeclaration?: ((node: ESTree.ClassDeclaration & NodeParentExtension) => void) | undefined;
454 ClassExpression?: ((node: ESTree.ClassExpression & NodeParentExtension) => void) | undefined;
455 ConditionalExpression?: ((node: ESTree.ConditionalExpression & NodeParentExtension) => void) | undefined;
456 ContinueStatement?: ((node: ESTree.ContinueStatement & NodeParentExtension) => void) | undefined;
457 DebuggerStatement?: ((node: ESTree.DebuggerStatement & NodeParentExtension) => void) | undefined;
458 DoWhileStatement?: ((node: ESTree.DoWhileStatement & NodeParentExtension) => void) | undefined;
459 EmptyStatement?: ((node: ESTree.EmptyStatement & NodeParentExtension) => void) | undefined;
460 ExportAllDeclaration?: ((node: ESTree.ExportAllDeclaration & NodeParentExtension) => void) | undefined;
461 ExportDefaultDeclaration?: ((node: ESTree.ExportDefaultDeclaration & NodeParentExtension) => void) | undefined;
462 ExportNamedDeclaration?: ((node: ESTree.ExportNamedDeclaration & NodeParentExtension) => void) | undefined;
463 ExportSpecifier?: ((node: ESTree.ExportSpecifier & NodeParentExtension) => void) | undefined;
464 ExpressionStatement?: ((node: ESTree.ExpressionStatement & NodeParentExtension) => void) | undefined;
465 ForInStatement?: ((node: ESTree.ForInStatement & NodeParentExtension) => void) | undefined;
466 ForOfStatement?: ((node: ESTree.ForOfStatement & NodeParentExtension) => void) | undefined;
467 ForStatement?: ((node: ESTree.ForStatement & NodeParentExtension) => void) | undefined;
468 FunctionDeclaration?: ((node: ESTree.FunctionDeclaration & NodeParentExtension) => void) | undefined;
469 FunctionExpression?: ((node: ESTree.FunctionExpression & NodeParentExtension) => void) | undefined;
470 Identifier?: ((node: ESTree.Identifier & NodeParentExtension) => void) | undefined;
471 IfStatement?: ((node: ESTree.IfStatement & NodeParentExtension) => void) | undefined;
472 ImportDeclaration?: ((node: ESTree.ImportDeclaration & NodeParentExtension) => void) | undefined;
473 ImportDefaultSpecifier?: ((node: ESTree.ImportDefaultSpecifier & NodeParentExtension) => void) | undefined;
474 ImportExpression?: ((node: ESTree.ImportExpression & NodeParentExtension) => void) | undefined;
475 ImportNamespaceSpecifier?: ((node: ESTree.ImportNamespaceSpecifier & NodeParentExtension) => void) | undefined;
476 ImportSpecifier?: ((node: ESTree.ImportSpecifier & NodeParentExtension) => void) | undefined;
477 LabeledStatement?: ((node: ESTree.LabeledStatement & NodeParentExtension) => void) | undefined;
478 Literal?: ((node: ESTree.Literal & NodeParentExtension) => void) | undefined;
479 LogicalExpression?: ((node: ESTree.LogicalExpression & NodeParentExtension) => void) | undefined;
480 MemberExpression?: ((node: ESTree.MemberExpression & NodeParentExtension) => void) | undefined;
481 MetaProperty?: ((node: ESTree.MetaProperty & NodeParentExtension) => void) | undefined;
482 MethodDefinition?: ((node: ESTree.MethodDefinition & NodeParentExtension) => void) | undefined;
483 NewExpression?: ((node: ESTree.NewExpression & NodeParentExtension) => void) | undefined;
484 ObjectExpression?: ((node: ESTree.ObjectExpression & NodeParentExtension) => void) | undefined;
485 ObjectPattern?: ((node: ESTree.ObjectPattern & NodeParentExtension) => void) | undefined;
486 Program?: ((node: ESTree.Program) => void) | undefined;
487 Property?: ((node: ESTree.Property & NodeParentExtension) => void) | undefined;
488 RestElement?: ((node: ESTree.RestElement & NodeParentExtension) => void) | undefined;
489 ReturnStatement?: ((node: ESTree.ReturnStatement & NodeParentExtension) => void) | undefined;
490 SequenceExpression?: ((node: ESTree.SequenceExpression & NodeParentExtension) => void) | undefined;
491 SpreadElement?: ((node: ESTree.SpreadElement & NodeParentExtension) => void) | undefined;
492 Super?: ((node: ESTree.Super & NodeParentExtension) => void) | undefined;
493 SwitchCase?: ((node: ESTree.SwitchCase & NodeParentExtension) => void) | undefined;
494 SwitchStatement?: ((node: ESTree.SwitchStatement & NodeParentExtension) => void) | undefined;
495 TaggedTemplateExpression?: ((node: ESTree.TaggedTemplateExpression & NodeParentExtension) => void) | undefined;
496 TemplateElement?: ((node: ESTree.TemplateElement & NodeParentExtension) => void) | undefined;
497 TemplateLiteral?: ((node: ESTree.TemplateLiteral & NodeParentExtension) => void) | undefined;
498 ThisExpression?: ((node: ESTree.ThisExpression & NodeParentExtension) => void) | undefined;
499 ThrowStatement?: ((node: ESTree.ThrowStatement & NodeParentExtension) => void) | undefined;
500 TryStatement?: ((node: ESTree.TryStatement & NodeParentExtension) => void) | undefined;
501 UnaryExpression?: ((node: ESTree.UnaryExpression & NodeParentExtension) => void) | undefined;
502 UpdateExpression?: ((node: ESTree.UpdateExpression & NodeParentExtension) => void) | undefined;
503 VariableDeclaration?: ((node: ESTree.VariableDeclaration & NodeParentExtension) => void) | undefined;
504 VariableDeclarator?: ((node: ESTree.VariableDeclarator & NodeParentExtension) => void) | undefined;
505 WhileStatement?: ((node: ESTree.WhileStatement & NodeParentExtension) => void) | undefined;
506 WithStatement?: ((node: ESTree.WithStatement & NodeParentExtension) => void) | undefined;
507 YieldExpression?: ((node: ESTree.YieldExpression & NodeParentExtension) => void) | undefined;
508 }
509
510 interface NodeParentExtension {
511 parent: Node;
512 }
513 type Node = ESTree.Node & NodeParentExtension;
514
515 interface RuleListener extends NodeListener {
516 onCodePathStart?(codePath: CodePath, node: Node): void;
517
518 onCodePathEnd?(codePath: CodePath, node: Node): void;
519
520 onCodePathSegmentStart?(segment: CodePathSegment, node: Node): void;
521
522 onCodePathSegmentEnd?(segment: CodePathSegment, node: Node): void;
523
524 onCodePathSegmentLoop?(fromSegment: CodePathSegment, toSegment: CodePathSegment, node: Node): void;
525
526 [key: string]:
527 | ((codePath: CodePath, node: Node) => void)
528 | ((segment: CodePathSegment, node: Node) => void)
529 | ((fromSegment: CodePathSegment, toSegment: CodePathSegment, node: Node) => void)
530 | ((node: Node) => void)
531 | NodeListener[keyof NodeListener]
532 | undefined;
533 }
534
535 interface CodePath {
536 id: string;
537 initialSegment: CodePathSegment;
538 finalSegments: CodePathSegment[];
539 returnedSegments: CodePathSegment[];
540 thrownSegments: CodePathSegment[];
541 currentSegments: CodePathSegment[];
542 upper: CodePath | null;
543 childCodePaths: CodePath[];
544 }
545
546 interface CodePathSegment {
547 id: string;
548 nextSegments: CodePathSegment[];
549 prevSegments: CodePathSegment[];
550 reachable: boolean;
551 }
552
553 interface RuleMetaData {
554 docs?: {
555 /** Provides a short description of the rule. */
556 description?: string | undefined;
557 /**
558 * TODO: remove this field in next major release of @types/eslint.
559 * @deprecated no longer used
560 */
561 category?: string | undefined;
562 /** Whether the rule is enabled in the plugin's `recommended` configuration. */
563 recommended?: boolean | undefined;
564 /** Specifies the URL at which the full documentation can be accessed (enabling code editors to provide a helpful link on highlighted rule violations). */
565 url?: string | undefined;
566 /**
567 * TODO: remove this field in next major release of @types/eslint.
568 * @deprecated use `meta.hasSuggestions` instead
569 */
570 suggestion?: boolean | undefined;
571 } | undefined;
572 /** Violation and suggestion messages. */
573 messages?: { [messageId: string]: string } | undefined;
574 /**
575 * Specifies if the `--fix` option on the command line automatically fixes problems reported by the rule.
576 * Mandatory for fixable rules.
577 */
578 fixable?: "code" | "whitespace" | undefined;
579 /**
580 * Specifies the [options](https://eslint.org/docs/latest/developer-guide/working-with-rules#options-schemas)
581 * so ESLint can prevent invalid [rule configurations](https://eslint.org/docs/latest/user-guide/configuring/rules#configuring-rules).
582 */
583 schema?: JSONSchema4 | JSONSchema4[] | undefined;
584 /** Indicates whether the rule has been deprecated. Omit if not deprecated. */
585 deprecated?: boolean | undefined;
586 /**
587 * Indicates the type of rule:
588 * - `"problem"` means the rule is identifying code that either will cause an error or may cause a confusing behavior. Developers should consider this a high priority to resolve.
589 * - `"suggestion"` means the rule is identifying something that could be done in a better way but no errors will occur if the code isn’t changed.
590 * - `"layout"` means the rule cares primarily about whitespace, semicolons, commas, and parentheses,
591 * all the parts of the program that determine how the code looks rather than how it executes.
592 * These rules work on parts of the code that aren’t specified in the AST.
593 */
594 type?: "problem" | "suggestion" | "layout" | undefined;
595 /**
596 * Specifies whether the rule can return suggestions (defaults to `false` if omitted).
597 * Mandatory for rules that provide suggestions.
598 */
599 hasSuggestions?: boolean | undefined;
600 }
601
602 interface RuleContext {
603 id: string;
604 options: any[];
605 settings: { [name: string]: any };
606 parserPath: string;
607 parserOptions: Linter.ParserOptions;
608 parserServices: SourceCode.ParserServices;
609
610 getAncestors(): ESTree.Node[];
611
612 getDeclaredVariables(node: ESTree.Node): Scope.Variable[];
613
614 getFilename(): string;
615
616 getPhysicalFilename(): string;
617
618 getCwd(): string;
619
620 getScope(): Scope.Scope;
621
622 getSourceCode(): SourceCode;
623
624 markVariableAsUsed(name: string): boolean;
625
626 report(descriptor: ReportDescriptor): void;
627 }
628
629 type ReportFixer = (fixer: RuleFixer) => null | Fix | IterableIterator<Fix> | Fix[];
630
631 interface ReportDescriptorOptionsBase {
632 data?: { [key: string]: string };
633
634 fix?: null | ReportFixer;
635 }
636
637 interface SuggestionReportOptions {
638 data?: { [key: string]: string };
639
640 fix: ReportFixer;
641 }
642
643 type SuggestionDescriptorMessage = { desc: string } | { messageId: string };
644 type SuggestionReportDescriptor = SuggestionDescriptorMessage & SuggestionReportOptions;
645
646 interface ReportDescriptorOptions extends ReportDescriptorOptionsBase {
647 suggest?: SuggestionReportDescriptor[] | null | undefined;
648 }
649
650 type ReportDescriptor = ReportDescriptorMessage & ReportDescriptorLocation & ReportDescriptorOptions;
651 type ReportDescriptorMessage = { message: string } | { messageId: string };
652 type ReportDescriptorLocation =
653 | { node: ESTree.Node }
654 | { loc: AST.SourceLocation | { line: number; column: number } };
655
656 interface RuleFixer {
657 insertTextAfter(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
658
659 insertTextAfterRange(range: AST.Range, text: string): Fix;
660
661 insertTextBefore(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
662
663 insertTextBeforeRange(range: AST.Range, text: string): Fix;
664
665 remove(nodeOrToken: ESTree.Node | AST.Token): Fix;
666
667 removeRange(range: AST.Range): Fix;
668
669 replaceText(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
670
671 replaceTextRange(range: AST.Range, text: string): Fix;
672 }
673
674 interface Fix {
675 range: AST.Range;
676 text: string;
677 }
678}
679
680//#region Linter
681
682export class Linter {
683 static version: string;
684
685 version: string;
686
687 constructor(options?: { cwd?: string | undefined });
688
689 verify(code: SourceCode | string, config: Linter.Config, filename?: string): Linter.LintMessage[];
690 verify(code: SourceCode | string, config: Linter.Config, options: Linter.LintOptions): Linter.LintMessage[];
691
692 verifyAndFix(code: string, config: Linter.Config, filename?: string): Linter.FixReport;
693 verifyAndFix(code: string, config: Linter.Config, options: Linter.FixOptions): Linter.FixReport;
694
695 getSourceCode(): SourceCode;
696
697 defineRule(name: string, rule: Rule.RuleModule): void;
698
699 defineRules(rules: { [name: string]: Rule.RuleModule }): void;
700
701 getRules(): Map<string, Rule.RuleModule>;
702
703 defineParser(name: string, parser: Linter.ParserModule): void;
704}
705
706export namespace Linter {
707 type Severity = 0 | 1 | 2;
708
709 type RuleLevel = Severity | "off" | "warn" | "error";
710 type RuleLevelAndOptions<Options extends any[] = any[]> = Prepend<Partial<Options>, RuleLevel>;
711
712 type RuleEntry<Options extends any[] = any[]> = RuleLevel | RuleLevelAndOptions<Options>;
713
714 interface RulesRecord {
715 [rule: string]: RuleEntry;
716 }
717
718 interface HasRules<Rules extends RulesRecord = RulesRecord> {
719 rules?: Partial<Rules> | undefined;
720 }
721
722 interface BaseConfig<Rules extends RulesRecord = RulesRecord> extends HasRules<Rules> {
723 $schema?: string | undefined;
724 env?: { [name: string]: boolean } | undefined;
725 extends?: string | string[] | undefined;
726 globals?: { [name: string]: boolean | "readonly" | "readable" | "writable" | "writeable" } | undefined;
727 noInlineConfig?: boolean | undefined;
728 overrides?: ConfigOverride[] | undefined;
729 parser?: string | undefined;
730 parserOptions?: ParserOptions | undefined;
731 plugins?: string[] | undefined;
732 processor?: string | undefined;
733 reportUnusedDisableDirectives?: boolean | undefined;
734 settings?: { [name: string]: any } | undefined;
735 }
736
737 interface ConfigOverride<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
738 excludedFiles?: string | string[] | undefined;
739 files: string | string[];
740 }
741
742 // https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js
743 interface Config<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
744 ignorePatterns?: string | string[] | undefined;
745 root?: boolean | undefined;
746 }
747
748 interface ParserOptions {
749 ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | "latest" | undefined;
750 sourceType?: "script" | "module" | undefined;
751 ecmaFeatures?: {
752 globalReturn?: boolean | undefined;
753 impliedStrict?: boolean | undefined;
754 jsx?: boolean | undefined;
755 experimentalObjectRestSpread?: boolean | undefined;
756 [key: string]: any;
757 } | undefined;
758 [key: string]: any;
759 }
760
761 interface LintOptions {
762 filename?: string | undefined;
763 preprocess?: ((code: string) => string[]) | undefined;
764 postprocess?: ((problemLists: LintMessage[][]) => LintMessage[]) | undefined;
765 filterCodeBlock?: boolean | undefined;
766 disableFixes?: boolean | undefined;
767 allowInlineConfig?: boolean | undefined;
768 reportUnusedDisableDirectives?: boolean | undefined;
769 }
770
771 interface LintSuggestion {
772 desc: string;
773 fix: Rule.Fix;
774 messageId?: string | undefined;
775 }
776
777 interface LintMessage {
778 column: number;
779 line: number;
780 endColumn?: number | undefined;
781 endLine?: number | undefined;
782 ruleId: string | null;
783 message: string;
784 messageId?: string | undefined;
785 nodeType?: string | undefined;
786 fatal?: true | undefined;
787 severity: Severity;
788 fix?: Rule.Fix | undefined;
789 /** @deprecated Use `linter.getSourceCode()` */
790 source?: string | null | undefined;
791 suggestions?: LintSuggestion[] | undefined;
792 }
793
794 interface LintSuppression {
795 kind: string;
796 justification: string;
797 }
798
799 interface SuppressedLintMessage extends LintMessage {
800 suppressions: LintSuppression[];
801 }
802
803 interface FixOptions extends LintOptions {
804 fix?: boolean | undefined;
805 }
806
807 interface FixReport {
808 fixed: boolean;
809 output: string;
810 messages: LintMessage[];
811 }
812
813 type ParserModule =
814 | {
815 parse(text: string, options?: any): AST.Program;
816 }
817 | {
818 parseForESLint(text: string, options?: any): ESLintParseResult;
819 };
820
821 interface ESLintParseResult {
822 ast: AST.Program;
823 parserServices?: SourceCode.ParserServices | undefined;
824 scopeManager?: Scope.ScopeManager | undefined;
825 visitorKeys?: SourceCode.VisitorKeys | undefined;
826 }
827
828 interface ProcessorFile {
829 text: string;
830 filename: string;
831 }
832
833 // https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins
834 interface Processor<T extends string | ProcessorFile = string | ProcessorFile> {
835 supportsAutofix?: boolean | undefined;
836 preprocess?(text: string, filename: string): T[];
837 postprocess?(messages: LintMessage[][], filename: string): LintMessage[];
838 }
839}
840
841//#endregion
842
843//#region ESLint
844
845export class ESLint {
846 static version: string;
847
848 static outputFixes(results: ESLint.LintResult[]): Promise<void>;
849
850 static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[];
851
852 constructor(options?: ESLint.Options);
853
854 lintFiles(patterns: string | string[]): Promise<ESLint.LintResult[]>;
855
856 lintText(code: string, options?: { filePath?: string | undefined; warnIgnored?: boolean | undefined }): Promise<ESLint.LintResult[]>;
857
858 getRulesMetaForResults(results: ESLint.LintResult[]): ESLint.LintResultData['rulesMeta'];
859
860 calculateConfigForFile(filePath: string): Promise<any>;
861
862 isPathIgnored(filePath: string): Promise<boolean>;
863
864 loadFormatter(nameOrPath?: string): Promise<ESLint.Formatter>;
865}
866
867export namespace ESLint {
868 type ConfigData<Rules extends Linter.RulesRecord = Linter.RulesRecord> = Omit<Linter.Config<Rules>, "$schema">;
869
870 interface Environment {
871 globals?: { [name: string]: boolean; } | undefined;
872 parserOptions?: Linter.ParserOptions | undefined;
873 }
874
875 interface Plugin {
876 configs?: Record<string, ConfigData> | undefined;
877 environments?: Record<string, Environment> | undefined;
878 processors?: Record<string, Linter.Processor> | undefined;
879 rules?: Record<string, ((...args: any[]) => any) | Rule.RuleModule> | undefined;
880 }
881
882 interface Options {
883 // File enumeration
884 cwd?: string | undefined;
885 errorOnUnmatchedPattern?: boolean | undefined;
886 extensions?: string[] | undefined;
887 globInputPaths?: boolean | undefined;
888 ignore?: boolean | undefined;
889 ignorePath?: string | undefined;
890
891 // Linting
892 allowInlineConfig?: boolean | undefined;
893 baseConfig?: Linter.Config | undefined;
894 overrideConfig?: Linter.Config | undefined;
895 overrideConfigFile?: string | undefined;
896 plugins?: Record<string, Plugin> | undefined;
897 reportUnusedDisableDirectives?: Linter.RuleLevel | undefined;
898 resolvePluginsRelativeTo?: string | undefined;
899 rulePaths?: string[] | undefined;
900 useEslintrc?: boolean | undefined;
901
902 // Autofix
903 fix?: boolean | ((message: Linter.LintMessage) => boolean) | undefined;
904 fixTypes?: Array<Rule.RuleMetaData["type"]> | undefined;
905
906 // Cache-related
907 cache?: boolean | undefined;
908 cacheLocation?: string | undefined;
909 cacheStrategy?: "content" | "metadata" | undefined;
910 }
911
912 interface LintResult {
913 filePath: string;
914 messages: Linter.LintMessage[];
915 suppressedMessages: Linter.SuppressedLintMessage[];
916 errorCount: number;
917 fatalErrorCount: number;
918 warningCount: number;
919 fixableErrorCount: number;
920 fixableWarningCount: number;
921 output?: string | undefined;
922 source?: string | undefined;
923 usedDeprecatedRules: DeprecatedRuleUse[];
924 }
925
926 interface LintResultData {
927 cwd: string;
928 rulesMeta: {
929 [ruleId: string]: Rule.RuleMetaData;
930 };
931 }
932
933 interface DeprecatedRuleUse {
934 ruleId: string;
935 replacedBy: string[];
936 }
937
938 interface Formatter {
939 format(results: LintResult[], data?: LintResultData): string | Promise<string>;
940 }
941
942 // Docs reference the type by this name
943 type EditInfo = Rule.Fix;
944}
945
946//#endregion
947
948//#region RuleTester
949
950export class RuleTester {
951 constructor(config?: any);
952
953 run(
954 name: string,
955 rule: Rule.RuleModule,
956 tests: {
957 valid?: Array<string | RuleTester.ValidTestCase> | undefined;
958 invalid?: RuleTester.InvalidTestCase[] | undefined;
959 },
960 ): void;
961
962 static only(
963 item: string | RuleTester.ValidTestCase | RuleTester.InvalidTestCase,
964 ): RuleTester.ValidTestCase | RuleTester.InvalidTestCase;
965}
966
967export namespace RuleTester {
968 interface ValidTestCase {
969 name?: string;
970 code: string;
971 options?: any;
972 filename?: string | undefined;
973 only?: boolean;
974 parserOptions?: Linter.ParserOptions | undefined;
975 settings?: { [name: string]: any } | undefined;
976 parser?: string | undefined;
977 globals?: { [name: string]: boolean } | undefined;
978 }
979
980 interface SuggestionOutput {
981 messageId?: string | undefined;
982 desc?: string | undefined;
983 data?: Record<string, unknown> | undefined;
984 output: string;
985 }
986
987 interface InvalidTestCase extends ValidTestCase {
988 errors: number | Array<TestCaseError | string>;
989 output?: string | null | undefined;
990 }
991
992 interface TestCaseError {
993 message?: string | RegExp | undefined;
994 messageId?: string | undefined;
995 type?: string | undefined;
996 data?: any;
997 line?: number | undefined;
998 column?: number | undefined;
999 endLine?: number | undefined;
1000 endColumn?: number | undefined;
1001 suggestions?: SuggestionOutput[] | undefined;
1002 }
1003}
1004
1005//#endregion
1006
\No newline at end of file