Code formatting in the JS Dev Environment
As part of the JS Dev Environment, you have two packages that do stuff to your code as you write and save your JS.
They are: eslint and prettier.
In this post I’ll talk about the prettier package.
What is prettier
Prettier is a JS library that does nothing but format your code to be consistent. All blog posts that describe it (here’s one from the official docs) state it is opinionated in its code formatting. That is, it has a set of rules that it follows and makes sure you do not break them by formatting your code in the event you do.
As an example, in the JS Dev Environment (JSDE from now on), I’ve set a rule in the .prettierrc.json file that our .js files should have semi colons at the end of every statement.
If I forget to add this punctuation mark at the end, on save, the prettier package will add those.
The way I set this up is threefold:
The package.json file contains the prettier source, so it will be installed when you do an install.
The .prettierrc.json file at the root of the project is there.
I’ve installed the extension (and recommended you do so) in my VS Code app.
All of these work together to make our code auto-format so we can focus on other things.
Getting this to work
Once the extension is installed into VS Code and, for each project you’ve installed Prettier into it, you can use it to format the document on demand and when you save.
To format the doc on demand, you open the command palette (command / ctrl + shift + p) and choose “Format Document”.
If you haven’t chosen a default formatter in the settings then you’ll do so here. Choose “Prettier” as your default formatter.
You then need to go into the settings of VSCode (click command/ctrl + , or choose “Settings” from the command palette). Search for “save” settings and choose the settings that make sense to you. I’ve chosen:
Editor: Format on Save
Files: Auto Save: after delay
Close this Settings file. Then make some changes to your code (remove some semi-colons) and save the file.
The semi colons should be added back.
Changing the rules
I picked some formatting rules that would make the most sense for us. Here’s the current settings:
{
"arrowParens": "always",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 150,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false,
"parser": "babel"
}
But please find other formatting rules that you’d like to have for your projects. (Note, if you do change these it’s per project; any new use of the JSDE template puts the settings back to what’s in that .prettierrc.json file)
In one of your projects test this out and change something:
turn ‘semi’ to false
turn ‘singleQuote’ to true
and so on.
Change them and test them out.
Here’s a complete list of rules if you want to do more and explanations of each of the above rules or options: https://prettier.io/docs/en/options.html
Some of them are deprecated, some refer to React stuff. So most of it might be not necessary.
What this doesn’t do
This extension / plugin doesn’t catch bugs. It only deals with your formatting. In another post we’ll look at the eslint file that catches the bugs.