prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

Feature: Sort imports by line length

Open oscaramos opened this issue 4 years ago • 8 comments

Hello,

Add an option to sort imports by line length. For example:

Sort by line length in ascending order:

import { LocationProvider } from "@services/location/location.context";
import { FavouritesProvider } from "@services/favourites/favourites.context";
import { RestaurantsProvider } from "@services/restaurants/restaurants.context";
import { AuthenticationProvider } from "@services/authentication/authentication.context";

Sort by line length in descending order:

import { AuthenticationProvider } from "@services/authentication/authentication.context";
import { RestaurantsProvider } from "@services/restaurants/restaurants.context";
import { FavouritesProvider } from "@services/favourites/favourites.context";
import { LocationProvider } from "@services/location/location.context";

oscaramos avatar Aug 28 '21 17:08 oscaramos

Hey, @oscaramos are there any updates on this?

ArtemKurtiakWork avatar Nov 23 '22 19:11 ArtemKurtiakWork

I have made a fork for this in my own org if anyone has an opinion on how they would want this added I can probably come up with something

KLewin23 avatar Mar 25 '23 01:03 KLewin23

I think a flag in prettier config would be best IMO

module.exports = {
  ...
  "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderSortByLength": "asc" | "desc"
}
  • "asc" will sort in ascending
  • "desc" will sort in descending
  • null/undefined will not sort by length

Aditeya avatar Mar 25 '23 05:03 Aditeya

Looking forward to this feature

azat-io avatar Apr 23 '23 00:04 azat-io

You can use eslint-plugin-perfectionist with fix on save option

azat-io avatar Jun 08 '23 15:06 azat-io

any updates here?

I am using plugin:perfectionist/recommended-line-length and I would like to be sorted "asc" not "desc", I don't want to go through each rule and overwrite that. Is there any option available to change the sort order?

Thanks D

dannypk avatar Dec 15 '23 03:12 dannypk

I am using plugin:perfectionist/recommended-line-length and I would like to be sorted "asc" not "desc"

@dannypk You can set up the plugin to sort in ascending or descending order. By default it sorts in desc.

module.exports = {
  rules: {
    'perfectionist/sort-imports': [
      'error',
      {
        'groups': [
          'side-effect',
          ['side-effect-style', 'style'],
          ['builtin-type', 'type', 'builtin', 'external', 'unknown'],
          [
            'internal-type',
            'parent-type',
            'sibling-type',
            'index-type',
            'internal',
            'parent',
            'sibling',
            'index'
          ],
          ['object']
        ],
        'newlines-between': 'always',
+        'order': 'asc',
        'type': 'line-length'
      }
    ],
  },
}

azat-io avatar Dec 15 '23 08:12 azat-io