Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve local plugin or rule using rc config file format?

Eslint v8.50, using eslintrc.js config format

I am following custom rule tutorial to create custom rule (which requires a plugin from what I understand), but I'm not able import my local plugin.

Tried following:

  • provide object as per tutorial: object not supported, string required
  • try providing path to my plugin: getting Plugins array cannot includes file paths error
  • create eslint-plugin-example dir with index.js in root of the project (next to eslintrc.js) and provide "eslint-plugin-example" in plugins config: config not found

The only way I managed to make it work is to do npm i ./eslint-plugin-example --save-dev which created symlink in node_modules. This feels too complicated for such simple thing as including local rule/plugin.

Is there any other way to achieve it? (without migrating to flat config)

like image 468
Pavel Gurecki Avatar asked Dec 29 '25 09:12

Pavel Gurecki


1 Answers

I also didn't find another solution, but it works quite well.

The file structure:

file structure

eslint-plugin-custom/index.js:

module.exports = {
    rules: {
      'my-custom-rule': require('./rules/my-custom-rule')
    }
  };
  

eslint-plugin-custom/package.json (need to install as package):

{
  "name": "eslint-plugin-custom",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
  

eslint-plugin-custom/rules/my-custom-rule.js (replace code with your rule):

module.exports = {
    meta: {
      type: 'problem',
      docs: {
        description: 'disallow the use of console.log',
        category: 'Best Practices',
        recommended: false
      },
      schema: [], // no options
    },
    create: function(context) {
      return {
        CallExpression(node) {
          if (node.callee.object && node.callee.object.name === 'console' && node.callee.property.name === 'log') {
            context.report({
              node,
              message: 'Unexpected console.log statement.'
            });
          }
        }
      };
    }
  };
  

updates in the eslintrc.json:

{
  ...
  "plugins": [
    ...,
    "custom"
  ],
  "overrides": [
    ...
    {
      ...
      "rules": {
        ...
        "custom/my-custom-rule": "error"
      }
    }
  ]
}
  

install command:

npm i .\eslint-plugin-custom --save-dev
like image 130
Igor Ganov Avatar answered Dec 31 '25 22:12

Igor Ganov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!