Skip to content

Commit a467de3

Browse files
snitin315fasttime
andauthored
fix: update context.report types (#19751)
* fix: udpate `context.report` types * test: add tests * docs: fix context.report() nodeType description * fix: udpate `Rule.RuleModule` types * refactor: udpate types * chore: fix formatting * fix: export JSSyntaxElement Co-authored-by: Francesco Trotta <github@fasttime.org> * chore: add tests --------- Co-authored-by: Francesco Trotta <github@fasttime.org>
1 parent 59dd7e6 commit a467de3

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

docs/src/extend/custom-rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ The main method you'll use when writing custom rules is `context.report()`, whic
163163

164164
- `messageId`: (`string`) The ID of the message (see [messageIds](#messageids)) (recommended over `message`).
165165
- `message`: (`string`) The problem message (alternative to `messageId`).
166-
- `node`: (optional `object`) The AST node related to the problem. If present and `loc` is not specified, then the starting location of the node is used as the location of the problem.
166+
- `node`: (optional `object`) This can be an AST node, a token, or a comment related to the problem. If present and `loc` is not specified, then the starting location of the node is used as the location of the problem.
167167
- `loc`: (optional `object`) Specifies the location of the problem. If both `loc` and `node` are specified, then the location is used from `loc` instead of `node`.
168168
- `start`: An object of the start location.
169169
- `line`: (`number`) The 1-based line number at which the problem occurred.

docs/src/integrate/nodejs-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ The information available for each linting message is:
672672
- `line` - the line on which the error occurred.
673673
- `message` - the message that should be output.
674674
- `messageId` - the ID of the message used to generate the message (this property is omitted if the rule does not use message IDs).
675-
- `nodeType` - (**Deprecated:** This property will be removed in a future version of ESLint.) the node or token type that was reported with the problem.
675+
- `nodeType` - (**Deprecated:** This property will be removed in a future version of ESLint.) the node, comment, or token type that was reported with the problem.
676676
- `ruleId` - the ID of the rule that triggered the messages (or null if `fatal` is true).
677677
- `severity` - either 1 or 2, depending on your configuration.
678678
- `endColumn` - the end column of the range on which the error occurred (this property is omitted if it's not range).

lib/types/index.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,19 @@ export namespace SourceCode {
584584

585585
// #endregion
586586

587+
export type JSSyntaxElement = {
588+
type: string;
589+
loc?: ESTree.SourceLocation | null | undefined;
590+
};
591+
587592
export namespace Rule {
588593
interface RuleModule
589594
extends RuleDefinition<{
590595
LangOptions: Linter.LanguageOptions;
591596
Code: SourceCode;
592597
RuleOptions: any[];
593598
Visitor: NodeListener;
594-
Node: ESTree.Node;
599+
Node: JSSyntaxElement;
595600
MessageIds: string;
596601
ExtRuleDocs: {};
597602
}> {
@@ -1159,10 +1164,10 @@ export namespace Rule {
11591164
/**
11601165
* Indicates the type of rule:
11611166
* - `"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.
1162-
* - `"suggestion"` means the rule is identifying something that could be done in a better way but no errors will occur if the code isnt changed.
1167+
* - `"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.
11631168
* - `"layout"` means the rule cares primarily about whitespace, semicolons, commas, and parentheses,
11641169
* all the parts of the program that determine how the code looks rather than how it executes.
1165-
* These rules work on parts of the code that arent specified in the AST.
1170+
* These rules work on parts of the code that aren't specified in the AST.
11661171
*/
11671172
type?: "problem" | "suggestion" | "layout" | undefined;
11681173
/**
@@ -1177,7 +1182,7 @@ export namespace Rule {
11771182
LangOptions: Linter.LanguageOptions;
11781183
Code: SourceCode;
11791184
RuleOptions: any[];
1180-
Node: ESTree.Node;
1185+
Node: JSSyntaxElement;
11811186
MessageIds: string;
11821187
}> {
11831188
/*
@@ -1275,7 +1280,7 @@ export type JSRuleDefinition<
12751280
LangOptions: Linter.LanguageOptions;
12761281
Code: SourceCode;
12771282
Visitor: Rule.NodeListener;
1278-
Node: ESTree.Node;
1283+
Node: JSSyntaxElement;
12791284
} & Required<
12801285
// Rule specific type options (custom)
12811286
Options &

tests/lib/types/types.test.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
RuleTester,
3636
Scope,
3737
SourceCode,
38+
JSSyntaxElement,
3839
} from "eslint";
3940
import { defineConfig, globalIgnores } from "eslint/config";
4041
import { ESLintRules } from "eslint/rules";
@@ -149,9 +150,9 @@ sourceCode.getFirstToken(AST, { includeComments: true }); // $ExpectType Comment
149150
sourceCode.getFirstToken(AST, { includeComments: true, skip: 0 });
150151
// prettier-ignore
151152
sourceCode.getFirstToken(AST, { // $ExpectType (Token & { type: "Identifier"; }) | null
152-
includeComments: true,
153-
skip: 0,
154-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
153+
includeComments: true,
154+
skip: 0,
155+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
155156
});
156157

157158
sourceCode.getFirstTokens(AST); // $ExpectType Token[]
@@ -169,16 +170,16 @@ sourceCode.getFirstTokens(AST, {
169170
});
170171
// prettier-ignore
171172
sourceCode.getFirstTokens(AST, { // $ExpectType (Token & { type: "Identifier"; })[]
172-
count: 0,
173-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
173+
count: 0,
174+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
174175
});
175176
sourceCode.getFirstTokens(AST, { includeComments: true }); // $ ExpectType (Comment | Token)[]
176177
sourceCode.getFirstTokens(AST, { includeComments: true, count: 0 }); // $ ExpectType (Comment | Token)[]
177178
// prettier-ignore
178179
sourceCode.getFirstTokens(AST, { // $ExpectType (Token & { type: "Identifier"; })[]
179-
includeComments: true,
180-
count: 0,
181-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
180+
includeComments: true,
181+
count: 0,
182+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
182183
});
183184

184185
sourceCode.getLastToken(AST);
@@ -302,7 +303,7 @@ sourceCode.getFirstTokenBetween(
302303
);
303304
// prettier-ignore
304305
sourceCode.getFirstTokenBetween(AST, AST, { // $ExpectType (Token & { type: "Identifier"; }) | null
305-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
306+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
306307
});
307308
sourceCode.getFirstTokenBetween(AST, AST, {
308309
skip: 0,
@@ -313,9 +314,9 @@ sourceCode.getFirstTokenBetween(AST, AST, { includeComments: true }); // $Expect
313314
sourceCode.getFirstTokenBetween(AST, AST, { includeComments: true, skip: 0 });
314315
// prettier-ignore
315316
sourceCode.getFirstTokenBetween(AST, AST, { // $ExpectType (Token & { type: "Identifier"; }) | null
316-
includeComments: true,
317-
skip: 0,
318-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
317+
includeComments: true,
318+
skip: 0,
319+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
319320
});
320321

321322
sourceCode.getFirstTokensBetween(AST, AST); // $ExpectType Token[]
@@ -329,7 +330,7 @@ sourceCode.getFirstTokensBetween(
329330
);
330331
// prettier-ignore
331332
sourceCode.getFirstTokensBetween(AST, AST, { // $ExpectType (Token & { type: "Identifier"; })[]
332-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
333+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
333334
});
334335
sourceCode.getFirstTokensBetween(AST, AST, {
335336
count: 0,
@@ -339,9 +340,9 @@ sourceCode.getFirstTokensBetween(AST, AST, { includeComments: true }); // $Expec
339340
sourceCode.getFirstTokensBetween(AST, AST, { includeComments: true, count: 0 });
340341
// prettier-ignore
341342
sourceCode.getFirstTokensBetween(AST, AST, { // $ExpectType (Token & { type: "Identifier"; })[]
342-
includeComments: true,
343-
count: 0,
344-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
343+
includeComments: true,
344+
count: 0,
345+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
345346
});
346347

347348
sourceCode.getLastTokenBetween(AST, AST);
@@ -401,8 +402,8 @@ sourceCode.getTokens(AST, {
401402
sourceCode.getTokens(AST, { includeComments: true }); // $ExpectType (Comment | Token)[]
402403
// prettier-ignore
403404
sourceCode.getTokens(AST, { // $ExpectType (Token & { type: "Identifier"; })[]
404-
includeComments: true,
405-
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
405+
includeComments: true,
406+
filter: (t): t is AST.Token & { type: "Identifier" } => t.type === "Identifier",
406407
});
407408

408409
sourceCode.commentsExistBetween(AST, AST);
@@ -2202,3 +2203,40 @@ defineConfig([
22022203
]);
22032204

22042205
// #endregion
2206+
2207+
// #region JSSyntaxElement
2208+
2209+
const fooRule1: Rule.RuleModule = {
2210+
create(context) {
2211+
return {
2212+
Program(node) {
2213+
for (const comment of node.comments ?? []) {
2214+
context.report({
2215+
node: comment,
2216+
messageId: "foo",
2217+
});
2218+
}
2219+
},
2220+
};
2221+
},
2222+
};
2223+
2224+
const fooRule2: JSRuleDefinition = {
2225+
create(context) {
2226+
return {
2227+
Program(node) {
2228+
for (const comment of node.comments ?? []) {
2229+
context.report({
2230+
node: comment,
2231+
messageId: "foo",
2232+
});
2233+
}
2234+
},
2235+
};
2236+
},
2237+
};
2238+
2239+
const SYNTAX_ELEMENT_COMMENT: JSSyntaxElement = COMMENT;
2240+
const SYNTAX_ELEMENT_TOKEN: JSSyntaxElement = TOKEN;
2241+
2242+
// #endregion JSSyntaxElement

0 commit comments

Comments
 (0)