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

Enable custom order in the same group

Open mattiaz9 opened this issue 2 years ago • 7 comments

Is your feature request related to a problem? I would like to manually sort some imports in a group without placing them in separated group. For instance I'd like to have import React from "react" as first and then all the others in alphabetical orders as default.

Describe the solution you'd like I thought we could set an array for a single group to define an order. Example:

{
  "importOrder": [
    ["^react", "<THIRD_PARTY_MODULES>"],
    "^[./]"
  ],
}

Describe alternatives you've considered None

mattiaz9 avatar Sep 02 '22 14:09 mattiaz9

This feature exists in a fork, if you want to use that until/unless this project merges https://github.com/trivago/prettier-plugin-sort-imports/pull/42.

https://github.com/ianvs/prettier-plugin-sort-imports#importorderseparation

IanVS avatar Sep 27 '22 19:09 IanVS

Could this help?
.prettierrc.js of mine

const packages = require('./package.json').dependencies;
const sortedPackages = Object.keys(packages).sort();
module.exports = {
  printWidth: 120,
  tabWidth: 2,
  useTabs: false,
  semi: true,
  singleQuote: true,
  trailingComma: 'all',
  arrowParens: 'avoid',
  endOfLine: 'lf',
  importOrder: sortedPackages.concat(['<THIRD_PARTY_MODULES>', '^\\.\\./', '^\\./']),
  importOrderSeparation: true,
  importOrderSortSpecifiers: true,
  importOrderCaseInsensitive: true,
};

Actually <THIRD_PARTY_MODULES> act as your custom imports

tearf001 avatar Sep 30 '22 08:09 tearf001

@tearf001 I don't think it would.

Anyway I'm using this fork at the moment that can do it: https://github.com/ianvs/prettier-plugin-sort-imports

mattiaz9 avatar Sep 30 '22 14:09 mattiaz9

@tearf001 Thanks for the great workaround. It helped me with custom aliases. I transformed to Regex because otherwise a package abc would catch a local path like ./app-abc.ts

const sortedPackages = Object.keys(packages).sort().map(package => `^${package}($|\/.+)`;

tsvetkovv avatar Jan 25 '23 11:01 tsvetkovv

I really would like this feature!

CagriUysal avatar Aug 04 '23 20:08 CagriUysal

I would LOVE this feature! Has any progress been made?

DaveKeehl avatar May 07 '24 16:05 DaveKeehl

I would LOVE this feature. I like React imports up top also.

standingdreams avatar Jun 02 '24 15:06 standingdreams