UNPKG

35.9 kBTypeScriptView Raw
1// Type definitions for eslint 7.2
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/// <reference path="lib/rules/index.d.ts" />
13
14import { JSONSchema4 } from "json-schema";
15import * as ESTree from "estree";
16
17export namespace AST {
18 type TokenType =
19 | "Boolean"
20 | "Null"
21 | "Identifier"
22 | "Keyword"
23 | "Punctuator"
24 | "JSXIdentifier"
25 | "JSXText"
26 | "Numeric"
27 | "String"
28 | "RegularExpression";
29
30 interface Token {
31 type: TokenType;
32 value: string;
33 range: Range;
34 loc: SourceLocation;
35 }
36
37 interface SourceLocation {
38 start: ESTree.Position;
39 end: ESTree.Position;
40 }
41
42 type Range = [number, number];
43
44 interface Program extends ESTree.Program {
45 comments: ESTree.Comment[];
46 tokens: Token[];
47 loc: SourceLocation;
48 range: Range;
49 }
50}
51
52export namespace Scope {
53 interface ScopeManager {
54 scopes: Scope[];
55 globalScope: Scope | null;
56
57 acquire(node: ESTree.Node, inner?: boolean): Scope | null;
58
59 getDeclaredVariables(node: ESTree.Node): Variable[];
60 }
61
62 interface Scope {
63 type:
64 | "block"
65 | "catch"
66 | "class"
67 | "for"
68 | "function"
69 | "function-expression-name"
70 | "global"
71 | "module"
72 | "switch"
73 | "with"
74 | "TDZ";
75 isStrict: boolean;
76 upper: Scope | null;
77 childScopes: Scope[];
78 variableScope: Scope;
79 block: ESTree.Node;
80 variables: Variable[];
81 set: Map<string, Variable>;
82 references: Reference[];
83 through: Reference[];
84 functionExpressionScope: boolean;
85 }
86
87 interface Variable {
88 name: string;
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;
219 scopeManager?: Scope.ScopeManager;
220 visitorKeys?: VisitorKeys;
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; skip?: number },
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;
242 },
243 ): T | null;
244 (
245 node: ESTree.Node,
246 options?:
247 | { filter?: (token: AST.Token) => boolean; includeComments?: false; skip?: number }
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;
255 includeComments: boolean;
256 skip?: number;
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; count?: number },
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;
274 },
275 ): T[];
276 (
277 node: ESTree.Node,
278 options?:
279 | { filter?: (token: AST.Token) => boolean; includeComments?: false; count?: number }
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;
287 includeComments: boolean;
288 count?: number;
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; skip?: number },
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;
306 },
307 ): T | null;
308 (
309 node: ESTree.Node | AST.Token | ESTree.Comment,
310 options?:
311 | { filter?: (token: AST.Token) => boolean; includeComments?: false; skip?: number }
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;
319 includeComments: boolean;
320 skip?: number;
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; count?: number },
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;
338 },
339 ): T[];
340 (
341 node: ESTree.Node | AST.Token | ESTree.Comment,
342 options?:
343 | { filter?: (token: AST.Token) => boolean; includeComments?: false; count?: number }
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;
351 includeComments: boolean;
352 count?: number;
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; skip?: number },
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;
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; includeComments?: false; skip?: number }
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;
387 includeComments: boolean;
388 skip?: number;
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; count?: number },
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;
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; includeComments?: false; count?: number }
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;
423 includeComments: boolean;
424 count?: number;
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;
436 }
437
438 type NodeTypes = ESTree.Node["type"];
439 interface NodeListener {
440 ArrayExpression?: (node: ESTree.ArrayExpression & NodeParentExtension) => void;
441 ArrayPattern?: (node: ESTree.ArrayPattern & NodeParentExtension) => void;
442 ArrowFunctionExpression?: (node: ESTree.ArrowFunctionExpression & NodeParentExtension) => void;
443 AssignmentExpression?: (node: ESTree.AssignmentExpression & NodeParentExtension) => void;
444 AssignmentPattern?: (node: ESTree.AssignmentPattern & NodeParentExtension) => void;
445 AwaitExpression?: (node: ESTree.AwaitExpression & NodeParentExtension) => void;
446 BinaryExpression?: (node: ESTree.BinaryExpression & NodeParentExtension) => void;
447 BlockStatement?: (node: ESTree.BlockStatement & NodeParentExtension) => void;
448 BreakStatement?: (node: ESTree.BreakStatement & NodeParentExtension) => void;
449 CallExpression?: (node: ESTree.CallExpression & NodeParentExtension) => void;
450 CatchClause?: (node: ESTree.CatchClause & NodeParentExtension) => void;
451 ChainExpression?: (node: ESTree.ChainExpression & NodeParentExtension) => void;
452 ClassBody?: (node: ESTree.ClassBody & NodeParentExtension) => void;
453 ClassDeclaration?: (node: ESTree.ClassDeclaration & NodeParentExtension) => void;
454 ClassExpression?: (node: ESTree.ClassExpression & NodeParentExtension) => void;
455 ConditionalExpression?: (node: ESTree.ConditionalExpression & NodeParentExtension) => void;
456 ContinueStatement?: (node: ESTree.ContinueStatement & NodeParentExtension) => void;
457 DebuggerStatement?: (node: ESTree.DebuggerStatement & NodeParentExtension) => void;
458 DoWhileStatement?: (node: ESTree.DoWhileStatement & NodeParentExtension) => void;
459 EmptyStatement?: (node: ESTree.EmptyStatement & NodeParentExtension) => void;
460 ExportAllDeclaration?: (node: ESTree.ExportAllDeclaration & NodeParentExtension) => void;
461 ExportDefaultDeclaration?: (node: ESTree.ExportDefaultDeclaration & NodeParentExtension) => void;
462 ExportNamedDeclaration?: (node: ESTree.ExportNamedDeclaration & NodeParentExtension) => void;
463 ExportSpecifier?: (node: ESTree.ExportSpecifier & NodeParentExtension) => void;
464 ExpressionStatement?: (node: ESTree.ExpressionStatement & NodeParentExtension) => void;
465 ForInStatement?: (node: ESTree.ForInStatement & NodeParentExtension) => void;
466 ForOfStatement?: (node: ESTree.ForOfStatement & NodeParentExtension) => void;
467 ForStatement?: (node: ESTree.ForStatement & NodeParentExtension) => void;
468 FunctionDeclaration?: (node: ESTree.FunctionDeclaration & NodeParentExtension) => void;
469 FunctionExpression?: (node: ESTree.FunctionExpression & NodeParentExtension) => void;
470 Identifier?: (node: ESTree.Identifier & NodeParentExtension) => void;
471 IfStatement?: (node: ESTree.IfStatement & NodeParentExtension) => void;
472 ImportDeclaration?: (node: ESTree.ImportDeclaration & NodeParentExtension) => void;
473 ImportDefaultSpecifier?: (node: ESTree.ImportDefaultSpecifier & NodeParentExtension) => void;
474 ImportExpression?: (node: ESTree.ImportExpression & NodeParentExtension) => void;
475 ImportNamespaceSpecifier?: (node: ESTree.ImportNamespaceSpecifier & NodeParentExtension) => void;
476 ImportSpecifier?: (node: ESTree.ImportSpecifier & NodeParentExtension) => void;
477 LabeledStatement?: (node: ESTree.LabeledStatement & NodeParentExtension) => void;
478 Literal?: (node: ESTree.Literal & NodeParentExtension) => void;
479 LogicalExpression?: (node: ESTree.LogicalExpression & NodeParentExtension) => void;
480 MemberExpression?: (node: ESTree.MemberExpression & NodeParentExtension) => void;
481 MetaProperty?: (node: ESTree.MetaProperty & NodeParentExtension) => void;
482 MethodDefinition?: (node: ESTree.MethodDefinition & NodeParentExtension) => void;
483 NewExpression?: (node: ESTree.NewExpression & NodeParentExtension) => void;
484 ObjectExpression?: (node: ESTree.ObjectExpression & NodeParentExtension) => void;
485 ObjectPattern?: (node: ESTree.ObjectPattern & NodeParentExtension) => void;
486 Program?: (node: ESTree.Program) => void;
487 Property?: (node: ESTree.Property & NodeParentExtension) => void;
488 RestElement?: (node: ESTree.RestElement & NodeParentExtension) => void;
489 ReturnStatement?: (node: ESTree.ReturnStatement & NodeParentExtension) => void;
490 SequenceExpression?: (node: ESTree.SequenceExpression & NodeParentExtension) => void;
491 SpreadElement?: (node: ESTree.SpreadElement & NodeParentExtension) => void;
492 Super?: (node: ESTree.Super & NodeParentExtension) => void;
493 SwitchCase?: (node: ESTree.SwitchCase & NodeParentExtension) => void;
494 SwitchStatement?: (node: ESTree.SwitchStatement & NodeParentExtension) => void;
495 TaggedTemplateExpression?: (node: ESTree.TaggedTemplateExpression & NodeParentExtension) => void;
496 TemplateElement?: (node: ESTree.TemplateElement & NodeParentExtension) => void;
497 TemplateLiteral?: (node: ESTree.TemplateLiteral & NodeParentExtension) => void;
498 ThisExpression?: (node: ESTree.ThisExpression & NodeParentExtension) => void;
499 ThrowStatement?: (node: ESTree.ThrowStatement & NodeParentExtension) => void;
500 TryStatement?: (node: ESTree.TryStatement & NodeParentExtension) => void;
501 UnaryExpression?: (node: ESTree.UnaryExpression & NodeParentExtension) => void;
502 UpdateExpression?: (node: ESTree.UpdateExpression & NodeParentExtension) => void;
503 VariableDeclaration?: (node: ESTree.VariableDeclaration & NodeParentExtension) => void;
504 VariableDeclarator?: (node: ESTree.VariableDeclarator & NodeParentExtension) => void;
505 WhileStatement?: (node: ESTree.WhileStatement & NodeParentExtension) => void;
506 WithStatement?: (node: ESTree.WithStatement & NodeParentExtension) => void;
507 YieldExpression?: (node: ESTree.YieldExpression & NodeParentExtension) => void;
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 the short description of the rule in the [rules index](https://eslint.org/docs/rules/) */
556 description?: string;
557 /** specifies the heading under which the rule is listed in the [rules index](https://eslint.org/docs/rules/) */
558 category?: string;
559 /** is whether the `"extends": "eslint:recommended"` property in a [configuration file](https://eslint.org/docs/user-guide/configuring#extending-configuration-files) enables the rule */
560 recommended?: boolean;
561 /** specifies the URL at which the full documentation can be accessed */
562 url?: string;
563 /** specifies whether rules can return suggestions (defaults to false if omitted) */
564 suggestion?: boolean;
565 };
566 messages?: { [messageId: string]: string };
567 fixable?: "code" | "whitespace";
568 schema?: JSONSchema4 | JSONSchema4[];
569 deprecated?: boolean;
570 type?: "problem" | "suggestion" | "layout";
571 }
572
573 interface RuleContext {
574 id: string;
575 options: any[];
576 settings: { [name: string]: any };
577 parserPath: string;
578 parserOptions: Linter.ParserOptions;
579 parserServices: SourceCode.ParserServices;
580
581 getAncestors(): ESTree.Node[];
582
583 getDeclaredVariables(node: ESTree.Node): Scope.Variable[];
584
585 getFilename(): string;
586
587 getCwd(): string;
588
589 getScope(): Scope.Scope;
590
591 getSourceCode(): SourceCode;
592
593 markVariableAsUsed(name: string): boolean;
594
595 report(descriptor: ReportDescriptor): void;
596 }
597
598 interface ReportDescriptorOptionsBase {
599 data?: { [key: string]: string };
600
601 fix?: null | ((fixer: RuleFixer) => null | Fix | IterableIterator<Fix> | Fix[]);
602 }
603
604 type SuggestionDescriptorMessage = { desc: string } | { messageId: string };
605 type SuggestionReportDescriptor = SuggestionDescriptorMessage & ReportDescriptorOptionsBase;
606
607 interface ReportDescriptorOptions extends ReportDescriptorOptionsBase {
608 suggest?: SuggestionReportDescriptor[] | null;
609 }
610
611 type ReportDescriptor = ReportDescriptorMessage & ReportDescriptorLocation & ReportDescriptorOptions;
612 type ReportDescriptorMessage = { message: string } | { messageId: string };
613 type ReportDescriptorLocation =
614 | { node: ESTree.Node }
615 | { loc: AST.SourceLocation | { line: number; column: number } };
616
617 interface RuleFixer {
618 insertTextAfter(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
619
620 insertTextAfterRange(range: AST.Range, text: string): Fix;
621
622 insertTextBefore(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
623
624 insertTextBeforeRange(range: AST.Range, text: string): Fix;
625
626 remove(nodeOrToken: ESTree.Node | AST.Token): Fix;
627
628 removeRange(range: AST.Range): Fix;
629
630 replaceText(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
631
632 replaceTextRange(range: AST.Range, text: string): Fix;
633 }
634
635 interface Fix {
636 range: AST.Range;
637 text: string;
638 }
639}
640
641//#region Linter
642
643export class Linter {
644 static version: string;
645
646 version: string;
647
648 constructor(options?: { cwd?: string });
649
650 verify(code: SourceCode | string, config: Linter.Config, filename?: string): Linter.LintMessage[];
651 verify(code: SourceCode | string, config: Linter.Config, options: Linter.LintOptions): Linter.LintMessage[];
652
653 verifyAndFix(code: string, config: Linter.Config, filename?: string): Linter.FixReport;
654 verifyAndFix(code: string, config: Linter.Config, options: Linter.FixOptions): Linter.FixReport;
655
656 getSourceCode(): SourceCode;
657
658 defineRule(name: string, rule: Rule.RuleModule): void;
659
660 defineRules(rules: { [name: string]: Rule.RuleModule }): void;
661
662 getRules(): Map<string, Rule.RuleModule>;
663
664 defineParser(name: string, parser: Linter.ParserModule): void;
665}
666
667export namespace Linter {
668 type Severity = 0 | 1 | 2;
669
670 type RuleLevel = Severity | "off" | "warn" | "error";
671 type RuleLevelAndOptions<Options extends any[] = any[]> = Prepend<Partial<Options>, RuleLevel>;
672
673 type RuleEntry<Options extends any[] = any[]> = RuleLevel | RuleLevelAndOptions<Options>;
674
675 interface RulesRecord {
676 [rule: string]: RuleEntry;
677 }
678
679 interface HasRules<Rules extends RulesRecord = RulesRecord> {
680 rules?: Partial<Rules>;
681 }
682
683 interface BaseConfig<Rules extends RulesRecord = RulesRecord> extends HasRules<Rules> {
684 $schema?: string;
685 env?: { [name: string]: boolean };
686 extends?: string | string[];
687 globals?: { [name: string]: boolean | "readonly" | "readable" | "writable" | "writeable" };
688 noInlineConfig?: boolean;
689 overrides?: ConfigOverride[];
690 parser?: string;
691 parserOptions?: ParserOptions;
692 plugins?: string[];
693 processor?: string;
694 reportUnusedDisableDirectives?: boolean;
695 settings?: { [name: string]: any };
696 }
697
698 interface ConfigOverride<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
699 excludedFiles?: string | string[];
700 files: string | string[];
701 }
702
703 // https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js
704 interface Config<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
705 ignorePatterns?: string | string[];
706 root?: boolean;
707 }
708
709 interface ParserOptions {
710 ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021;
711 sourceType?: "script" | "module";
712 ecmaFeatures?: {
713 globalReturn?: boolean;
714 impliedStrict?: boolean;
715 jsx?: boolean;
716 experimentalObjectRestSpread?: boolean;
717 [key: string]: any;
718 };
719 [key: string]: any;
720 }
721
722 interface LintOptions {
723 filename?: string;
724 preprocess?: (code: string) => string[];
725 postprocess?: (problemLists: LintMessage[][]) => LintMessage[];
726 filterCodeBlock?: boolean;
727 disableFixes?: boolean;
728 allowInlineConfig?: boolean;
729 reportUnusedDisableDirectives?: boolean;
730 }
731
732 interface LintSuggestion {
733 desc: string;
734 fix: Rule.Fix;
735 messageId?: string;
736 }
737
738 interface LintMessage {
739 column: number;
740 line: number;
741 endColumn?: number;
742 endLine?: number;
743 ruleId: string | null;
744 message: string;
745 messageId?: string;
746 nodeType?: string;
747 fatal?: true;
748 severity: Severity;
749 fix?: Rule.Fix;
750 /** @deprecated Use `linter.getSourceCode()` */
751 source?: string | null;
752 suggestions?: LintSuggestion[];
753 }
754
755 interface FixOptions extends LintOptions {
756 fix?: boolean;
757 }
758
759 interface FixReport {
760 fixed: boolean;
761 output: string;
762 messages: LintMessage[];
763 }
764
765 type ParserModule =
766 | {
767 parse(text: string, options?: any): AST.Program;
768 }
769 | {
770 parseForESLint(text: string, options?: any): ESLintParseResult;
771 };
772
773 interface ESLintParseResult {
774 ast: AST.Program;
775 parserServices?: SourceCode.ParserServices;
776 scopeManager?: Scope.ScopeManager;
777 visitorKeys?: SourceCode.VisitorKeys;
778 }
779
780 interface ProcessorFile {
781 text: string;
782 filename: string;
783 }
784
785 // https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins
786 interface Processor<T extends string | ProcessorFile = string | ProcessorFile> {
787 supportsAutofix?: boolean;
788 preprocess?(text: string, filename: string): T[];
789 postprocess?(messages: LintMessage[][], filename: string): LintMessage[];
790 }
791}
792
793//#endregion
794
795//#region ESLint
796
797export class ESLint {
798 static version: string;
799
800 static outputFixes(results: ESLint.LintResult[]): Promise<void>;
801
802 static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[];
803
804 constructor(options?: ESLint.Options);
805
806 lintFiles(patterns: string | string[]): Promise<ESLint.LintResult[]>;
807
808 lintText(code: string, options?: { filePath?: string; warnIgnored?: boolean }): Promise<ESLint.LintResult[]>;
809
810 calculateConfigForFile(filePath: string): Promise<any>;
811
812 isPathIgnored(filePath: string): Promise<boolean>;
813
814 loadFormatter(nameOrPath?: string): Promise<ESLint.Formatter>;
815}
816
817export namespace ESLint {
818 interface Options {
819 // File enumeration
820 cwd?: string;
821 errorOnUnmatchedPattern?: boolean;
822 extensions?: string[];
823 globInputPaths?: boolean;
824 ignore?: boolean;
825 ignorePath?: string;
826
827 // Linting
828 allowInlineConfig?: boolean;
829 baseConfig?: Linter.Config;
830 overrideConfig?: Linter.Config;
831 overrideConfigFile?: string;
832 plugins?: Record<string, any>;
833 reportUnusedDisableDirectives?: Linter.RuleLevel;
834 resolvePluginsRelativeTo?: string;
835 rulePaths?: string[];
836 useEslintrc?: boolean;
837
838 // Autofix
839 fix?: boolean | ((message: Linter.LintMessage) => boolean);
840 fixTypes?: Array<Rule.RuleMetaData["type"]>;
841
842 // Cache-related
843 cache?: boolean;
844 cacheLocation?: string;
845 cacheStrategy?: "content" | "metadata";
846 }
847
848 interface LintResult {
849 filePath: string;
850 messages: Linter.LintMessage[];
851 errorCount: number;
852 warningCount: number;
853 fixableErrorCount: number;
854 fixableWarningCount: number;
855 output?: string;
856 source?: string;
857 usedDeprecatedRules: DeprecatedRuleUse[];
858 }
859
860 interface LintResultData {
861 rulesMeta: {
862 [ruleId: string]: Rule.RuleMetaData;
863 };
864 }
865
866 interface DeprecatedRuleUse {
867 ruleId: string;
868 replacedBy: string[];
869 }
870
871 interface Formatter {
872 format(results: LintResult[], data?: LintResultData): string;
873 }
874
875 // Docs reference the type by this name
876 type EditInfo = Rule.Fix;
877}
878
879//#endregion
880
881//#region CLIEngine
882
883/** @deprecated Deprecated in favor of `ESLint` */
884export class CLIEngine {
885 static version: string;
886
887 constructor(options: CLIEngine.Options);
888
889 executeOnFiles(patterns: string[]): CLIEngine.LintReport;
890
891 resolveFileGlobPatterns(patterns: string[]): string[];
892
893 getConfigForFile(filePath: string): Linter.Config;
894
895 executeOnText(text: string, filename?: string): CLIEngine.LintReport;
896
897 addPlugin(name: string, pluginObject: any): void;
898
899 isPathIgnored(filePath: string): boolean;
900
901 getFormatter(format?: string): CLIEngine.Formatter;
902
903 getRules(): Map<string, Rule.RuleModule>;
904
905 static getErrorResults(results: CLIEngine.LintResult[]): CLIEngine.LintResult[];
906
907 static getFormatter(format?: string): CLIEngine.Formatter;
908
909 static outputFixes(report: CLIEngine.LintReport): void;
910}
911
912/** @deprecated Deprecated in favor of `ESLint` */
913export namespace CLIEngine {
914 class Options {
915 allowInlineConfig?: boolean;
916 baseConfig?: false | { [name: string]: any };
917 cache?: boolean;
918 cacheFile?: string;
919 cacheLocation?: string;
920 cacheStrategy?: "content" | "metadata";
921 configFile?: string;
922 cwd?: string;
923 envs?: string[];
924 errorOnUnmatchedPattern?: boolean;
925 extensions?: string[];
926 fix?: boolean;
927 globals?: string[];
928 ignore?: boolean;
929 ignorePath?: string;
930 ignorePattern?: string | string[];
931 useEslintrc?: boolean;
932 parser?: string;
933 parserOptions?: Linter.ParserOptions;
934 plugins?: string[];
935 resolvePluginsRelativeTo?: string;
936 rules?: {
937 [name: string]: Linter.RuleLevel | Linter.RuleLevelAndOptions;
938 };
939 rulePaths?: string[];
940 reportUnusedDisableDirectives?: boolean;
941 }
942
943 type LintResult = ESLint.LintResult;
944
945 type LintResultData = ESLint.LintResultData;
946
947 interface LintReport {
948 results: LintResult[];
949 errorCount: number;
950 warningCount: number;
951 fixableErrorCount: number;
952 fixableWarningCount: number;
953 usedDeprecatedRules: DeprecatedRuleUse[];
954 }
955
956 type DeprecatedRuleUse = ESLint.DeprecatedRuleUse;
957
958 type Formatter = (results: LintResult[], data?: LintResultData) => string;
959}
960
961//#endregion
962
963//#region RuleTester
964
965export class RuleTester {
966 constructor(config?: any);
967
968 run(
969 name: string,
970 rule: Rule.RuleModule,
971 tests: {
972 valid?: Array<string | RuleTester.ValidTestCase>;
973 invalid?: RuleTester.InvalidTestCase[];
974 },
975 ): void;
976}
977
978export namespace RuleTester {
979 interface ValidTestCase {
980 code: string;
981 options?: any;
982 filename?: string;
983 parserOptions?: Linter.ParserOptions;
984 settings?: { [name: string]: any };
985 parser?: string;
986 globals?: { [name: string]: boolean };
987 }
988
989 interface SuggestionOutput {
990 messageId?: string;
991 desc?: string;
992 data?: Record<string, unknown>;
993 output: string;
994 }
995
996 interface InvalidTestCase extends ValidTestCase {
997 errors: number | Array<TestCaseError | string>;
998 output?: string | null;
999 }
1000
1001 interface TestCaseError {
1002 message?: string | RegExp;
1003 messageId?: string;
1004 type?: string;
1005 data?: any;
1006 line?: number;
1007 column?: number;
1008 endLine?: number;
1009 endColumn?: number;
1010 suggestions?: SuggestionOutput[];
1011 }
1012}
1013
1014//#endregion
1015
\No newline at end of file