VS-Code: How to sort imports in specific order to go along with InteliJ
Asked Answered
B

2

11

I am working in a team which mostly uses InteliJ. There imports get automatically sorted after the path. So "@namespace" goes first then less nested imports "./" > "../../" and on the same level its alphabetical.

Until now I used the "source.organizeImports" setting from VSCode which sorted the imports for named and default imports. This cant be customized as stated in another SO question and is not listed here: https://code.visualstudio.com/docs/languages/typescript#_organize-imports

I tried the VSC extension vsc-organize-imports but that doesnt have that option either. I would like to avoid to configure and run esLint to fix my import order. Any other suggestions, setting or extension I could try?

Blues answered 12/10, 2020 at 12:47 Comment(2)
This ESLint plugin can do the sorting via the --fix command to ESLint: eslint-plugin-importSandler
Yeah, but you can only do prettier or eslint --fix. Both together is usually not a good idea. I have used this on projects where eslint is used for formattingBlues
B
4

In another project I have taken another look into this matter. The only solution is to turn the VSC organize import feature off and let the formatter/linter do the work. As I dont like my linter on autofix and I generally think its the job of the formatter and everyone should have one heres my solution:

  • In a TS project I used prettier-plugin-organize-imports. This prettier plugin mimicks the formatting order of the organize imports feature, so its basically the same. Very nice, except it has a TS peer dependency.

Therefore a second solution for JS projects:

  • Use the prettier plugin prettier-plugin-import-sort. This plugin needs you to add an order style of your choice with it. But it supports vanilla JS

As mentioned I have also found rules for eslint with an autofix, but I dont like that solution. The above packages fix the problem if you are using prettier.

Blues answered 4/6, 2021 at 13:27 Comment(0)
M
-1

You can install a vs-code extension sort-imports to achieve the sorting of imports.

Mascot answered 31/3, 2021 at 3:59 Comment(2)
As I stated in my question my colleagues do not use VSCode, so a VSCode Plugin is of little use.Blues
not just that, but it is also extremely limited. It only supports very specific "styles" of importing, but OP was looking for a specific order, they can define.Aphaeresis

© 2022 - 2024 — McMap. All rights reserved.