Angular project for setting up ESLint + Prettier.
Below command will run the project:
npm start
Add Prettier to the project as dev dependency:
npm install --save-dev --save-exact prettier
Create a Prettier configuration file .prettierrc
to control the Prettier configurations at project level.
{
"singleQuote": true,
"bracketSameLine": true,
"endOfLine": "auto",
"overrides": [
{
"files": ["*.html", "*.ts"],
"options": {
"printWidth": 120
}
},
{
"files": "*.html",
"options": {
"parser": "html"
}
},
{
"files": "*.component.html",
"options": {
"parser": "angular"
}
}
]
}
Here, we are updating some of the few settings:
singleQuotes
: Setting the totrue
to follow JS recommendation of using single quotes for string.bracketSameLine
: Setting the totrue
to make end of closing brackets appearing on the same line.endOfLine
: Setting the toauto
to make the end of line to work as per environment.overrides
: Overriding settings for.html
and.ts
fileprintWidth
size to120
as SonarLint allows 120.
Read more from here Options Prettier.
Create a Prettier ignore file .prettierignore
to control which file to avoid for formatting.
package.json
package-lock.json
dist
/.angular/cache
lint-*
Add a prettier command to run for entire project to format all of the files at once, this command can be handy to use in order to format the entire project.
{
"scripts": {
"prettier:write": "prettier --config .prettierrc --write .",
"prettier:check": "prettier --config .prettierrc --check ."
}
}
Understand the philosophy of formatting with ESLint + Prettier from: Formatting Philosophy.
These 2 packages are required for adding Prettier rule to ESLint:
- ESLint Config Prettier: https://github.com/prettier/eslint-config-prettier (personally recommended as it keeps linting and formatting as separate responsibilities)
- ESLint Plugin Prettier: https://github.com/prettier/eslint-plugin-prettier (runs prettier within ESLint so you get feedback on violations from prettier itself rather
Add eslint-config-prettier
to project:
npm install --save-dev eslint-config-prettier
Add eslint-plugin-prettier
and prettier
to project:
npm install --save-dev eslint-plugin-prettier
npm install --save-dev --save-exact prettier
Add Prettier rules to .eslintrc.json
:
{
"plugins": [
"prettier"
],
"overrides": [
{
"files": [
"*.ts"
],
"extends": [
...
"plugin:prettier/recommended"
],
},
{
"files": [
"*.html"
],
"extends": [
...
"plugin:prettier/recommended"
],
}
]
}
Here, we are adding the prettier rule to the extends
array for .ts
and .html
files.