40

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.

Ben Winding
  • 10,208
  • 4
  • 80
  • 67

4 Answers4

65

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.

Paul Lockwood
  • 4,283
  • 1
  • 24
  • 19
  • 1
    a fix that --fix options cannot do by itself is no-console. Sad because it will be useful to conditionally delete debug console log. – netalex Jul 22 '19 at 07:45
  • If you have changed your project name and or the name in the pkg.json and this does not work, check your angular.json file and ensure the name in the pkg.json is listed under projects. where is listed under "projects" in your angular.json – sirBassetDeHound Nov 18 '20 at 17:38
12

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

David Castillo
  • 4,266
  • 4
  • 23
  • 25
5

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.

zejuel
  • 113
  • 7
3

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.

Yuri
  • 4,254
  • 1
  • 29
  • 46