Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort imports alphabetically for TypeScript files automatically in WebStorm / PhpStorm?

I use tslint with default config tslint:recommended and I want to adapt as less rules as possible.

Certain rules enforce that imports should be alphabetized:

src/core/task/TaskMockDecorator.ts[2, 1]: Import sources within a group must be alphabetized.
src/core/task/taskRunner.ts[1, 16]: Named imports must be alphabetized.

I don't want to sort the imports alphabetically by myself. Before I disable that linting: Can WebStorm / PhpStorm do that for me?

like image 526
k0pernikus Avatar asked Dec 04 '16 17:12

k0pernikus


3 Answers

In WebStorm and Rider there is default keymap action to Optimize Imports, under CTRL+ALT+O, I suppose that it exist in PhpStorm as well.

Screenshot from JetBrains Rider settings with Optimize Imports keymap shown

like image 70
tometchy Avatar answered Sep 18 '22 09:09

tometchy


According to webstorm: https://www.jetbrains.com/help/webstorm/tslint.html

With WebStorm, you can fix some of the issues reported by TSLint > automatically.

To fix a specific error, place the cursor at the highlighted code, press ⌥⏎, and then choose TSLint: fix current error from the pop-up menu. To fix all the issues detected in the file, choose TSLint: fix current file.

like image 29
kenianbei Avatar answered Sep 21 '22 09:09

kenianbei


As of TSLint 4.0, the ordered-imports rule features automatic fixing of violations. You can run it through the CLI with the --fix flag.

like image 26
Adi Dahiya Avatar answered Sep 21 '22 09:09

Adi Dahiya