Skip to content

Change Request: context.report should accept wider type for node #19733

@paulius-valiunas

Description

@paulius-valiunas

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

acceptedThere is consensus among the team that this change meets the criteria for inclusioncoreRelates to ESLint's core APIs and featuresenhancementThis change enhances an existing feature of ESLinttypesRelated to TypeScript types

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions