Skip to content

Instantly share code, notes, and snippets.

@eneajaho
Last active April 20, 2024 18:30
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save eneajaho/17bbcf71c44eabf56d404b028572b97b to your computer and use it in GitHub Desktop.
Save eneajaho/17bbcf71c44eabf56d404b028572b97b to your computer and use it in GitHub Desktop.
Angular ESLint & Prettier Configuration

Install Angular ESLint

ng add @angular-eslint/schematics

Install Prettier and Prettier-ESLint dependencies

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

ESLint configuration

Filename: .eslintrc.json

// https://github.com/angular-eslint/angular-eslint#notes-for-eslint-plugin-prettier-users
{
  "root": true,
  "ignorePatterns": ["projects/**/*"],
  "overrides": [
    {
      "files": ["*.ts"],
      "parserOptions": {
        "project": ["tsconfig.json", "e2e/tsconfig.json"],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates",
        "plugin:prettier/recommended"
      ],
      "rules": {
        "@angular-eslint/component-class-suffix": [
          "error",
          {
            "suffixes": ["Page", "Component"]
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ],
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ],
        "@typescript-eslint/member-ordering": 0,
        "@typescript-eslint/naming-convention": 0
      }
    },
    // 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" }]
      }
    }
  ]
}

Prettier Configuration

Filename: .prettierrc

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

Filename: .prettierignore

build
coverage
e2e
node_modules

angular.json config

"lint": {
  "builder": "@angular-eslint/builder:lint",
  "options": {
    "lintFilePatterns": [
      "src/**/*.ts",
      "src/**/*.html"
    ]
  }
},

VSCode extensions:

dbaeumer.vscode-eslint
esbenp.prettier-vscode

Add the following to your .vscode/settings.json file:

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

Webstorm settings

WebStorm settings - Actions on save

Add Fix Lint and Prettier errors command in package.json

"lint": "ng lint --fix"

@pacocom
Copy link

pacocom commented Jun 28, 2022

Please, can you add file date (config.md)?

@josileudo
Copy link

In my project, with "editor.defaultFormatter": "dbaeumer.vscode-eslint", it is not formatting automatically. Can you help me?

@ezhupa99
Copy link

ezhupa99 commented Mar 6, 2023

@josileudo Do you have ESLint installed? if so, do you have an .eslintrc.json on the root of your application?

@Grobouls
Copy link

Grobouls commented Jun 8, 2023

Nice work, thanks a lot !

@guikpeyes
Copy link

thanks

@fingust
Copy link

fingust commented Jul 6, 2023

Hello. I am getting the following error: An unhandled exception occurred: prettier.resolveConfig.sync is not a function. Any ideas why?

@ollie314
Copy link

ollie314 commented Jul 6, 2023

Hello. I am getting the following error: An unhandled exception occurred: prettier.resolveConfig.sync is not a function. Any ideas why?

Same issue here, the function seems removed from the 3.0.0
https://github.com/search?q=repo%3Aprettier%2Fprettier+prettier.resolveConfig.sync&type=code
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment