Skip to content

Commit 91d4d9f

Browse files
authored
fix: Bring types in sync with @eslint/core (#19157)
* fix: Bring types in sync with @eslint/core * Clean up RuleModule * Update @eslint/core and RuleListener/NodeListener * Update Plugin definition * Update types test * Fix eslint/json types test * Update @eslint/core
1 parent b7012c8 commit 91d4d9f

File tree

4 files changed

+101
-63
lines changed

4 files changed

+101
-63
lines changed

.github/workflows/types-integration.yml

+35-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,40 @@ jobs:
147147
working-directory: eslint-js
148148
run: npm run build:types --workspace eslint-visitor-keys
149149

150+
eslint_json:
151+
name: Types (@eslint/json)
152+
runs-on: ubuntu-latest
153+
steps:
154+
- name: Checkout eslint
155+
uses: actions/checkout@v4
156+
with:
157+
path: eslint
158+
159+
- name: Checkout @eslint/json
160+
uses: actions/checkout@v4
161+
with:
162+
repository: eslint/json
163+
path: json
164+
165+
- uses: actions/setup-node@v4
166+
with:
167+
node-version: "lts/*"
168+
169+
- name: Install Packages (eslint)
170+
working-directory: eslint
171+
run: npm install
172+
173+
- name: Install Packages (neostandard)
174+
working-directory: json
175+
run: |
176+
npm install
177+
npm run build
178+
npm install ../eslint
179+
180+
- name: Run TSC
181+
working-directory: json
182+
run: npm run test:types
183+
150184
are-the-types-wrong:
151185
name: Are the types wrong?
152186
runs-on: ubuntu-latest
@@ -176,4 +210,4 @@ jobs:
176210

177211
- name: Check validity of type definitions
178212
working-directory: ${{ matrix.package.directory }}
179-
run: npm run lint:types
213+
run: npm run lint:types

lib/types/index.d.ts

+64-60
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,41 @@
2626
*/
2727

2828
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";
3039
import { JSONSchema4 } from "json-schema";
3140
import { LegacyESLint } from "./use-at-your-own-risk.js";
3241

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+
3364
export namespace AST {
3465
type TokenType =
3566
| "Boolean"
@@ -149,7 +180,12 @@ export namespace Scope {
149180

150181
// #region SourceCode
151182

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+
}> {
153189
text: string;
154190
ast: AST.Program;
155191
lines: string[];
@@ -163,6 +199,9 @@ export class SourceCode {
163199

164200
static splitLines(text: string): string[];
165201

202+
getLoc(syntaxElement: AST.Token | ESTree.Node): ESTree.SourceLocation;
203+
getRange(syntaxElement: AST.Token | ESTree.Node): SourceRange;
204+
166205
getText(node?: ESTree.Node, beforeCount?: number, afterCount?: number): string;
167206

168207
getLines(): string[];
@@ -238,6 +277,8 @@ export class SourceCode {
238277
): boolean;
239278

240279
markVariableAsUsed(name: string, refNode?: ESTree.Node): boolean;
280+
281+
traverse(): Iterable<TraversalStep>;
241282
}
242283

243284
export namespace SourceCode {
@@ -507,21 +548,25 @@ export namespace SourceCode {
507548
// #endregion
508549

509550
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+
}>;
514561

515562
type NodeTypes = ESTree.Node["type"];
516-
interface NodeListener {
563+
interface NodeListener extends RuleVisitor {
517564
ArrayExpression?: ((node: ESTree.ArrayExpression & NodeParentExtension) => void) | undefined;
518565
"ArrayExpression:exit"?: ((node: ESTree.ArrayExpression & NodeParentExtension) => void) | undefined;
519566
ArrayPattern?: ((node: ESTree.ArrayPattern & NodeParentExtension) => void) | undefined;
520567
"ArrayPattern:exit"?: ((node: ESTree.ArrayPattern & NodeParentExtension) => void) | undefined;
521568
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;
525570
AssignmentExpression?: ((node: ESTree.AssignmentExpression & NodeParentExtension) => void) | undefined;
526571
"AssignmentExpression:exit"?: ((node: ESTree.AssignmentExpression & NodeParentExtension) => void) | undefined;
527572
AssignmentPattern?: ((node: ESTree.AssignmentPattern & NodeParentExtension) => void) | undefined;
@@ -559,13 +604,9 @@ export namespace Rule {
559604
ExportAllDeclaration?: ((node: ESTree.ExportAllDeclaration & NodeParentExtension) => void) | undefined;
560605
"ExportAllDeclaration:exit"?: ((node: ESTree.ExportAllDeclaration & NodeParentExtension) => void) | undefined;
561606
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;
565608
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;
569610
ExportSpecifier?: ((node: ESTree.ExportSpecifier & NodeParentExtension) => void) | undefined;
570611
"ExportSpecifier:exit"?: ((node: ESTree.ExportSpecifier & NodeParentExtension) => void) | undefined;
571612
ExpressionStatement?: ((node: ESTree.ExpressionStatement & NodeParentExtension) => void) | undefined;
@@ -587,15 +628,11 @@ export namespace Rule {
587628
ImportDeclaration?: ((node: ESTree.ImportDeclaration & NodeParentExtension) => void) | undefined;
588629
"ImportDeclaration:exit"?: ((node: ESTree.ImportDeclaration & NodeParentExtension) => void) | undefined;
589630
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;
593632
ImportExpression?: ((node: ESTree.ImportExpression & NodeParentExtension) => void) | undefined;
594633
"ImportExpression:exit"?: ((node: ESTree.ImportExpression & NodeParentExtension) => void) | undefined;
595634
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;
599636
ImportSpecifier?: ((node: ESTree.ImportSpecifier & NodeParentExtension) => void) | undefined;
600637
"ImportSpecifier:exit"?: ((node: ESTree.ImportSpecifier & NodeParentExtension) => void) | undefined;
601638
LabeledStatement?: ((node: ESTree.LabeledStatement & NodeParentExtension) => void) | undefined;
@@ -641,9 +678,7 @@ export namespace Rule {
641678
SwitchStatement?: ((node: ESTree.SwitchStatement & NodeParentExtension) => void) | undefined;
642679
"SwitchStatement:exit"?: ((node: ESTree.SwitchStatement & NodeParentExtension) => void) | undefined;
643680
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;
647682
TemplateElement?: ((node: ESTree.TemplateElement & NodeParentExtension) => void) | undefined;
648683
"TemplateElement:exit"?: ((node: ESTree.TemplateElement & NodeParentExtension) => void) | undefined;
649684
TemplateLiteral?: ((node: ESTree.TemplateLiteral & NodeParentExtension) => void) | undefined;
@@ -765,39 +800,8 @@ export namespace Rule {
765800
hasSuggestions?: boolean | undefined;
766801
}
767802

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;
801805
}
802806

803807
type ReportFixer = (fixer: RuleFixer) => null | Fix | IterableIterator<Fix> | Fix[];
@@ -1325,7 +1329,7 @@ export namespace Linter {
13251329
[name: string]: GlobalConf;
13261330
}
13271331

1328-
interface LanguageOptions {
1332+
interface LanguageOptions extends GenericLanguageOptions {
13291333
/**
13301334
* The version of ECMAScript to support. May be any year (i.e., 2022) or
13311335
* version (i.e., 5). Set to "latest" for the most recent supported version.
@@ -1469,7 +1473,7 @@ export namespace ESLint {
14691473
environments?: Record<string, Environment> | undefined;
14701474
languages?: Record<string, Language> | undefined;
14711475
processors?: Record<string, Linter.Processor> | undefined;
1472-
rules?: Record<string, Rule.RuleModule> | undefined;
1476+
rules?: Record<string, RuleDefinition> | undefined;
14731477
}
14741478

14751479
type FixType = "directive" | "problem" | "suggestion" | "layout";

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"@eslint-community/eslint-utils": "^4.2.0",
103103
"@eslint-community/regexpp": "^4.12.1",
104104
"@eslint/config-array": "^0.19.0",
105-
"@eslint/core": "^0.10.0",
105+
"@eslint/core": "^0.11.0",
106106
"@eslint/eslintrc": "^3.2.0",
107107
"@eslint/js": "9.19.0",
108108
"@eslint/plugin-kit": "^0.2.5",

tests/lib/types/types.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ rule = {
468468
context.markVariableAsUsed("foo");
469469

470470
context.report({ message: "foo", node: AST });
471-
context.report({ message: "foo", loc: { line: 0, column: 0 } });
471+
context.report({ message: "foo", loc: { start: {line: 0, column: 0}, end: { line: 1, column: 1 } } });
472472
context.report({ message: "foo", node: AST, data: { foo: "bar" } });
473473
context.report({ message: "foo", node: AST, fix: () => null });
474474
context.report({ message: "foo", node: AST, fix: ruleFixer => ruleFixer.replaceText(AST, "foo") });

0 commit comments

Comments
 (0)