Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-restricted-imports] Allow configuring severity at the path level so both errors and warnings are possible #14061

Closed
dshook opened this issue Feb 2, 2021 · 7 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint

Comments

@dshook
Copy link

dshook commented Feb 2, 2021

The version of ESLint you are using.
6.8.0

The problem you want to solve.
I want to be able to configure the no-restricted-imports rule to have some paths be errors and others only be warnings. After looking at the docs, the source for the rule, and much searching I couldn't find any way to configure it to do that. I believe this is a broader configuration issue than just the no-restricted-imports but this is the current rule I'm running into the roadblock with.

In JSON configuration

    "no-restricted-imports": [
      "error",
      {
        "paths": [
          {
            "name": "somepackage",
            "message": "I want to make this one an error"
          },
          {
            "name": "otherpackage",
            "message": "But this should only be a warning"
          },
        ],
      }
    ],

Your take on the correct solution to problem.

    "no-restricted-imports": [
      "error",
      {
        "paths": [
          {
            "name": "somepackage",
            "message": "I want to make this one an error"
          },
          {
            "name": "otherpackage",
            "message": "But this should only be a warning. By providing a severity here it overrides the severity set above",
            "severity": "warn"
          },
        ],
      }
    ],

Are you willing to submit a pull request to implement this change?
No

@dshook dshook added core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint triage An ESLint team member will look at this issue soon labels Feb 2, 2021
@mdjermanovic mdjermanovic removed the triage An ESLint team member will look at this issue soon label Feb 2, 2021
@mdjermanovic
Copy link
Member

Hi @dshook, thanks for the issue!

This request looks similar to #13831.

Supporting this would require a lot of changes in ESLint's design, for maybe only a few rules where it would be useful.

As a workaround, you can make a clone of the rule and then configure the two rules separately (e.g., the original rule as "error", and its clone as "warn").

@dshook
Copy link
Author

dshook commented Feb 2, 2021

Thanks for the quick reply!

Just to be clear, when you say clone you mean using one of the other packages (like eslint-rule-extender or eslint-rule-composer mentioned in the other issue) right? I can't just put another instance of the no-restricted-imports in the config since it's a key of the rules object.

@mdjermanovic
Copy link
Member

Yes, it should be two different rules, seemingly unrelated to each other.

eslint-rule-extender or eslint-rule-composer are actually not necessary if we just want to clone a rule without any modifications. New rule can just "re-export" the existing one, so this would be all the code of the new rule:

const eslint = require("eslint");

module.exports = new eslint.Linter().getRules().get("no-restricted-imports");

Now you can use this module as a custom rule with --rulesdir. For example, the above code can be placed in my-rules/no-restricted-imports-clone.js, and then the two rules would be configured like this:

  rules: {
    "no-restricted-imports": [
      "error",
      {
        "paths": [
          {
            "name": "somepackage",
            "message": "I want to make this one an error"
          }
        ],
      }
    ],
    "no-restricted-imports-clone": [
      "warn",
      {
        "paths": [
          {
            "name": "otherpackage",
            "message": "But this should only be a warning"
          },
        ],
      }
    ]
  },

ESLint would be run with --rulesdir my-rules.

Another option is to make a plugin. For an example of a plugin that doesn't have to be published, you can see our internal rules used for this project only:

"eslint-plugin-internal-rules": "file:tools/internal-rules",

The plugin is in https://github.com/eslint/eslint/tree/master/tools/internal-rules

@kylecombes
Copy link

kylecombes commented Feb 8, 2021

Thanks, this works great! The only thing I had to add is an /* eslint-disable */ to the top of the rule file (otherwise it complained about not being able to find the rule it was defining).

I'm getting a ESLint: Definition for rule 'no-restricted-imports-clone' was not found. (no-restricted-imports-clone) when trying that approach. I'm going to try defining a plugin tomorrow and see if that works for us.

@kylecombes
Copy link

Looks like configuring it as a plugin (following this guide) worked.

@nzakas
Copy link
Member

nzakas commented Feb 9, 2021

Resolved, so closing.

@nzakas nzakas closed this as completed Feb 9, 2021
@BarbecueSilver
Copy link

I am having the same issue, that we want warnings as well as errors for imports.
Thanks for pointing out, how to work around this issue 👍

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Aug 9, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint
Projects
Archived in project
Development

No branches or pull requests

5 participants