Skip to content

Feature: Support sorting by namespace export in sort-exports rule #623

@AlexMan123456

Description

@AlexMan123456

What rule do you want to change?

sort-exports

Describe the problem

As of now, the built-in export groups seem to be value-export and type-export, which already work well, but I propose we should add a namespace-export group for exports such as export * from "./functions", for example. I tried doing this using the customGroups property with something like this:

{
  customGroups: [
    {
      anyOf: [
        {
          elementNamePattern: "*",
        },
      ],
      groupName: "namespace-export",
      type: "alphabetical",
    },
  ],
  fallbackSort: { type: "natural" },
  groups: ["value-export", "namespace-export", "type-export"],
  ignoreCase: true,
  newlinesBetween: 1,
  order: "asc",
  partitionByComment: false,
  partitionByNewLine: false,
  specialCharacters: "keep",
  type: "alphabetical",
}

But it didn't seem to like the elementNamePattern for whatever reason, maybe because the * isn't actually an exported element (e.g. it expects export { myFunction } from "./functions" so the elementNamePattern as is tries to match export { * } from "./functions").

It would be good if this could be something that is supported natively. Maybe break it down into namespace-value-export and namespace-type-export, as I believe that export type * from "./types is also valid and may need separate treatment.

Code example

As of now, with the following sort-exports configuration:

{
    customGroups: [],
    fallbackSort: { type: "natural" },
    groups: ["value-export", "type-export"],
    ignoreCase: true,
    newlinesBetween: 1,
    order: "asc",
    partitionByComment: false,
    partitionByNewLine: false,
    specialCharacters: "keep",
    type: "alphabetical",
}

The index.ts file of my utility package looks like this:

export { default as appendSemicolon } from "src/functions/appendSemicolon";
export * from "src/functions/arrayHelpers";
export { default as camelToKebab } from "src/functions/camelToKebab";
export { default as convertFileToBase64 } from "src/functions/convertFileToBase64";
export { default as createFormData } from "src/functions/createFormData";
export { default as createTemplateStringsArray } from "src/functions/createTemplateStringsArray";
export * from "src/functions/date";
export { default as getRandomNumber } from "src/functions/getRandomNumber";
export { default as getRecordKeys } from "src/functions/getRecordKeys";
export { default as isOrdered } from "src/functions/isOrdered";
export { default as kebabToCamel } from "src/functions/kebabToCamel";
export {
  default as normalizeImportPath,
  normaliseImportPath,
} from "src/functions/normalizeImportPath";
export { default as omitProperties } from "src/functions/omitProperties";
export * from "src/functions/parsers";
export { default as removeDuplicates } from "src/functions/removeDuplicates";
export { default as stringListToArray } from "src/functions/stringListToArray";
export * from "src/functions/taggedTemplate";
export { default as truncate } from "src/functions/truncate";
export { default as wait } from "src/functions/wait";

export type {
  FormDataNullableResolutionStrategy as FormDataResolutionStrategy,
  CreateFormDataOptions,
  CreateFormDataOptionsNullableResolution,
  CreateFormDataOptionsUndefinedOrNullResolution,
} from "src/functions/createFormData";
export type { KebabToCamelOptions } from "src/functions/kebabToCamel";
export type { StringListToArrayOptions } from "src/functions/stringListToArray";

where the namespace exports are mixed with the rest of the value exports. However, it would be nice if I could instead sort them like this:

export { default as appendSemicolon } from "src/functions/appendSemicolon";
export { default as camelToKebab } from "src/functions/camelToKebab";
export { default as convertFileToBase64 } from "src/functions/convertFileToBase64";
export { default as createFormData } from "src/functions/createFormData";
export { default as createTemplateStringsArray } from "src/functions/createTemplateStringsArray";
export { default as getRandomNumber } from "src/functions/getRandomNumber";
export { default as getRecordKeys } from "src/functions/getRecordKeys";
export { default as isOrdered } from "src/functions/isOrdered";
export { default as kebabToCamel } from "src/functions/kebabToCamel";
export {
  default as normalizeImportPath,
  normaliseImportPath,
} from "src/functions/normalizeImportPath";
export { default as omitProperties } from "src/functions/omitProperties";
export { default as removeDuplicates } from "src/functions/removeDuplicates";
export { default as stringListToArray } from "src/functions/stringListToArray";
export { default as truncate } from "src/functions/truncate";
export { default as wait } from "src/functions/wait";

export * from "src/functions/arrayHelpers";
export * from "src/functions/date";
export * from "src/functions/parsers";
export * from "src/functions/taggedTemplate";

export type {
  FormDataNullableResolutionStrategy as FormDataResolutionStrategy,
  CreateFormDataOptions,
  CreateFormDataOptionsNullableResolution,
  CreateFormDataOptionsUndefinedOrNullResolution,
} from "src/functions/createFormData";
export type { KebabToCamelOptions } from "src/functions/kebabToCamel";
export type { StringListToArrayOptions } from "src/functions/stringListToArray";

where the namespace exports are recognised as their own group.

Additional comments

Probably not super relevant to this issue, but this is the relevant index file in my utility package:

https://github.com/AlexMan123456/utility/blob/main/src/functions/index.ts

And the sort-exports configuration can be found here, in my ESLint plugin (I use my ESLint plugin to centralise my ESLint configs across all my projects):

https://github.com/AlexMan123456/eslint-plugin/blob/main/src/configs/helpers/sorting/sortExports.ts

Validations

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions