Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-cli : Using ng lint

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.

like image 570
Ben Winding Avatar asked Nov 02 '16 01:11

Ben Winding


People also ask

How do you fix ng lint issues?

Using the command ng lint --fix is correct and should fix the indentation. I played around with tslint in a new Angular project, and while it did fix e.g. quotemarks, it does not fix the indentation for some reason (even though it should have a fixer).

Is ng lint deprecated?

ng lint. TSLint is deprecated and the CLI now warns us, and offers to try ESLint as an alternative, with angular-eslint as an alternative for codelyzer.

What is lint error in angular?

Linting is the process of running a program that analyses your code for programmatic and stylistic errors. A Linting tool, or a linter, checks any potential errors in your code such as syntax errors, incorrectly spelled variable names, and many more. This can save time and help you write better code.

What is TSLint file in angular?

tslint is a package that parses your Typescript code looking for points that 'break the best practice rules'. It comes with a pre-configured set of rules for best practices in Typescript in general. You don't have to do something to install it.


2 Answers

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.

like image 123
Paul Lockwood Avatar answered Sep 20 '22 11:09

Paul Lockwood


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

like image 29
David Castillo Avatar answered Sep 20 '22 11:09

David Castillo