eslint-plugin-perfectionist icon indicating copy to clipboard operation
eslint-plugin-perfectionist copied to clipboard

Feature: (sort-exports) Support sorting exports of `const` / `function` / `interface` / `type` and other similar exports.

Open u3u opened this issue 11 months ago • 1 comments

What rule do you want to change?

sort-exports

Describe the problem

Same as https://github.com/jrdrg/eslint-plugin-sort-exports

I think eslint-plugin-sort-exports is great, and I have been using it all along. However, it has a fatal flaw: if the exported variables depend on another variable, sorting them may result in accessing undefined variables. Currently, I can only disable it using // eslint-disable-next-line sort-exports/sort-exports (even if referenced within an exported function or an arrow function defined with export const, there won't be any issues because of variable hoisting). It would be great if it could be optimized and fixed based on this!

Code example

Input

export const z = { x: 0 };

// line comment
export const a = () => {
  return b;
};

// Due to the dependency on constant `z`, `b` should be placed after `z`.
export const b = z.x;

export enum E {};

export type B = '';

/** block comment */
export interface C {};

Output

If the sortExportKindFirst option is set to type, then the export interface and export type need to be placed at the top for separate sorting, with other export statements placed below them for separate sorting. I think this should be the default value because it is more in line with the intuitive definition of React components.

Note: The comment above the export statement should be sorted together with it.

export type B = '';

/** block comment */
export interface C {}

export enum E {}

// line comment
export const a = () => {
  return b;
};

export const z = { x: 0 };

// Due to the dependency on constant `z`, `b` should be placed after `z`.
export const b = z.x;

Additional comments

No response

Validations

  • [X] Read the docs.
  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

u3u avatar Aug 04 '23 14:08 u3u