Skip to content

Commit 0e20aa7

Browse files
authoredMar 20, 2025··
fix: move deprecated RuleContext methods to subtype (#19531)
1 parent 5228383 commit 0e20aa7

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed
 

‎lib/types/index.d.ts

+29-31
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@ import type {
4141
import { JSONSchema4 } from "json-schema";
4242
import { LegacyESLint } from "./use-at-your-own-risk.js";
4343

44-
/*
45-
* Need to extend the `RuleContext` interface to include the
46-
* deprecated methods that have not yet been removed.
47-
* TODO: Remove in v10.0.0.
48-
*/
49-
declare module "@eslint/core" {
50-
interface RuleContext {
51-
/** @deprecated Use `sourceCode.getAncestors()` instead */
52-
getAncestors(): ESTree.Node[];
53-
54-
/** @deprecated Use `sourceCode.getDeclaredVariables()` instead */
55-
getDeclaredVariables(node: ESTree.Node): Scope.Variable[];
56-
57-
/** @deprecated Use `sourceCode.getScope()` instead */
58-
getScope(): Scope.Scope;
59-
60-
/** @deprecated Use `sourceCode.markVariableAsUsed()` instead */
61-
markVariableAsUsed(name: string): boolean;
62-
}
63-
}
64-
6544
export namespace AST {
6645
type TokenType =
6746
| "Boolean"
@@ -607,15 +586,18 @@ export namespace SourceCode {
607586
// #endregion
608587

609588
export namespace Rule {
610-
type RuleModule = RuleDefinition<{
611-
LangOptions: Linter.LanguageOptions;
612-
Code: SourceCode;
613-
RuleOptions: any[];
614-
Visitor: NodeListener;
615-
Node: ESTree.Node;
616-
MessageIds: string;
617-
ExtRuleDocs: {};
618-
}>;
589+
interface RuleModule
590+
extends RuleDefinition<{
591+
LangOptions: Linter.LanguageOptions;
592+
Code: SourceCode;
593+
RuleOptions: any[];
594+
Visitor: NodeListener;
595+
Node: ESTree.Node;
596+
MessageIds: string;
597+
ExtRuleDocs: {};
598+
}> {
599+
create(context: RuleContext): NodeListener;
600+
}
619601

620602
type NodeTypes = ESTree.Node["type"];
621603
interface NodeListener extends RuleVisitor {
@@ -1199,7 +1181,23 @@ export namespace Rule {
11991181
Node: ESTree.Node;
12001182
}
12011183
> {
1202-
// report(descriptor: ReportDescriptor): void;
1184+
/*
1185+
* Need to extend the `RuleContext` interface to include the
1186+
* deprecated methods that have not yet been removed.
1187+
* TODO: Remove in v10.0.0.
1188+
*/
1189+
1190+
/** @deprecated Use `sourceCode.getAncestors()` instead */
1191+
getAncestors(): ESTree.Node[];
1192+
1193+
/** @deprecated Use `sourceCode.getDeclaredVariables()` instead */
1194+
getDeclaredVariables(node: ESTree.Node): Scope.Variable[];
1195+
1196+
/** @deprecated Use `sourceCode.getScope()` instead */
1197+
getScope(): Scope.Scope;
1198+
1199+
/** @deprecated Use `sourceCode.markVariableAsUsed()` instead */
1200+
markVariableAsUsed(name: string): boolean;
12031201
}
12041202

12051203
type ReportFixer = (

‎tests/lib/types/types.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
StaticBlock,
5353
WhileStatement,
5454
} from "estree";
55-
import { Language } from "@eslint/core";
55+
import { Language, RuleDefinition } from "@eslint/core";
5656

5757
const SOURCE = `var foo = bar;`;
5858

@@ -783,6 +783,23 @@ rule = {
783783
},
784784
};
785785

786+
type DeprecatedRuleContextKeys =
787+
| "getAncestors"
788+
| "getDeclaredVariables"
789+
| "getScope"
790+
| "markVariableAsUsed";
791+
(): RuleDefinition => ({
792+
create(context) {
793+
// Ensure that deprecated RuleContext methods are not defined when using RuleDefinition
794+
context satisfies {
795+
[Key in keyof typeof context]: Key extends DeprecatedRuleContextKeys
796+
? never
797+
: (typeof context)[Key];
798+
};
799+
return {};
800+
},
801+
});
802+
786803
// #endregion
787804

788805
// #region Linter

0 commit comments

Comments
 (0)
Please sign in to comment.