Skip to content

SmartRecruiters rules and configs for ESLint

License

Notifications You must be signed in to change notification settings

smartrecruiters/eslint-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@smartrecruiters/eslint-config

NPM Version NPM Downloads Node.js Version Licence Build

SmartRecruiters’ ESLint rules and configs.

Linting code in your module

ESLint compatibility

Current version is designed to work with eslint@^6.

Install eslint & @smartrecruiters/eslint-config

Install the latest eslint and @smartrecruiters/eslint-config as devDependency (-D) in your project:

$ npm i -D eslint @smartrecruiters/eslint-config

Configure eslint in your project

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 ."
  }
}

Run linter

To run linter, just type:

$ npm run lint

Hints

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:

Automatic fix

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

Disable unwanted rule

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

Change rule violation from error to warning

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

Limit warnings

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

Ignore specific files and directories

Use .eslintignore file to disable eslint in files and directories: Ignoring Files and Directories

Configuration for Intellij IDEA

It is very handy to have enabled automatic linting in your IDE:

  1. Intellij IDEA -> Preferences
  2. Languages & Frameworks -> JavaScript -> Code Quality Tools -> ESLint
  3. Tick Enable
  4. Specify ESLint package: <path_to_your_project>/node_modules/eslint
  5. Tick Automatic search

Available configurations

smartrecruiters/node/main

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):

smartrecruiters/node/mocha

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.

Contributing

Please see our Code of conduct and Contributing guidelines

References

License

MIT