Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 19, 2022 07:48 pm GMT

Configure Prettier and ESLint with Angular

Everyone wants to write code in a fast bug-free way without thinking about its style most of the time. Thats why in this post I will talk about configuring ESLint and Prettier in an Angular project.

How does ESLint help?

By statically analyzing our code, ESLint can find problems and also suggest us fixes for them. And it can do better than that, it can fix our code automatically (who doesnt want that?).

Install and configure ESLint

In this section, I will explain how to install ESLint in an Angular project and also configure it to better align with the Angular style guide and community standards.

Open the terminal and install ESLint schematics using this command:

ng add @angular-eslint/schematics

That was it. Now we have ESLint installed and also configured thanks to **ng add** command provided by the Angular-ESLint team.
Example error and how ESLint helps to fix it:

ESLint error about Angular input binding alias

ESLint quick fix for Angular Input aliasing

We can also run this command in terminal:
ng lint --fix
to fix all the fixable bugs in the project.

Install and configure Prettier

Even if we have ESLint watching our code for bugs, we also need a tool to better style and format it. Thats where Prettier comes into play.

Prettier is an opinionated code formatter that helps us beautify code in a standardized way every time we save the code.

Open terminal and type:
npm install prettier --save-dev
or if youre using yarn :
yarn add prettier -D

Then we need to add .prettierrc.json and .prettierignore files in our root project directory.

Inside .prettierignore its better to add whatever we have inside .gitignore file.

Then we can run this command inside our project to format it:
npx prettier --write .

Inside .prettierrc.json we can change the default settings by overriding them.

The settings I use most of the time are this:

{  "tabWidth": 2,  "useTabs": false,  "singleQuote": true,  "semi": true,  "bracketSpacing": true,  "arrowParens": "avoid",  "trailingComma": "es5",  "bracketSameLine": true,  "printWidth": 80}

Thats it about Prettier. But we are not finished.

There are times where ESLint and Prettier have different opinions about code formatting and style. Thats why we need to tweak this part. More info here.

Configure Prettier to be used as an ESLint plugin

For ESLint and Prettier to play well together, we need to run Prettier as an ESLint plugin. This way we can just call ng lint --fix and ESLint will fix bugs but also format the code.

Open terminal and type:

npm install prettier-eslint eslint-config-prettier eslint-plugin-prettier  save-dev

or if you're using yarn:

yarn add prettier-eslint eslint-config-prettier eslint-plugin-prettier -D

Now we need to edit the .eslintrc.json file to include the prettier plugin.

{  "root": true,  "overrides": [    {      "files": ["*.ts"],      "extends": [        ...        "plugin:prettier/recommended"      ],    },    // NOTE: WE ARE NOT APPLYING PRETTIER IN THIS OVERRIDE, ONLY @ANGULAR-ESLINT/TEMPLATE    {      "files": ["*.html"],      "extends": ["plugin:@angular-eslint/template/recommended"],      "rules": {}    },    // NOTE: WE ARE NOT APPLYING @ANGULAR-ESLINT/TEMPLATE IN THIS OVERRIDE, ONLY PRETTIER    {      "files": ["*.html"],      "excludedFiles": ["*inline-template-*.component.html"],      "extends": ["plugin:prettier/recommended"],      "rules": {        // NOTE: WE ARE OVERRIDING THE DEFAULT CONFIG TO ALWAYS SET THE PARSER TO ANGULAR (SEE BELOW)        "prettier/prettier": ["error", { "parser": "angular" }]      }    }  ]}

VSCode and Webstorm shortucts

That was it. Were done with the configuration part.

After we edit a file, we want to format it and then save. Thats what we will configure now for both VS Code and Webstorm.

First make sure you have ESLint and Prettier plugin installed. WebStorm has support of of the box for both.

For VS Code we need to add this lines to settings.json:

{  "[html]": {    "editor.defaultFormatter": "esbenp.prettier-vscode",    "editor.codeActionsOnSave": {      "source.fixAll.eslint": true    },    "editor.formatOnSave": false  },  "[typescript]": {    "editor.defaultFormatter": "dbaeumer.vscode-eslint",    "editor.codeActionsOnSave": {      "source.fixAll.eslint": true    },    "editor.formatOnSave": false  },}

For Webstorm:
We need to check: Run eslint --fix on Actions On Save settings page:
Webstorm Fix File on save

That was it. Thank you for reading! See you on the next one.

Here you can find the steps summed up: Angular ESLint & Prettier Configuration


Original Link: https://dev.to/eneajaho/configure-prettier-and-eslint-with-angular-526c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To