简体   繁体   中英

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?

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.

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.

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.

JetBrains Rider 设置的屏幕截图,其中显示了优化导入键盘映射

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM