Linting & Fixing In Javascript

As javascript matures there have been a proliferation of linting tools that can help improve the state of your codebase. Linting is the process of running a program that will analyze code for potential errors or stylistic issues.

Our favorite linter and fixer respectively are eslint and prettier. We’re big fans of typescript and one of our previous favorite linters tslint is being deprecated in favor of eslint.

Eslint allows developers to create their own set of rules to enforce and there are a large set of pre-existing rules that you can use out of the box to create a set of standards for your codebase. Let’s see how you can quickly get started with eslint:

npm install eslint --save-dev
eslint --init

Then, if you have a javascript file you can run eslint on that file:

eslint myFile.js

Prettier is an opinionated code formatter and can ensure your code is clean and consistent. Since prettier is technically not a linter, we’re a fan of using both on a codebase. Let’s see how to get quickly started with Prettier

npm install --save-dev --save-exact prettier

Prettier integrates with editors so you can have it run on file save, but you can also use the cli to perform formatting changes:

prettier --single-quote --trailing-comma es6 --write "{app,__{tests,mocks}__}/**/*.js"

Instagram Post