Starting...
This lab is to help our codes to standardize automatically. As you have experienced a lot, group coding is very difficult to be unified in one specific style. In this lab, we will explore how to make one style code with very efficient or effective libraries, prettier and lint for node js.
Progressing...
Prettier, link, is great for matching our coding style especially indent with tab or space, how many line and so on. The first step I took is to install prettier and put simple scripts in package.json, "prettier-fix": "prettier --write \"./**/*.{md,js,json}\"", "prettier": "prettier --check \"./**/*.{md,js,json}\"". I just run the script and I realized that there were a lot of prettier issues like indent, different ending style, tab, space and so on. Luckily, these scripts were very helpful since it provided auto editing features as well so I did not have to modify following the rules manually.
Eslint, link, is also great tool for more small mistakes of developers. It is more complicated to install since there are many plugin to adopt. For example, I chose airbnb's rules for it, https://github.com/airbnb/javascript. To be honest, I don't like it for a small project, but I believe for a big one, it will be very helpful because there are so many restrictions like below. I had to add a lot of exceptions into eslintrc.js and in real development world, I should not.
I think it is more powerful than prettier. Because of it, I could modify small mistakes like useless escape, strong equal and so on and it didn't provide auto-modification feature since the error is more complicated so I spent a lot of time for it.
VSCode, one of famous IDE, also provides Prettier - Code formatter nad Eslint as extensions. Once I install them, they also provide a guide to set up eslint and prettier. So, I set up the configs in .vscode folder.
Finally, I set the pre-hook for commit since any developer can forget to run the prettier or eslint script. For this feature, I install husky and lint-staged.
Comments
Post a Comment