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.
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).
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With