mskelton / eslint-plugin-sort

Auto-fixable sort rules for ESLint.
https://www.npmjs.com/package/eslint-plugin-sort
ISC License
24 stars 1 forks source link

How to sort type exports? #29

Closed jlarmstrongiv closed 1 year ago

jlarmstrongiv commented 1 year ago

I have an index.ts file where I export:

export { getArbitraryFunction } from "./getArbitraryFunction";
export type { GetArbitraryFunctionInput } from "./getArbitraryFunction"

but it could also look like:

export type { GetArbitraryFunctionInput } from "./getArbitraryFunction"
export { getArbitraryFunction } from "./getArbitraryFunction";

How can I make it so that the type imports/exports are always second (but are still grouped together by file)?

mskelton commented 1 year ago

This is not currently possible, though it is something I would like to improve. I'd also like to explore the possibility of making type exports all grouped together after all the normal imports via a configuration setting.

jlarmstrongiv commented 1 year ago

I like it 😄 I think it would apply to sorting type imports as well

mskelton commented 1 year ago

@jlarmstrongiv I was thinking a little more about this and have some ideas of what the options might look like.

I'm thinking of 1-2 options. The first would be a new source group for type exports. This way you could combine all type exports into a single sort group.

{
  "sort/exports": [
    "warn",
    {
      "groups": [
        { "type": "default", "order": 2 },
        { "type": "type", "order": 3 },
        { "type": "other", "order": 1 }
      ]
    }
  ]
}

The other option would be the type export placement. This would apply only if you don't use type as a sort group.

{
  "sort/exports": [
    "warn",
    {
      "groups": [
        { "type": "default", "order": 2 },
        { "type": "other", "order": 1 }
      ],
      "typeOrder": "keep" | "before" | "after"
    }
  ]
}

With the typeOrder option you could sort type exports before or after the related export, or with keep it would maintain the original order. I would say the default should be after.

jlarmstrongiv commented 1 year ago

Great! That covers a lot of use cases. I’ll personally be using the typeOrder: "before" 😄

There’s also a new syntax in TypeScript 4.5 to combine the imports into a single statement:

import { someFunc, type BaseType } from "./some-module";

mskelton commented 1 year ago

I didn't think "typeOrder: "before" would be that useful, so sounds like it would be good as an option. Though individual type import/exports is a feature, I want to ensure the more classic way of structuring type imports/exports works. I also need to add some tests to make sure that individual type imports/exports sort properly.

mskelton commented 1 year ago

@jlarmstrongiv I've added the two new options that make sorting type imports/exports in https://github.com/mskelton/eslint-plugin-sort/releases/tag/v2.10.0. There is a new type sort group to sort type imports/exports totally separately and also a new typeOrder property that you can use to order types first, last, or preserve their order when they are sorted alongside of their corresponding imports/exports.

jlarmstrongiv commented 1 year ago

Thank you @mskelton 🎉