-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Description
ESLint version
9.26.0
What problem do you want to solve?
Currently, only variables of type Node
can be used as the node
argument for RuleContext.report
. The Node
type is defined like so:
export interface NodeMap {
AssignmentProperty: AssignmentProperty;
CatchClause: CatchClause;
Class: Class;
ClassBody: ClassBody;
Expression: Expression;
Function: Function;
Identifier: Identifier;
Literal: Literal;
MethodDefinition: MethodDefinition;
ModuleDeclaration: ModuleDeclaration;
ModuleSpecifier: ModuleSpecifier;
Pattern: Pattern;
PrivateIdentifier: PrivateIdentifier;
Program: Program;
Property: Property;
PropertyDefinition: PropertyDefinition;
SpreadElement: SpreadElement;
Statement: Statement;
Super: Super;
SwitchCase: SwitchCase;
TemplateElement: TemplateElement;
VariableDeclarator: VariableDeclarator;
}
export type Node = NodeMap[keyof NodeMap];
This means you cannot use it with other types of nodes, such as Comment
.
What do you think is the correct solution?
What RuleContext.report
really needs is a reference to the source location of the node. These properties come from the BaseNodeWithoutComments
interface that each of the allowed Node
types extend. However, those are not the only types that extend it. For example:
export interface BaseNodeWithoutComments {
// Every leaf interface that extends BaseNode must specify a type property.
// The type property should be a string literal. For example, Identifier
// has: `type: "Identifier"`
type: string;
loc?: SourceLocation | null | undefined;
range?: [number, number] | undefined;
}
export interface Comment extends BaseNodeWithoutComments {
type: "Line" | "Block";
value: string;
}
The Comment
type has the same location properties as any other node type on the list, but it's not accepted as an argument for RuleContext.report
. I think any type that extends BaseNodeWithoutComments
should be accepted.
Participation
- I am willing to submit a pull request for this change.
Additional comments
It's worth noting that RuleContext
is generic and the type for Node
is unknown
by default. However, that type is in @eslint/core
and it's only re-exported from eslint
with the generic parameter set to the Node
type quoted above. As far as I know, there's no way to use it with a different node type without explicitly importing @eslint/core
Metadata
Metadata
Assignees
Labels
Type
Projects
Status