Skip to content

Change Request: Support generic types for Rule.RuleModule type #19521

@lumirlumir

Description

@lumirlumir

ESLint version

v9.22.0

What problem do you want to solve?

Hello,

Currently, the Rule.RuleModule type is not generic, which prevents me from extending the ExtRuleDocs property with custom properties.

Please take a look at the source code below.

https://github.com/eslint/eslint/blob/main/lib/types/index.d.ts#L554-L562

For example:

// @ts-check

/**
 * @typedef {import("eslint").Rule.RuleModule} RuleModule
 */

/** @type {RuleModule} */
export default {
  meta: {
    docs: {
      'my-custom-property': 'my-custom-property' // ❌This line causes a type error❌
      recommended: true,
    },
  },
}

When I try to add my-custom-property to meta.docs, I get a type error because ExtRuleDocs is simply initialized as {} with no way to extend its functionality.

I’d like to suggest adding support for generic types in Rule.RuleModule to allow for greater flexibility.

What do you think is the correct solution?

  • Before: The RuleModule type cannot be extended.
-   type RuleModule = RuleDefinition<{
        LangOptions: Linter.LanguageOptions,
        Code: SourceCode,
        RuleOptions: any[],
        Visitor: NodeListener,
        Node: ESTree.Node,
        MessageIds: string,
-       ExtRuleDocs: {}
    }>;
  • After: The RuleModule type can be extended using generics, allowing users to define custom types.
+   type RuleModule<ExtRuleDocs = {}> = RuleDefinition<{
        LangOptions: Linter.LanguageOptions,
        Code: SourceCode,
        RuleOptions: any[],
        Visitor: NodeListener,
        Node: ESTree.Node,
        MessageIds: string,
+       ExtRuleDocs: ExtRuleDocs,
    }>;

As a result, We can define a custom ExtRuleDocs type like the code below:

/**
 * @typedef {import("eslint").Rule.RuleModule<{ 'my-custom-property'?: string }>} RuleModule
 */

Please note that this change does not affect ESLint's current behavior. Looking forward to your comments on this!

Participation

  • I am willing to submit a pull request for this change.

Additional comments

No response

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