Angular-cli : Using ng lint

54,038

Solution 1

Updated answer for Angular CLI v6.x, 7.x, 8.x:

ng lint <project-name> --fix

where <project-name> is "name:" from package.json

-- answer for Angular CLI v1.x --

ng lint -fix

-- Original answer below --

To have tslint autofix many errors run the following in the root of your code. Obviously it can only autofix simpler issues like let -> const, "" -> ' etc.

npx tslint src/**/*.ts --fix

Yesterday I did this to auto-fix hundreds of let -> const issues in our fairly large code bases. Just reviewing the changes before committing took long enough, manually fixing them all would have taken over a day.

Solution 2

For Angular 6.0+ you can run ng lint with autofix like so:

ng lint <project> --fix

where <project> is the name you gave to your project when running ng new.

Learn more here: https://github.com/angular/angular-cli/wiki/lint

Solution 3

The functionality you are asking about is partially available these days in VS Code using the TSLint extension which does support Auto Fixing for some (but not all) of the default TSLint warnings.

I've been using it for a few weeks now and I find it quite helpful.

Solution 4

Apparently, Angular 6.0 and newer supports this functionality natively.


For pre-Angular 6.0:

ng lint runs the tslint (TypeScript linter) which just prints out linting errors. AFAIK, it doesn't directly allow fixing found problems (as of Nov 16)

There are projects as tslint-fix which aim for fixing a set of so-called auto-fixable problems.

Share:
54,038
Ben Winding
Author by

Ben Winding

Updated on July 25, 2020

Comments

  • Ben Winding
    Ben Winding almost 4 years

    I'm aware the angular-cli uses codelyzer which uses lint.js

    When using the command: ng lint

    Can it automatically fix formatting? or will it only notify of formatting errors?

    ng lint --help outputs all help commands for the angular-cli.