Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Sort imports by line length #76

Open
oscaramos opened this issue Aug 28, 2021 · 7 comments
Open

Feature: Sort imports by line length #76

oscaramos opened this issue Aug 28, 2021 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@oscaramos
Copy link

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";
@ayusharma ayusharma added the help wanted Extra attention is needed label Oct 28, 2021
@ArtemKurtiakWork
Copy link

Hey, @oscaramos are there any updates on this?

@KLewin23
Copy link

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

@Aditeya
Copy link

Aditeya commented Mar 25, 2023

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

@azat-io
Copy link

azat-io commented Apr 23, 2023

Looking forward to this feature

@azat-io
Copy link

azat-io commented Jun 8, 2023

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

@dannypk
Copy link

dannypk commented Dec 15, 2023

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

@azat-io
Copy link

azat-io commented Dec 15, 2023

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'
      }
    ],
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants