Giter VIP home page Giter VIP logo

angular-eslint's Introduction

Angular ESLint

Monorepo for all the tooling which enables ESLint to lint Angular projects

Build Status NPM Version GitHub license NPM Downloads Codecov Commitizen friendly


This project is made possible thanks to the continued hard work going into https://github.com/typescript-eslint/typescript-eslint, and brilliant work on the original TSLint rule implementations in https://github.com/mgechev/codelyzer.

Feel free to begin playing with the tooling in your own projects and submit PRs with missing rules and bug fixes.

We would also be very grateful for documentation PRs!


Packages included in this project

Please follow the links below for the packages you care about.

  • @angular-eslint/builder - An Angular CLI Builder which is used to execute ESLint on your Angular projects using standard commands such as ng lint

  • @angular-eslint/eslint-plugin - An ESLint-specific plugin that contains rules which are specific to Angular projects. It can be combined with any other ESLint plugins in the normal way.

  • @angular-eslint/template-parser - An ESLint-specific parser which leverages the @angular/compiler to allow for custom ESLint rules to be written which assert things about your Angular templates.

  • @angular-eslint/eslint-plugin-template - An ESLint-specific plugin which, when used in conjunction with @angular-eslint/template-parser, allows for Angular template-specific linting rules to run.

  • @angular-eslint/schematics - Schematics which are used to add and update configuration files which are relevant for running ESLint on an Angular workspace.


Migrating from Codelyzer and TSLint

We have some work in progress tooling to make this as automated as possible, but the reality is it will always be somewhat project-specific as to how much work will be involved in the migration.

Step 1 - Add relevant dependencies

The first step is to run the schematic to add @angular-eslint to your project:

ng add @angular-eslint/schematics

This will handle installing the latest version of all the relevant packages for you and adding them to the devDependencies of your package.json.

Step 2 - Add new ESLint-related configuration

The next thing to do is consider which "project" you want to migrate to use ESLint. If you have a single application in your workspace you will likely have just a single entry in the projects configuration object within your angular.json file. If you have a projects/ directory in your workspace, you will have multiple entires in your projects configuration and you will need to chose which one you want to migrate using the add-config-to-project schematic.

You can run it like so:

ng g @angular-eslint/schematics:add-config-to-project {{YOUR_PROJECT_NAME_GOES_HERE}}

The schematic will do the following for you:

  • CREATE a .eslintrc.json at the root of the specific project which extends from the root config (if you do not already have a root config, it will also add one automatically for you)
  • UPDATE the project's architect configuration in the angular.json to add a new target called eslint

You can run the new target like so:

npx ng run {{YOUR_PROJECT_NAME_GOES_HERE}}:eslint

This command uses the Angular CLI's standard tooling to invoke the builder from this project and ultimately run ESLint on the files which are relevant to your chosen project.

As this stage you have both TSLint and ESLint configured as different targets for your project - we have not removed or changed TSLint in any way.

Step 3 - Use the new ESLint configuration files to match your original TSLint configuration, or change it however you would like

Currently this is a manual step, but we hope to create a schematic to assist with this process soon. See Notes on ESLint Configuration below for some more info.

Step 4 - Remove TSLint configuration and use only ESLint

Once you are happy with your ESLint setup, you simply need to remove the project specific tslint.json and the lint configuration block within the project's architect configuration block in your angular.json.

Then you need to rename your eslint target to just lint and it will work as you are used to via:

npx ng run {{YOUR_PROJECT_NAME_GOES_HERE}}:lint

OR

npx ng lint {{YOUR_PROJECT_NAME_GOES_HERE}}

Notes on ESLint Configuration

It's important to understand up front that using Angular with ESLint is actually an advanced/complex use-case because of the nature of the files involved:

  • Angular projects use TypeScript files for source code
  • Angular projects use a custom/extended form of HTML for templates (be they inline or external HTML files)

The thing is: ESLint understands neither of these things out of the box.

Fortunately, however, ESLint has clearly defined points of extensibility that we can leverage to make this all work.

For detailed information about ESLint plugins, parsers etc please review the official ESLint documentation: https://eslint.org

The key principal of our configuration required for Angular projects is that we need to run different blocks of configuration for different file types/extensions. In other words, we don't want the same rules to apply on TypeScript files that we do on HTML/inline-templates.

Therefore, the critical part of our configuration is the "overrides" array:

{
  "overrides": [
    /**
     * -----------------------------------------------------
     * TYPESCRIPT FILES (COMPONENTS, SERVICES ETC) (.ts)
     * -----------------------------------------------------
     */
    {
      "files": ["*.ts"],
      // ... config specific to TypeScript files
    },

    /**
     * -----------------------------------------------------
     * COMPONENT TEMPLATES
     * -----------------------------------------------------
     */
    {
      "files": ["*.component.html"],
      // ... config specific to Angular Component templates
    },

    /**
     * -----------------------------------------------------
     * EXTRACT INLINE TEMPLATES (from within .component.ts)
     * -----------------------------------------------------
     */
    {
      "files": ["*.component.ts"],
      // ... applies a special processor to extract the template
      "extends": ["plugin:@angular-eslint/template/process-inline-templates"]
    }
  ]
}

By setting up our config in this way, we have complete control over what rules etc apply to what file types and our separate concerns remain clearer and easier to maintain.

For a full reference configuration example check out the full Angular CLI integration test located within this monorepo. Check out the relevant configuration files:

If you are looking for general help in migrating specific rules from TSLint to ESLint, you can check out this project: https://github.com/typescript-eslint/tslint-to-eslint-config


Linting HTML files and inline-templates with the VSCode extension for ESLint

If you use vscode-eslint, and want to lint HTML files and inline-templates on your Angular Components, you will need to make sure you add the following to your VSCode settings.json:

// ... more config

"eslint.options": {
  "extensions": [".ts", ".html"]
},

// ... more config

"eslint.validate": [
  "javascript",
  "javascriptreact",
  "typescript",
  "typescriptreact",
  "html"
],

// ... more config

Please see the following issue for more information: microsoft/vscode-eslint#922


Usage without Angular CLI Builder

If you're using this without the Angular CLI Builder don't forget to include .component.html as one of the file extensions when running the eslint CLI, otherwise templates will not be linted, e.g.:

eslint --ext .ts,.component.html

Rules List

โœ… = done
๐Ÿšง = work in progress

Functionality

Codelyzer rule Status
contextual-decorator
contextual-lifecycle โœ…
no-attribute-decorator โœ…
no-lifecycle-call โœ…
no-output-native โœ…
no-pipe-impure โœ…
prefer-on-push-component-change-detection โœ…
template-accessibility-alt-text
template-accessibility-elements-content
template-accessibility-label-for
template-accessibility-tabindex-no-positive โœ…
template-accessibility-table-scope
template-accessibility-valid-aria
template-banana-in-box โœ…
template-click-events-have-key-events
template-mouse-events-have-key-events
template-no-any
template-no-autofocus
template-no-distracting-elements
template-no-negated-async โœ…
use-injectable-provided-in โœ…
use-lifecycle-interface โœ…

Maintainability

Codelyzer rule Status
component-max-inline-declarations โœ…
no-conflicting-lifecycle โœ…
no-forward-ref โœ…
no-input-prefix โœ…
no-input-rename โœ…
no-output-on-prefix โœ…
no-output-rename โœ…
no-unused-css
prefer-output-readonly โœ…
relative-url-prefix โœ…
template-conditional-complexity
template-cyclomatic-complexity โœ…
template-i18n
template-no-call-expression โœ…
template-use-track-by-function
use-component-selector โœ…
use-component-view-encapsulation โœ…
use-pipe-decorator โœ…
use-pipe-transform-interface โœ…

Style

Codelyzer rule Status
angular-whitespace
component-class-suffix โœ…
component-selector โœ…
directive-class-suffix โœ…
directive-selector โœ…
import-destructuring-spacing
no-host-metadata-property โœ…
no-inputs-metadata-property โœ…
no-outputs-metadata-property โœ…
no-queries-metadata-property โœ…
pipe-prefix โœ…
prefer-inline-decorator

angular-eslint's People

Contributors

jameshenry avatar dependabot[bot] avatar sfabriece avatar wkoza avatar paulotokimatu avatar andrefilimono avatar gavinwu1991 avatar kuzivany avatar nzacca avatar alexanderfsp avatar dpraul avatar cammisuli avatar kimamula avatar qkevinto avatar lakmoore avatar mikematusz avatar rafaelss95 avatar rarmatei avatar rgunning avatar gebsh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.