Skip to content

linter: JS Plugins types for custom rules with createRule() seem incorrect #18154

@connorshea

Description

@connorshea

What version of Oxlint are you using?

1.39.0

What command did you run?

oxlint

What does your .oxlintrc.json config file look like?

{
  "$schema": "./node_modules/oxlint/configuration_schema.json",
  "jsPlugins": [
    "./plugin/oxlint-plugin.ts"
  ],
  "plugins": [
    "react",
    "typescript"
  ],
  "categories": {
    "correctness": "off"
  },
  "rules": {
    "example/require-using-for-temp-prefs": "error",
    "example/too-many-methods": "error"
  }
}

What happened?

This may be a known problem, apologies if so.

I noticed this because of the @ts-expect-error on the custom rule here: https://github.com/glide-browser/glide/blob/265f237e5a5e201dd3e1e69228f0945005791453/.oxlint-plugin.mjs

And then confirmed it had a weird type error, and also that I could create my own, fully-working rule based vaguely on the current docs and get the same kind of type error.

Demo plugin: https://github.com/connorshea/oxlint-issue-repro-template/blob/de85ac327776f2a669a0cff47f729892717dae4a/plugin/oxlint-plugin.ts

You can clone that repo and switch to the custom-js-rules branch to see my custom lint rule working, and also the type error on it and the other rule.

  • git clone https://github.com/connorshea/oxlint-issue-repro-template.git
  • cd oxlint-issue-repro-template && git checkout custom-js-rule
  • npm install
  • npm run lint
  • npx tsc --noEmit

Rule:

import { definePlugin, defineRule } from "oxlint";

const plugin = definePlugin({
  meta: {
    name: "example",
  },
  rules: {
    "too-many-methods": defineRule({
      meta: { type: "problem" },
      // this type-errors, why?
      create(context) {
        return {
          ClassDeclaration(node) {
            const methodCount = node.body.body.filter(
              (member) => member.type === "MethodDefinition"
            ).length;
            if (methodCount >= 3) {
              context.report({ message: "Too many methods on this class! bad!", node });
            }
          },
        };
      },
    }),
  }
});

export default plugin;

Type error:

oxlint-issue-repro-template % npx tsc --noEmit
plugin/oxlint-plugin.ts:11:7 - error TS2322: Type '(context: Context) => { ClassDeclaration(node: Class): void; }' is not assignable to type '((context: Context) => VisitorObject) | ((context: Context) => VisitorObject) | undefined'.
  Type '(context: Context) => { ClassDeclaration(node: Class): void; }' is not assignable to type '(context: Context) => VisitorObject'.
    Type '{ ClassDeclaration(node: Class): void; }' is not assignable to type 'VisitorObject'.
      Property 'ClassDeclaration' is incompatible with index signature.
        Type '(node: Class) => void' is not assignable to type '(node: Node$1) => void'.
          Types of parameters 'node' and 'node' are incompatible.
            Type 'Node$1' is not assignable to type 'Class'.
              Type 'Program' is missing the following properties from type 'Class': decorators, id, superClass

11       create(context) {
         ~~~~~~

  node_modules/oxlint/dist/index.d.ts:3901:3
    3901   create: (context: Context) => VisitorObject;
           ~~~~~~
    The expected type comes from property 'create' which is declared here on type 'Rule'

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions