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

Also sort the exports #119

Open
eduardodallmann opened this issue Aug 30, 2023 · 13 comments
Open

Also sort the exports #119

eduardodallmann opened this issue Aug 30, 2023 · 13 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@eduardodallmann
Copy link

Is your feature request related to a problem?
Sort also export not just imports.

Describe the solution you'd like
When I have code like that:

export { c, a, b, A } from 'foo';
export { x } from 'bar';

To be sorted like that:

export { x } from 'bar';
export { A, a, b, c } from 'foo';

Describe alternatives you've considered
None

Additional context
Related: trivago/prettier-plugin-sort-imports#159

@fbartho
Copy link
Collaborator

fbartho commented Aug 30, 2023

@eduardodallmann unfortunately sorting exports could break code. For example:

export const FooAPI = new MyAPI(args);

// export our default implementation separately
export const beAwesome = FooAPI.beAwesome:

If you sorted this, your output would break! Obviously this is a trivial example, there’s tons of other ways to case moving around exports to be a problem.

This is a neat idea, that might make sense in someone’s codebase, so I encourage you to to make a prettier-plugin-sort-exports library. I just think that’s out-of-scope for this library!

@fbartho fbartho added enhancement New feature or request wontfix This will not be worked on labels Aug 30, 2023
@eduardodallmann
Copy link
Author

You are correct. Sorting export lines can cause a lot of problems. But I believe that organizing just the de-structuring could be good.

This plugin does that https://github.com/simonhaenisch/prettier-plugin-organize-imports

@IanVS
Copy link
Owner

IanVS commented Aug 30, 2023

There are potentially also cases where we could sort re-exports based on the source, which you show in your original example. I have some files in my own project which could benefit from that. But technically I think it might be difficult to achieve with the current way this plugin works, since exports can be spread throughout the file, whereas imports are always at the top (at least by convention, since they execute first). I'll take off the wontfix label for now, but not commit to anything just yet.

Thanks for mentioning prettier-plugin-organize-imports. I haven't looked into that one too closely, what are your impressions on the main differences between that library and this one, and are you currently using either one of them?

@IanVS IanVS removed the wontfix This will not be worked on label Aug 30, 2023
@eduardodallmann
Copy link
Author

I'm currently using the prettier-plugin-sort-imports lib. In my opinion it is much better because it is more like trivago but solves many of the problems.

The only thing I like about prettier-plugin-organize-imports is organizing the export.

I'm in a remix.run project, and by convention the routes have loader and action functions being exported. But to better separate and organize the codebase I did it this way:

routes/
├── login/
│   ├── login.server.ts
│   ├── route.tsx

login.server.ts

export const loader: LoaderFunction = (): LoginLoaderReturn => ....

export const action: ActionFunction = async ({ request }) => ....

route.tsx

import ....
import ....

export { loader, action } from './login.server';

export default function Login() {
   return a react component....

Reference https://remix.run/docs/en/1.19.3/route/loader

@SalahAdDin
Copy link

I'm looking exactly for something like this, but there are not maintained libraries at the moment, and honestly, I don't understand why.

@IanVS
Copy link
Owner

IanVS commented Oct 2, 2023

@SalahAdDin, are you looking for sorting exports, or only re-exports from other files?

@SalahAdDin
Copy link

SalahAdDin commented Oct 2, 2023

@SalahAdDin, are you looking for sorting exports, or only re-exports from other files?

Sorting exports, We don't have to go to every file and sort them manually hahahahaha.

@IanVS
Copy link
Owner

IanVS commented Oct 2, 2023

I mean, do you want to sort all exports? If a file exports two functions and a constant, you want to sort those? Or only sort re-exports like this?

export foo from "./foo";

@SalahAdDin
Copy link

I mean, do you want to sort all exports? If a file exports two functions and a constant, do you want to sort those? Or only sort re-exports like this?

export foo from "./foo";

Not re-export, the file exports, that is the most common use for the export function (i think).

@IanVS
Copy link
Owner

IanVS commented Oct 3, 2023

Can you give an example of a before and after?

@SalahAdDin
Copy link

Can you give an example of a before and after?

Input:

export type {
  RContentNode,
  RImage,
  RContent,
  RLink,
  RList,
  RNode,
  RTextAttributes,
  RListItem,
  RVideo,,
  RNodeChildren,
  RText,
  RNodeType
};

Ouput:

export type {
  RContent,
  RContentNode,
  RImage,
  RLink,
  RList,
  RListItem,
  RNode,
  RNodeChildren,
  RNodeType,
  RText,
  RTextAttributes,
  RVideo,
};

@IanVS
Copy link
Owner

IanVS commented Oct 3, 2023

ok, so just the specifiers within a named export block, got it. I think that could potentially be doable, but it would need to be placed behind a new option, defaulted to false, to avoid unexpected changes.

I don't have time to work on it right now, but I'd be willing to review a PR if someone else wants to take a shot. If anyone plans to work on it, let us know and we can discuss the details.

@IanVS IanVS added the help wanted Extra attention is needed label Jun 25, 2024
@curiosbasant
Copy link

Yes, re-exports should also be sorted just like imports

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

No branches or pull requests

5 participants