Having a standard set of rules for formatting and linting makes projects a little more sane to work with. Particularly when working with others in a project.
There are a number of opinionated ways to configure a project with linting and formatting, this is just mine for reference whenever I set up a new project or ask myself, "why the hell did I do this?"
There are two common toolings to use in projects to ensure projects follow common standards. Linting is to ensure that code follow standards to avoid bugs and other problems. Formatters are mostly opinionated styling of code for visual presentation.
Stylers are the only thing that should run to format project code. Linting can provide warnings and errors for code and it should be up to the developer to manually make the fixes to pass.
These are the common tools I use for a project:
- ESLint
- Prettier
- EditorConfig
- Commitlint
To ensure these run and format the project, I use the following:
- VSCode workspace settings
- Husky
ESLint, Prettier and EditorConfig have overlaps in rules. Some minor adjustments and additional packages need to be added to ensure they work together and don't step on each others toes.
The general logic goes as follows:
- Start with
.editorconfig. It's designed for use across any editor and IDE. Define all of the available configuration rules in the file. - Prettier is next. It inherits rules from
.editorconfig. Don't re-define the rules in prettier that are already defined in editorconfig. - ESLint is last. Since it has lint rules that overlap with Prettier,
add in the
eslint-config-prettierplugin to disable them. - Add additional rules and configurations into the
.vscode/settings.jsonfile. Main thing we want to do is ensure that Prettier is run on save for a file. - Define the recommended list of
.vscode/extensions.jsonto enhance the use of these various plugins and packages.
With the setup above, most of the linting and formatting should be managed automatically. If we add linting beyond the project into git commits, we need Husky.
We can also add additional sanity checks using Husky so we can
catch fools who don't install the VScode extensions or have
their linters or formatters working automatically.
lint-staged is useful for this.
- Set up commitlint.config.js.
Just the basic
@commitlint/config-conventionalis fine. - Add the commitlint action to the
commit-msghusky hook. - Setup a
.lintstagedrc.ymlfile for linting staged files. - Add lint-staged to the
pre-commithusky hook.