Although eslint is not yet supported by angular (see https://github.com/mgechev/codelyzer/issues/763), there are things that can be done to completely switch from tslint to eslint.
I do not recommend that you try to run eslint directly inside an angular project. You still need to use ng cli to lint the project. It is also recommended to remove tslint from your project to avoid confusing the tools.
add eslint, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, prettier, @angular-eslint (follow instructions at https://github.com/angular-eslint/angular-eslint), and any other eslint plugin you want to use
create a .eslintrc.js at the root of your workspace. I recommend .js and not .json in order to be able to do like angular does with tslint and define app specific .eslintrc.js in the app director
For every project your have under anguler.json, update the "lint" configration as following:
"lint": {
"builder": "@angular-eslint/builder:lint",
"options": {
"eslintConfig": "apps/<>/.eslintrc.js",
- Remove tslint and its files.
.eslintrc.js at root level
/**
* We are using the .JS version of an ESLint config file here so that we can
* add lots of comments to better explain and document the setup.
*/
module.exports = {
/**
* See packages/eslint-plugin/src/configs/README.md
* for what this recommended config contains.
*/
ignorePatterns: ['**/*.js'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
'plugin:@angular-eslint/recommended',
],
plugins: ['@typescript-eslint'],
rules: {
// ORIGINAL tslint.json -> "directive-selector": [true, "attribute", "app", "camelCase"],
'@angular-eslint/directive-selector': [
'error',
{ type: 'attribute', prefix: 'hc', style: 'camelCase' },
],
// ORIGINAL tslint.json -> "component-selector": [true, "element", "app", "kebab-case"],
'@angular-eslint/component-selector': [
'error',
{ type: 'element', prefix: 'hc', style: 'kebab-case' },
],
},
overrides: [
/**
* This extra piece of configuration is only necessary if you make use of inline
* templates within Component metadata, e.g.:
*
* @Component({
* template: `<h1>Hello, World!</h1>`
* })
* ...
*
* It is not necessary if you only use .html files for templates.
*/
{
files: ['*.component.ts'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
plugins: ['@angular-eslint/template'],
processor: '@angular-eslint/template/extract-inline-html',
},
],
};
.eslintrc.js for an app
const _ = require('lodash');
const workspaceConfig = require('../../.eslintrc');
/**
* We are using the .JS version of an ESLint config file here so that we can
* add lots of comments to better explain and document the setup.
*/
module.exports = _.merge({}, workspaceConfig, {
rules: {
// ORIGINAL tslint.json -> "directive-selector": [true, "attribute", "app", "camelCase"],
'@angular-eslint/directive-selector': [
'error',
{ type: 'attribute', prefix: 'shop', style: 'camelCase' },
],
// ORIGINAL tslint.json -> "component-selector": [true, "element", "app", "kebab-case"],
'@angular-eslint/component-selector': [
'error',
{ type: 'element', prefix: 'shop', style: 'kebab-case' },
],
},
});
Linting
As mentioned above, just use ng lint :-). Do not run eslint directly.
I have a fully working repo using angular 9 and eslint, you can check it at https://github.com/abdes/happy-coding