Auto fix TSLint Warnings
Asked Answered
L

6

83
    [64, 1]: space indentation expected
    [15, 27]: Missing semicolon
    [109, 36]: missing whitespace
    [111, 24]: missing whitespace
    [70, 1]: Consecutive blank lines are forbidden

I keep getting warnings like these from TSLint. There are huge amount of warnings, and it will be very difficult to fix it manually.

I was looking for a way which can auto-fix most of the warnings.

Longhair answered 30/6, 2017 at 7:42 Comment(1)
this answer may be usefull: #36448410Hasen
F
146

You can use the --fix option of TSLint to automatically fix most warnings. This might look something like this in a common use case:

tslint --fix -c ./config/tslint.json 'src/**/*{.ts,.tsx}'

Keep in mind that this will overwrite your source code. While this is safe 99.9% of the time, I recommend the following workflow:

  1. Commit the changes you have made to your code
  2. Run TSLint with the --fix flag like above
  3. Quickly review the changes TSLint has made
  4. Make a new commit with these changes, or simply amend them to your previous commit

This way, you'll never be taken surprise by a rogue autocorrection gone wrong.

Frankfurter answered 30/6, 2017 at 15:42 Comment(2)
If you are using Angular CLI, this is run using ng lint --fixDalenedalenna
@Dalenedalenna or node_modules\.bin\tslint --fix -c ./tslint.json 'src/**/*{.ts,.tsx}'Sisk
L
25
tslint --fix --project ./tsconfig.json

This is auto fix all Error is root folder

Longhair answered 26/10, 2017 at 7:56 Comment(0)
T
14

With @angular/cli you can use ng lint --fix

Tyro answered 4/8, 2020 at 8:53 Comment(0)
I
9

May be this could help some one looking for autoFix on save!

we can make tslint warnings to get autofixed on save. To do this, go to tslint.json file and add the below settings. [Note: This setting works with latest TSLint and not the deprecated one].

  "source.fixAll.tslint": true

After updating this, go to any file and try giving some spaces and it shows tslint warning message as "trailing whitespace" and when you save (Ctrl + S), this warning will get disappear. Though it appears manually, we usually tend to try saving the file once edited and by that time it will get fixed automatically.

Happy coding!

Incitement answered 31/5, 2020 at 7:35 Comment(1)
Amazing! One thing: you have to add the line to settings.json and not tslint.json. Full code to add: "editor.codeActionsOnSave": { "source.fixAll.tslint": true }Disjunction
S
4

If you using webpack . You can use tslint-loader:

Add this to webpack module rules:

{
    test: /\.ts$/,
    loader: 'tslint-loader',
    enforce: 'pre',
    options: {
      fix: true
    }
  }

Read more here: tslint-loader

Sammie answered 4/5, 2018 at 0:54 Comment(0)
C
4

If you using IntelliJ Idea or WebStrom, then you can create File Watcher in Settings > Tools by the following configuration:

enter image description here

On save (Ctrl + s), your current file will be fixed.

Communist answered 21/2, 2019 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.