To begin with, clone the repo you want to fix and create a new branch: feature/eslint or name it as you find appropriate.
Following are the steps to introduce ESLint to LoopBack repositories:
To make reviews easier, best practice is to send three separate commits:
Add ESLint infrastructure
Refer to this commit for an example.
- Remove all
jshintand/orjscsfiles, i.e..jshintrc,.jscsrcor.jshintignorefiles. - If the repo:
- Is not using Grunt, install
eslintas adevDependenciesvianpm install eslint --save-dev. - Is using Grunt and depends on
grunt-jshint, replace it withgrunt-eslint(no need to installeslintas it’s already installed as a dep fromgrunt-eslint).
- Is not using Grunt, install
- Update
devDependenciesby installingeslint-config-loopback.- Run
npm install --save-dev eslint-config-loopback@latestto ensure the latest version is installed.
- Run
- In
package.json’sdevDependenciessection, seteslintversion to^2.13.1.- Please refrain from using version
3.x.xuntil LoopBack drops support for versions of Node < 4. - See ESLint requires Node.js 4 or higher
- Please refrain from using version
-
Add
.eslintrcfile with following content:{ "extends": "loopback" } - Add
.eslintignorefile as well, if applicable. - If the repository is using
Grunt, update theGruntfile.jsto useeslint(refer to the example commit). - Scan code base for any occurances of
jshint/jscsand replace witheslintwhere appropriate. NOTE: Mac users rungrep -r jshint <dir/file> <dir/file> ...to find occurences of wordjshintin a dir/file. - Modify
package.jsonand/or Gruntfile to ensure that linter (eslint) is run after the tests pass. This typically means adding a"posttest": "eslint"script. If the original package contains apretestscript to run the linter, then remove thepretestscript in favor ofposttest.
Commit with message Add eslint infrastructure.
Auto-update by eslint –fix
Once you set up eslint infrastructure, the following steps automatically will eliminate most linting errors, such as comma-dangle errors:
- In a command terminal, go to repo and run following command:
./node_modules/.bin/eslint --fix .
Above command will auto-fix linting errors as per linter rules and will also show the errors it didn’t fix.
Commit these changes with message: Auto-update by eslint --fix.
Fix linting errors
Note:
When fixing errors, DO NOT change the original code because you might break things and it makes it harder for us to review! If you must change logic, do it in a separate PR. Disclaimer: this can be a tedious task.
-
From terminal, go to repo and run following command:
./node_modules/.bin/eslint .it will display list of errors with a line-number that needs fixup. Take a note of number of errors reported -
If you see a high number of
max-lenviolation, then add a custommax-lensettings to.eslintrcas follows:
{
"extends": "loopback",
"rules": {
"max-len": ["error", 90, 4, {
"ignoreComments": true,
"ignoreUrls": true,
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
}]
}
}
Then run eslint as in previous step.
-
Try different values (90, 100, 110, 120) and find the lowest one where the number of failing lines is still reasonable. For example, in
strong-remoting,eslintwas reporting 76 errors formax-lenof100and changing it to90was reporting78errors, thus lowest number90would make sense to set as max line length. -
Using
'use strict';may cause this error:
Uncaught SyntaxError: In strict mode code, functions can only
be declared at top level or immediately within another function
- Cause: function is declared inside a If/switch…etc block.
- Solution: Move the function outside the block. For example:
function funcName(parameter) {
}
switch() {
case 1: function() {
funcName (parameter);
}
}
- Go to the files and fix errors manually and commit.
Push the commit with message: Fix linting errors
That’s it, open a PR for review!