Introduction
This post is about configuring a linter, git hooks and auto-format on VS Code in order to improve our development workflow. This configuration can be used for any project, but in that particular case I’ll add specific linting rules that applly to our Alexa Skill’s code. In order to cover all the linting options and functionality we want to configure the linter on different points of our workflow:
- Create
npm run lint
to be run linter on demand. - Create git hook
precommit
to run our linter using husky and lint-staged - Create git hook
prepush
to run our test framework using husky
Linter
There are different methods and options to consider when we want to add linting to our projects:
- Prettier: that is a good option for automatically format our code (but at the same time is quite risky as it will decide for you eventually)
- Eslint: that is really configurable by using rule sets, and it also integrates really well with VS Code auto-format via eslint extension for VS Code.
I’m going to use eslint for that project
Add linter, git hooks and rules to our project
Alexa Skill could be written using NodeJS and the Standard JavaScript linting rules. I’ll show how to configure our project to add all the requirements mentioned above.
package.json
1 | { |
Add devDependencies:
eslint
,husky
,lint-staged
,eslint-config-standard
andeslint-plugins
Run this command:
npm i -D eslint husky lint-staged eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard
Add scripts:
test
,lint
,precommit
andprepush
1
2
3
4"test": "jest",
"lint": "eslint .",
"precommit": "lint-staged",
"prepush": "npm run test"Add lint-staged section. Apply to all
*.js
files that have been committed and run firstlynpm run lint
and thengit add
. Then before doing the git commit the linter is going to run. If the linter fails, thengit commit
will not be done.
.eslintrc
As part of eslint configuration we should add .eslintrc
file. For the specific scenario of Alexa Skill, that is the set of rules that better works for our team:
1 | { |
.eslintignore
In order to make sure we are not linting external files and libraries we should ignore node_modules
folder using .eslintignore
file:
1 | !.* |
VS Extensions
If we are using VS Code, we also can suggest or recommend some extensions which will help providing “live lintint” on the files opened by VS Code.
We can add under .vscode
folder, a file called extensions.json
with that object on it:
1 | { |
Then, when a new developer opens that project, will be suggested to install eslint
extension.
Note: Eslint VS Code extension will also use our
.eslintrc
rules when we use auto-formating on our files, applying as many rules as it can.VS Code Auto-Formatting shortcuts:
MacOS
: ⇧ + ⌥ + FWindows
: Shift + Alt + FLinux
: Control + Shift + I
Conclusion
This configuration is not only for Alexa Skill project, we use it in some of our JS, React and NodeJS projects and it really helps on improving our development workflow performance. Having that configuration in our project will help on:
- Running linter on demand ->
npm run lint
- Running linter before commit ->
git commit
- Running tests before push ->
git push
- Running linter on each file in live (using VS Code Extension) and being able to solve some issues with auto-formatting.