Search code examples
javascriptnode.jssonarqubeeslintsonarscanner

throws an error while running my custom rules in ESlint 1:1 error Definition for rule 'D:/Sports/eslint-rules/no-log.js' was not found


I want to create couple of custom rule for my nodejs project by eslint and generate one json analysis report but while running my custom rules it's throwing an error say 1:1 error Definition for rule 'D:/Sports/eslint-rules/no-log.js' was not found

I have created one custom rule with ESlint for my nodejs project

here is my .eslintrc.js file

module.exports = {
  env: {
    node: true,
  },
  plugins: ['eslint-rules'],
  rules: {
    'eslint-rules/no-log': 'error',
    'eslint-rules/max-params': ['error', { maxParams: 3 }],
  },
};

and here is my one of the custom rule file named as eslint-rules/no-log.js

module.exports = {
  meta: {
    type: "suggestion",
    docs: {
      description: "Disallow console.log() in code",
      category: "Best Practices",
      recommended: true,
    },
    schema: [],
  },
  create: function (context) {
    return {
      CallExpression(node) {
        const { callee } = node;
        if (
          callee.type === "MemberExpression" &&
          callee.object &&
          callee.object.name === "console" &&
          callee.property &&
          callee.property.name === "log"
        ) {
          context.report({
            node,
            message: "Avoid using console.log() in code.",
          });
        }
      },
    };
  },
};

and i run my custom rule through this command npx eslint ./ --format json --output-file eslint-report.json


Solution

  • Local rules in eslint need one of the following:

    1. Run eslint with the --rulesdir option pointing at the location of your rules (this won't work if you have an IDE plugin for eslint though)
    2. Use eslint-plugin-rulesdir which lets you specify rulesdir in your config file instead of command line)
    3. Use eslint-plugin-local-rules which is super simple to configure.