Static Analysis Testing
This section focuses on the tools that help you eliminate bugs related to typos and incorrect types (e.g. providing a string to a function that expects a number).
Linting with ESLint
Linting is the process of analyzing code to flag errors, bugs, stylistic problems, and suspicious constructs. The basic idea to set up eslint
in particular is to
npm install --save-dev eslint
.In the root directory, include
.eslintrc
with all your configuration.Follow the docs to see what you can configure.
Run
npx eslint .
to analyze your code.
Here's a sample of what you can configure:
Pro tip: The key options for rules are error
, warn
, and off
.
Use
error
when you want to stop your build altogether. This is good for catching essential errors that you can't allow.Use
warn
when you're willing to allow some issues to pass by without disturbing your build. This is useful when you want to give a recommendation or your team is transitioning into eslint.Use
off
when you don't need any errors or warnings at all.
ESLint in VS Code
If you want to catch common ESLint issues in your code editor, you can download eslint
in VS Code.
This will automatically underline issues for you. You can even run ctrl + .
or cmd + .
when hovered you're on top of the error to tell the extension to fix the issue for you.
Pro tip: When you run ctrl + .
, you can also tell ESLint to ignore your issue. This will include an ignore code comment above the issue that ESLint will acknowledge. Example:
Note: Another option is to run npx eslint . --fix
. ESLint will fix as many issues as it can this way.
Pre-built ESLint configurations
ESLint has a ton of rules. If you want to piggyback off of the recommended configurations of others, you can use extends
in .eslintrc
.
Note: You can also pass multiple custom configurations, and each successive one overrides the previous.
Running eslint in scripts
To allow us to run our linter in terminal, we're going to include the following command in package.json
:
Note that we include --ignore-path .gitignore
because 99% of the time, our .gitignore
will include all the files and folders we don't need to lint (like build
or node_modules
).
Note: We can also create a .eslintignore
file as well.
Prettier
Running Prettier out of the box
We can easily run Prettier by doing the following:
npm install --save-dev prettier
.Include the following script:
prettier --ignore-path .gitignore --write \"**/*.+(js|json)\""
.Like ESLint,
--ignore-path
ignores files and folders in.gitignore
.--write
overwrites all formatting issues in.js
and.json
files.
Configuring Prettier
We can start our configuration by going to playground and setting up our preferred options.
Then we can simply copy-paste those options into a .pretterrc
file.
We end up with something like this:
Prettier in VS Code
To set up Prettier in VS Code, all we need to do is include this in our settings.json
:
Disabling ESLint rules made useless by Prettier
Some ESLint rules add a red underline to stylistic rules that Prettier also fixes. This creates redundancy. For example, if we put multiple semicolons like ;;;
at the end of a line, both ESLint and Prettier prohibit this.
In order to remove redundant ESLint rules, just npm install --save-dev eslint-config-prettier
.
Then include it at the end of your extends
in .prettierrc
:
Validate Code through Linter and Prettier
You can't force every developer in your team to use Prettier in their text editor. However, you can enforce verification in your project via a validate
script that
Checks Prettier format.
Runs ESLint to check for linting issues.
Runs build to test.
validate
basically halts the script if anything goes wrong with Prettier or ESLint or build.
In the case of check-format
, we receive an error and the script stops if Prettier catches formatting problems. The only way to then fix this issue would be to execute npm run format
. Bam! Now your project enforces formatting.
TypeScript
ESLint doesn't catch all our errors. Type errors don't get caught:
To solve this, we can install typescript
, which handles exactly this sort of thing.
npm install --save-dev typescript
.Write out our code according to TypeScript's specifications.
Include a
tsconfig.json
.npx tsc
(stands for TypeScript Compiler) to analyze code for errors.
Here's a basic configuration:
Now in our file, we should catch our errors:
Now we can include type checking in our validate
script:
Things to note:
Make sure to include
.ts
and.tsx
files for Prettier (and for Babel).If you're using Babel, make sure to install
@babel/preset-typescript
, so Babel knows how to compile TypeScript.Be sure to
@babel/preset-typescript
in yourpresets
array in.babelrc
.
Configure ESLint to check TypeScript
By default, ESLint only check .js
files, not .ts
files.
To fix this, we need to
Install more packages:
npm install --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
.Then we add the
--ext .js,.ts,.tsx
flag tonpm run lint
to include TypeScript.Finally, we configure
.eslintrc
to override how it specifically processes TypeScript files. (Think of it like special configuration inside.eslintrc
.)
Validating before Commit
It would be great if we could have our project run npm run validate
before any of our team commits code. Doing so ensures that all code passes our validation check before becoming part of the codebase. To do this, we use a pre-commit hook using husky
.
npm install --save-dev husky
. This will add scripts inside our project's.git/hooks
directory.Add a
.huskyrc
config file. Whenever yougit commit
, it will run the hooks scripts as well, which accesses.huskyrc
to tell it what to do.
So, in our case, we can tell Husky to run a pre-commit hook that runs npm run validate
:
Pro tip: Check out the .git/hooks
directory for all the hooks you can use with Husky. Pretty powerful stuff!
Adding auto-formatting
Right now, if Prettier fails the formatting check, we won't be able to commit. A solution we can implement is to Prettier format our code automatically.
To format our code automatically, we can just use lint-staged
.
npm install --save-dev lint-staged
.Create a
.lintstagedrc
file where you define how to handle different files.Update
pre-commit
hook in.huskyrc
to uselint-staged
command.
The pre-commit
is now the automated version of npm run validate
, where lint-staged
replaces our custom Prettier and ESLint checks.
Note: lint-staged
does even more. For example, it only checks staged files instead of every file.
BONUS: Run Validate in Parallel
All of our commands in npm run validate
don't impact each other, so it's nice to make them run in parallel, since it runs faster.
npm install --save-dev npm-run-all
.Change script to
"validate": "npm-run-all --parallel check-types check-format lint build"
.
Last updated