eslint-config-topcoder v2.0.0
eslint-config-topcoder
This package contains ESLint configs for applications:
- nodejs v7 (backend application)
- nodejs v7 with babel (backend application written in babel)
- react
It contains plugins:
- eslint-plugin-babel - ESLint rules for babel
- eslint-plugin-react - ESLint rules for react
- eslint-plugin-lodash - ESLint rules for lodash (
underscore
is forbidden) - eslint-plugin-import - ESLint plugin with rules that help validate proper imports
Installation
Nodejs
npm install --save-dev eslint-config-topcoder
Configure .eslintrc
{
"extends": "eslint-config-topcoder/nodejs"
}
Nodejs + babel
npm install --save-dev eslint-config-topcoder eslint-plugin-babel@^4.0.0 babel-eslint@^7.1.1
Configure .eslintrc
{
"extends": "eslint-config-topcoder/nodejs-babel"
}
React
npm install --save-dev eslint-config-topcoder eslint-plugin-babel@^4.0.0 eslint-plugin-react@^6.8.0 babel-eslint@^7.1.1
Configure .eslintrc
{
"extends": "eslint-config-topcoder/react"
}
Add scripts to package.json
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
}
Run scripts with -s
flag
npm run lint -s
npm run lint:fix -s
General notes
- It's not allowed to disable rules. The purpose of this config is to keep consistent styles in all topcoder projects.
However some exception are allowed. You can disable rules in configuration/setup files.
Example:app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars res.json({ error: err.message }); });
ExpressJS requires to create a function with 4 parameters for error handling, but it may happen that we don't use some parameters.
Following example is not allowed, becausesocket
can be removed.io.on('connection', (socket) => { // eslint-disable-line no-unused-vars winston.info('socket connection established'); });
magic-numbers
Following magic numbers are allowed:-1
,0
,1
.
In some cases you can disable this rule:
Math computations
const avg = (foo + bar) / 2; // eslint-disable-line magic-numbers
Unit tests
Unit tests usually contain hard-coded data, andmagic-numbers
can be problematic.
In following cases it's not allowed to disable magic-numbers
badres.status(400); res.json({error: 'some validation error'});
good
import HttpStatus from 'http-status'; res.status(HttpStatus.BAD_REQUEST); res.json({error: 'some validation error'});
bad
if (foo.status === 1) { }
good
const ACTIVE_STATUS = 1; if (foo.status === ACTIVE_STATUS) { }
You should disable only using inline commands:
bad (all rules are disabled)// eslint-disable-line
good (only 1 rule is disabled)
// eslint-disable-line magic-numbers
Exceptions for
import/no-unresolved
It's allowed to add exceptions if you use aliases (mostly React).
For example:'import/no-unresolved': [2, { ignore: ['^components/', '^containers/', '^services/', '^layouts/'] }]
Config for unit tests
If test
directory contains unit tests, you should create test/.eslintrc
and extend the base config.
Example:
{
"extends" : "../.eslintrc",
"env" : {
"mocha" : true
},
"globals": {
"expect": true,
},
"rules": {
// allow chai asserts like `expect(foo).to.be.true`
"no-unused-expressions": 0,
"no-magic-numbers": 0,
"max-lines": 0,
}
}
Contributors
- lsentkiewicz - Łukasz Sentkiewicz @Sky_
License
MIT