13

With a node.js project, I've added eslint-plugin-security and it is giving a lot of warnings for code in my test/spec files (using mochajs). Since the test code won't be running in production, these don't seem as useful as they do in the project's actual code. (A lot of Generic Object Injection Sink warnings )

Is there a way to have the security plugin ignore certain files other than putting /* eslint-disable */ at the top of every spec file?

Jim
  • 3,476
  • 4
  • 23
  • 33
  • So maybe a better question is "Is there a way to configure ignore rules per plugin?" – Jim Aug 16 '18 at 15:10
  • 2
    What I went with was using https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy project/.eslintrc.js [has plugins that apply to everything: prettier] project/src/.eslintrc.js [adds override to add eslint-plugin-security] project/tests/.eslintrc.js [adds override to add eslint-plugin-mocha] I'm not really in love with that setup though because of the way it spreads the eslint configs around, I'd rather have just one file. Also found, but did not use: https://www.npmjs.com/package/eslint-plugin-disable – Jim Aug 16 '18 at 15:50

2 Answers2

20

The best way I found to deal with this case is based on this answer. You can override parts of your eslint file in a subfolder. In my case I'm disabling problematic rules from a jest plugin inside my e2e tests folder. Example .eslintrc.js in /e2e-tests/ :

module.exports = {
  overrides: [
    {
      files: ["*.spec.js"],

      rules: {
        "jest/valid-expect": 0
      }
    }
  ]
};
Sebastian Rehm
  • 340
  • 2
  • 9
  • Just a little note for anyone reading this: If you're using an .eslintrc file, you'll need to wrap properties like in double quotes like `"overrides"`, `"files"` and `"rules"`, but ensure that the `0` is not quoted. – Manpreet Mar 24 '23 at 17:07
  • This is fucking awesome! Thank you – bj97301 Apr 06 '23 at 19:21
17

There is three way to ignore files or folders:

1. Creating a .eslintignore on your project root folder with the thing you want to ignore:

**/*.js

2. Using eslint cli & the --ignore-path to specify another file where your ignore rules will be located

eslint --ignore-path .jshintignore file.js

3. Using your package.json

{
  "name": "mypackage",
  "version": "0.0.1",
  "eslintConfig": {
      "env": {
          "browser": true,
          "node": true
      }
  },
  "eslintIgnore": ["*.spec.ts", "world.js"]
}

Official Documentation

On my side, I had issue with Intellij IDEA where eslint was checking files in a folder only dedicated to Typescript (+tslint) which was a pain, so I've picked solution 3.

Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119