26
26
*/
27
27
28
28
import * as ESTree from "estree" ;
29
- import { Language } from "@eslint/core" ;
29
+ import type {
30
+ RuleVisitor ,
31
+ TextSourceCode ,
32
+ Language ,
33
+ SourceRange ,
34
+ TraversalStep ,
35
+ LanguageOptions as GenericLanguageOptions ,
36
+ RuleDefinition ,
37
+ RuleContext as CoreRuleContext
38
+ } from "@eslint/core" ;
30
39
import { JSONSchema4 } from "json-schema" ;
31
40
import { LegacyESLint } from "./use-at-your-own-risk.js" ;
32
41
42
+ /*
43
+ * Need to extend the `RuleContext` interface to include the
44
+ * deprecated methods that have not yet been removed.
45
+ * TODO: Remove in v10.0.0.
46
+ */
47
+ declare module "@eslint/core" {
48
+ interface RuleContext {
49
+
50
+ /** @deprecated Use `sourceCode.getAncestors()` instead */
51
+ getAncestors ( ) : ESTree . Node [ ] ;
52
+
53
+ /** @deprecated Use `sourceCode.getDeclaredVariables()` instead */
54
+ getDeclaredVariables ( node : ESTree . Node ) : Scope . Variable [ ] ;
55
+
56
+ /** @deprecated Use `sourceCode.getScope()` instead */
57
+ getScope ( ) : Scope . Scope ;
58
+
59
+ /** @deprecated Use `sourceCode.markVariableAsUsed()` instead */
60
+ markVariableAsUsed ( name : string ) : boolean ;
61
+ }
62
+ }
63
+
33
64
export namespace AST {
34
65
type TokenType =
35
66
| "Boolean"
@@ -149,7 +180,12 @@ export namespace Scope {
149
180
150
181
// #region SourceCode
151
182
152
- export class SourceCode {
183
+ export class SourceCode implements TextSourceCode < {
184
+ LangOptions : Linter . LanguageOptions ;
185
+ RootNode : AST . Program ;
186
+ SyntaxElementWithLoc : AST . Token | ESTree . Node ;
187
+ ConfigNode : ESTree . Comment ;
188
+ } > {
153
189
text : string ;
154
190
ast : AST . Program ;
155
191
lines : string [ ] ;
@@ -163,6 +199,9 @@ export class SourceCode {
163
199
164
200
static splitLines ( text : string ) : string [ ] ;
165
201
202
+ getLoc ( syntaxElement : AST . Token | ESTree . Node ) : ESTree . SourceLocation ;
203
+ getRange ( syntaxElement : AST . Token | ESTree . Node ) : SourceRange ;
204
+
166
205
getText ( node ?: ESTree . Node , beforeCount ?: number , afterCount ?: number ) : string ;
167
206
168
207
getLines ( ) : string [ ] ;
@@ -238,6 +277,8 @@ export class SourceCode {
238
277
) : boolean ;
239
278
240
279
markVariableAsUsed ( name : string , refNode ?: ESTree . Node ) : boolean ;
280
+
281
+ traverse ( ) : Iterable < TraversalStep > ;
241
282
}
242
283
243
284
export namespace SourceCode {
@@ -507,21 +548,25 @@ export namespace SourceCode {
507
548
// #endregion
508
549
509
550
export namespace Rule {
510
- interface RuleModule {
511
- create ( context : RuleContext ) : RuleListener ;
512
- meta ?: RuleMetaData | undefined ;
513
- }
551
+
552
+ type RuleModule = RuleDefinition < {
553
+ LangOptions : Linter . LanguageOptions ,
554
+ Code : SourceCode ,
555
+ RuleOptions : any [ ] ,
556
+ Visitor : NodeListener ,
557
+ Node : ESTree . Node ,
558
+ MessageIds : string ,
559
+ ExtRuleDocs : { }
560
+ } > ;
514
561
515
562
type NodeTypes = ESTree . Node [ "type" ] ;
516
- interface NodeListener {
563
+ interface NodeListener extends RuleVisitor {
517
564
ArrayExpression ?: ( ( node : ESTree . ArrayExpression & NodeParentExtension ) => void ) | undefined ;
518
565
"ArrayExpression:exit" ?: ( ( node : ESTree . ArrayExpression & NodeParentExtension ) => void ) | undefined ;
519
566
ArrayPattern ?: ( ( node : ESTree . ArrayPattern & NodeParentExtension ) => void ) | undefined ;
520
567
"ArrayPattern:exit" ?: ( ( node : ESTree . ArrayPattern & NodeParentExtension ) => void ) | undefined ;
521
568
ArrowFunctionExpression ?: ( ( node : ESTree . ArrowFunctionExpression & NodeParentExtension ) => void ) | undefined ;
522
- "ArrowFunctionExpression:exit" ?:
523
- | ( ( node : ESTree . ArrowFunctionExpression & NodeParentExtension ) => void )
524
- | undefined ;
569
+ "ArrowFunctionExpression:exit" ?: ( ( node : ESTree . ArrowFunctionExpression & NodeParentExtension ) => void ) | undefined ;
525
570
AssignmentExpression ?: ( ( node : ESTree . AssignmentExpression & NodeParentExtension ) => void ) | undefined ;
526
571
"AssignmentExpression:exit" ?: ( ( node : ESTree . AssignmentExpression & NodeParentExtension ) => void ) | undefined ;
527
572
AssignmentPattern ?: ( ( node : ESTree . AssignmentPattern & NodeParentExtension ) => void ) | undefined ;
@@ -559,13 +604,9 @@ export namespace Rule {
559
604
ExportAllDeclaration ?: ( ( node : ESTree . ExportAllDeclaration & NodeParentExtension ) => void ) | undefined ;
560
605
"ExportAllDeclaration:exit" ?: ( ( node : ESTree . ExportAllDeclaration & NodeParentExtension ) => void ) | undefined ;
561
606
ExportDefaultDeclaration ?: ( ( node : ESTree . ExportDefaultDeclaration & NodeParentExtension ) => void ) | undefined ;
562
- "ExportDefaultDeclaration:exit" ?:
563
- | ( ( node : ESTree . ExportDefaultDeclaration & NodeParentExtension ) => void )
564
- | undefined ;
607
+ "ExportDefaultDeclaration:exit" ?: ( ( node : ESTree . ExportDefaultDeclaration & NodeParentExtension ) => void ) | undefined ;
565
608
ExportNamedDeclaration ?: ( ( node : ESTree . ExportNamedDeclaration & NodeParentExtension ) => void ) | undefined ;
566
- "ExportNamedDeclaration:exit" ?:
567
- | ( ( node : ESTree . ExportNamedDeclaration & NodeParentExtension ) => void )
568
- | undefined ;
609
+ "ExportNamedDeclaration:exit" ?: ( ( node : ESTree . ExportNamedDeclaration & NodeParentExtension ) => void ) | undefined ;
569
610
ExportSpecifier ?: ( ( node : ESTree . ExportSpecifier & NodeParentExtension ) => void ) | undefined ;
570
611
"ExportSpecifier:exit" ?: ( ( node : ESTree . ExportSpecifier & NodeParentExtension ) => void ) | undefined ;
571
612
ExpressionStatement ?: ( ( node : ESTree . ExpressionStatement & NodeParentExtension ) => void ) | undefined ;
@@ -587,15 +628,11 @@ export namespace Rule {
587
628
ImportDeclaration ?: ( ( node : ESTree . ImportDeclaration & NodeParentExtension ) => void ) | undefined ;
588
629
"ImportDeclaration:exit" ?: ( ( node : ESTree . ImportDeclaration & NodeParentExtension ) => void ) | undefined ;
589
630
ImportDefaultSpecifier ?: ( ( node : ESTree . ImportDefaultSpecifier & NodeParentExtension ) => void ) | undefined ;
590
- "ImportDefaultSpecifier:exit" ?:
591
- | ( ( node : ESTree . ImportDefaultSpecifier & NodeParentExtension ) => void )
592
- | undefined ;
631
+ "ImportDefaultSpecifier:exit" ?: ( ( node : ESTree . ImportDefaultSpecifier & NodeParentExtension ) => void ) | undefined ;
593
632
ImportExpression ?: ( ( node : ESTree . ImportExpression & NodeParentExtension ) => void ) | undefined ;
594
633
"ImportExpression:exit" ?: ( ( node : ESTree . ImportExpression & NodeParentExtension ) => void ) | undefined ;
595
634
ImportNamespaceSpecifier ?: ( ( node : ESTree . ImportNamespaceSpecifier & NodeParentExtension ) => void ) | undefined ;
596
- "ImportNamespaceSpecifier:exit" ?:
597
- | ( ( node : ESTree . ImportNamespaceSpecifier & NodeParentExtension ) => void )
598
- | undefined ;
635
+ "ImportNamespaceSpecifier:exit" ?: ( ( node : ESTree . ImportNamespaceSpecifier & NodeParentExtension ) => void ) | undefined ;
599
636
ImportSpecifier ?: ( ( node : ESTree . ImportSpecifier & NodeParentExtension ) => void ) | undefined ;
600
637
"ImportSpecifier:exit" ?: ( ( node : ESTree . ImportSpecifier & NodeParentExtension ) => void ) | undefined ;
601
638
LabeledStatement ?: ( ( node : ESTree . LabeledStatement & NodeParentExtension ) => void ) | undefined ;
@@ -641,9 +678,7 @@ export namespace Rule {
641
678
SwitchStatement ?: ( ( node : ESTree . SwitchStatement & NodeParentExtension ) => void ) | undefined ;
642
679
"SwitchStatement:exit" ?: ( ( node : ESTree . SwitchStatement & NodeParentExtension ) => void ) | undefined ;
643
680
TaggedTemplateExpression ?: ( ( node : ESTree . TaggedTemplateExpression & NodeParentExtension ) => void ) | undefined ;
644
- "TaggedTemplateExpression:exit" ?:
645
- | ( ( node : ESTree . TaggedTemplateExpression & NodeParentExtension ) => void )
646
- | undefined ;
681
+ "TaggedTemplateExpression:exit" ?: ( ( node : ESTree . TaggedTemplateExpression & NodeParentExtension ) => void ) | undefined ;
647
682
TemplateElement ?: ( ( node : ESTree . TemplateElement & NodeParentExtension ) => void ) | undefined ;
648
683
"TemplateElement:exit" ?: ( ( node : ESTree . TemplateElement & NodeParentExtension ) => void ) | undefined ;
649
684
TemplateLiteral ?: ( ( node : ESTree . TemplateLiteral & NodeParentExtension ) => void ) | undefined ;
@@ -765,39 +800,8 @@ export namespace Rule {
765
800
hasSuggestions ?: boolean | undefined ;
766
801
}
767
802
768
- interface RuleContext {
769
- id : string ;
770
- options : any [ ] ;
771
- settings : { [ name : string ] : any } ;
772
- parserPath : string | undefined ;
773
- languageOptions : Linter . LanguageOptions ;
774
- parserOptions : Linter . ParserOptions ;
775
- cwd : string ;
776
- filename : string ;
777
- physicalFilename : string ;
778
- sourceCode : SourceCode ;
779
-
780
- getAncestors ( ) : ESTree . Node [ ] ;
781
-
782
- getDeclaredVariables ( node : ESTree . Node ) : Scope . Variable [ ] ;
783
-
784
- /** @deprecated Use property `filename` directly instead */
785
- getFilename ( ) : string ;
786
-
787
- /** @deprecated Use property `physicalFilename` directly instead */
788
- getPhysicalFilename ( ) : string ;
789
-
790
- /** @deprecated Use property `cwd` directly instead */
791
- getCwd ( ) : string ;
792
-
793
- getScope ( ) : Scope . Scope ;
794
-
795
- /** @deprecated Use property `sourceCode` directly instead */
796
- getSourceCode ( ) : SourceCode ;
797
-
798
- markVariableAsUsed ( name : string ) : boolean ;
799
-
800
- report ( descriptor : ReportDescriptor ) : void ;
803
+ interface RuleContext extends CoreRuleContext {
804
+ // report(descriptor: ReportDescriptor): void;
801
805
}
802
806
803
807
type ReportFixer = ( fixer : RuleFixer ) => null | Fix | IterableIterator < Fix > | Fix [ ] ;
@@ -1325,7 +1329,7 @@ export namespace Linter {
1325
1329
[ name : string ] : GlobalConf ;
1326
1330
}
1327
1331
1328
- interface LanguageOptions {
1332
+ interface LanguageOptions extends GenericLanguageOptions {
1329
1333
/**
1330
1334
* The version of ECMAScript to support. May be any year (i.e., 2022) or
1331
1335
* version (i.e., 5). Set to "latest" for the most recent supported version.
@@ -1469,7 +1473,7 @@ export namespace ESLint {
1469
1473
environments ?: Record < string , Environment > | undefined ;
1470
1474
languages ?: Record < string , Language > | undefined ;
1471
1475
processors ?: Record < string , Linter . Processor > | undefined ;
1472
- rules ?: Record < string , Rule . RuleModule > | undefined ;
1476
+ rules ?: Record < string , RuleDefinition > | undefined ;
1473
1477
}
1474
1478
1475
1479
type FixType = "directive" | "problem" | "suggestion" | "layout" ;
0 commit comments