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. That’s why in this post I will talk about configuring ESLint and Prettier in an Angular project.

How does ESLint help?

By statically a…


This content originally appeared on DEV Community and was authored by Enea Jahollari

Everyone wants to write code in a fast bug-free way without thinking about its style most of the time. That’s 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 doesn’t 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. That’s 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 you’re using yarn :
yarn add prettier -D

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

Inside .prettierignore it’s 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
}

That’s it about Prettier. But we are not finished.

There are times where ESLint and Prettier have different opinions about code formatting and style. That’s 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. We’re done with the configuration part.

After we edit a file, we want to format it and then save. That’s 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


This content originally appeared on DEV Community and was authored by Enea Jahollari


Print Share Comment Cite Upload Translate Updates
APA

Enea Jahollari | Sciencx (2022-02-19T19:48:34+00:00) Configure Prettier and ESLint with Angular. Retrieved from https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/

MLA
" » Configure Prettier and ESLint with Angular." Enea Jahollari | Sciencx - Saturday February 19, 2022, https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/
HARVARD
Enea Jahollari | Sciencx Saturday February 19, 2022 » Configure Prettier and ESLint with Angular., viewed ,<https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/>
VANCOUVER
Enea Jahollari | Sciencx - » Configure Prettier and ESLint with Angular. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/
CHICAGO
" » Configure Prettier and ESLint with Angular." Enea Jahollari | Sciencx - Accessed . https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/
IEEE
" » Configure Prettier and ESLint with Angular." Enea Jahollari | Sciencx [Online]. Available: https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/. [Accessed: ]
rf:citation
» Configure Prettier and ESLint with Angular | Enea Jahollari | Sciencx | https://www.scien.cx/2022/02/19/configure-prettier-and-eslint-with-angular/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.