not-an-aardvark / eslint-rule-composer

A utility for composing ESLint rules from other ESLint rules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing rule config to composed rule

OliverJAsh opened this issue · comments

I want to create an ESLint rule that composes no-restricted-imports. This rule requires configuration, but I'm not sure how to pass that to the composed rule.

For example, I would like to define a rule called no-foo-imports which disallows imports from module foo. (The reason I would like to do this is so I can use disable comments to target this specific rule rather than all usages of no-restricted-imports, which may define rules for other imports which I do not want to disable.)

I want this myself, but after trying to hack through it, I've learning that it doesn't appear possible with the way that ESLint has frozen the context.options property. I'm going to keep looking, but I'm about to just roll my own version of the rules I wanted to extend.

FWIW, I needed to do this with a rule of my own - that I'd deprecated in favour of a new rule that took options - and I did it like this:

  create: (context) =>
    baseRule.create(
      Object.setPrototypeOf(
        {
          options: [{ types: ["Array"] }],
        },
        context
      )
    ),