question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Feb 3, 2021

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:

https://github.com/eslint/eslint/blob/4cab165bf4e6e5e9f42a59a37a8ff2548c0af87d/package.json#L102

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

1reaction
kylecombescommented, Feb 9, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-restricted-imports - ESLint - Pluggable JavaScript Linter
This rule allows you to specify imports that you don't want to use in your application. It applies to static imports only, not...
Read more >
Emitting Multiple Error Levels with the Same ESlint Rule
This rule allows users to specify imports that are not allowed in their applications, flagging them at either the “error” level or the...
Read more >
Improve your code with lint checks - Android Developers
All lint methods that override the given severity level of an issue— enable , disable / ignore , informational , warning , error...
Read more >
Settings Reference for Python - Visual Studio Code
Allows a user to override the severity levels for individual diagnostics. For each rule, the available severity levels are error (red squiggle), warning...
Read more >
How to use no-restricted-imports to prevent any import file that ...
To do so, I've used the following rules: { 'no-restricted-imports': ['error', { patterns: [{ group: ['_*'], message: 'Import files starting ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found