SmartRecruiters’ ESLint rules and configs.
Current version is designed to work with eslint@^6
.
Install the latest eslint
and @smartrecruiters/eslint-config
as devDependency (-D
) in your project:
$ npm i -D eslint @smartrecruiters/eslint-config
In root directory of your project, create .eslintrc.yaml
with following content:
extends: '@smartrecruiters/eslint-config/node/main'
or, when you use node.js v10
extends: '@smartrecruiters/eslint-config/node/10/main'
Additionally, if your project uses mocha
, you may override main config and use eslint config prepared for mocha test.
To do it, go to your test directory and create .eslintrc.yaml
with following content:
extends: '@smartrecruiters/eslint-config/node/mocha'
or, when you use node.js v10
extends: '@smartrecruiters/eslint-config/node/10/mocha'
Update script section in your package.json
:
{
"scripts": {
"lint": "eslint ."
}
}
To run linter, just type:
$ npm run lint
When you first apply linting in your legacy project, you may find many violations. Don't worry, there are some easy ways to handle this:
Many rules have ability to fix your code. After you configured eslint in your project in a way described above, just run:
npm run lint -- --fix
It is done in such way because if you want to pass params into your custom npm script, you need to do that after --
,
so in fact what this command does is:
node_modules/.bin/eslint . --fix
You can easily override eslint configuration. For example, if you really want to use console.log
function, you can
globally disable no-console
rule by changing .eslintrc.yaml
:
extends: '@smartrecruiters/eslint-config/node/main'
rules:
no-console: off
or disable this rule in a particular file or in a part of it: Disabling Rules with Inline Comments
Similarly, if you really want to use console.log
, but you also want to be somehow warned about it's usage, you can
change no-console
rule violations severity from error to warning by changing .eslintrc.yaml
:
extends: '@smartrecruiters/eslint-config/node/main'
rules:
no-console: warn
or you can change severity of rule in a particular file: Configuring Rules
And if you decide to change some errors to warnings, you can limit possible warning count to make sure no more lint violations are introduced:
npm run lint -- --max-warnings Int
where Int
is a number of maximum number of warnings allowed.
Same, what this command does is actually:
node_modules/.bin/eslint . --max-warnings Int
Use .eslintignore
file to disable eslint in files and directories:
Ignoring Files and Directories
It is very handy to have enabled automatic linting in your IDE:
- Intellij IDEA -> Preferences
- Languages & Frameworks -> JavaScript -> Code Quality Tools -> ESLint
- Tick Enable
- Specify ESLint package:
<path_to_your_project>/node_modules/eslint
- Tick Automatic search
Prepared for your node.js code. It is based on built-in eslint config eslint-recommended. But it also has following rules enabled (see reference section for description of each rule):
Based on '@smartrecruiters/eslint-config/node/main', but it is prepared for mocha & chai env. If you have globals configured for your mocha test similarly:
const chai = require('chai')
chai.use(require('chai-as-promised'))
const sinon = require('sinon')
global.sinon = sinon
global.chai = chai
global.expect = chai.expect
global.should = chai.should()
global.assert = chai.assert
Then you can write tests without requiring chai in each file. This will look like using undefined variables, so this
eslint configuration speficies sinon
, chai
, expect
,should
and assert
as allowed global variables.
It also disables no-unused-expressions
rule, because e.g. writing assertions with expect may end up with:
expect(aVirginOver20).to.exists
Which of course from technical point of view is an unused expression.
Please see our Code of conduct and Contributing guidelines