ESLint
When developing a JavaScript project with multiple people (or even solo), using ESLint lets you keep your coding rules consistent. Running ESLint in CI as well makes it easier to catch mistakes as an early step before review.
eslint-disable-line
ESLint lets you disable rules for a specific line using an inline comment.
When developing locally, you might use eslint-disable-line here and there just to check something, but if you push code while it's still in there, code that doesn't actually conform to the coding rules can slip in unintentionally.
For example, even with a rule that forbids console, ESLint can't detect code like the following: (if anyone knows a way to catch this, please let me know) ↓ see the addendum below
console.log('hello') // eslint-disable-line
Detecting It in CircleCI
To check whether eslint-disable-line is being used, run the following command in CircleCI.
- run:
name: Search eslint-disable-line
command: |
git grep --heading --break -n -e 'eslint-disable-line' -- '*.js' '*.vue' || status=$?
if [ -z "$status" ]; then exit 1; fi
git grep --heading --break -n -e 'eslint-disable-line' -- '*.js' '*.vue'
This is a command that searches js files (and vue files in this example — other patterns can be specified too) for eslint-disable-line; feel free to adjust the options to your liking.
Regarding the trailing || status=$?: the git-grep command above returns a status of 0 if a matching line is found, and a status of 1 if no matching line is found.
In other words, if a matching line is found, $status doesn't get a value set — so the command below exits with exit 1 if the value isn't set.
if [ -z "$status" ]; then exit 1; fi
This lets us detect eslint-disable-line in CircleCI.
Addendum
Looking at the ESLint CLI documentation, I found an option called --no-inline-config.
With this option, even places where rules are changed inline get evaluated against the original rules, so passing this option when running ESLint in CI should achieve the same goal.
It also seems possible to set up a config that forbids inline rule changes altogether, so if needed, it might be worth setting that up too.