Rules for organizing import and export statements, including support for TypeScript, side effects, and custom grouping.
Sorts and organizes import statements with support for grouping, side effects, and TypeScript imports.
/**
* Rule for sorting import statements with advanced grouping and TypeScript support
*/
const sortImports: Rule.RuleModule;
/**
* Configuration options for sort-imports rule
*/
interface SortImportsOptions {
/** Sorting strategy */
type?: "alphabetical" | "line-length" | "natural" | "custom";
/** Sort direction */
order?: "asc" | "desc";
/** Case sensitivity for sorting */
ignoreCase?: boolean;
/** Locale for sorting */
locales?: NonNullable<Intl.LocalesArgument>;
/** Custom alphabet for sorting */
alphabet?: string;
/** How to handle special characters */
specialCharacters?: "remove" | "trim" | "keep";
/** Newlines between different groups */
newlinesBetween?: "ignore" | "always" | "never" | number;
/** Maximum line length for line-length sorting */
maxLineLength?: number;
/** Custom import groups configuration */
groups?: GroupsOptions<Group>;
/** Custom groups for fine-tuned control */
customGroups?: CustomGroupsOption<SingleCustomGroup> | DeprecatedCustomGroupsOption;
/** Patterns for internal modules */
internalPattern?: RegexOption[];
/** Environment for path resolution */
environment?: "node" | "bun";
/** Sort side-effect imports */
sortSideEffects?: boolean;
/** Partition imports by comments */
partitionByComment?: PartitionByCommentOption;
/** Partition imports by newlines */
partitionByNewLine?: boolean;
/** TypeScript configuration */
tsconfig?: {
filename?: string;
rootDir: string;
};
}
type Group =
| "builtin" | "external" | "internal" | "parent" | "sibling" | "index"
| "type" | "side-effect" | "style" | "unknown"
| string;
interface SingleCustomGroup {
selector?: Selector;
modifiers?: Modifier[];
elementNamePattern?: RegexOption;
}
type Selector =
| "builtin" | "external" | "internal" | "parent" | "sibling" | "index"
| "type" | "side-effect" | "style" | "import";
type Modifier =
| "default" | "named" | "type" | "value" | "wildcard"
| "require" | "side-effect" | "ts-equals";Usage Examples:
// Basic import sorting
{
"perfectionist/sort-imports": ["error", {
"type": "alphabetical",
"order": "asc"
}]
}
// Advanced grouping with custom patterns
{
"perfectionist/sort-imports": ["error", {
"type": "natural",
"groups": [
"builtin",
"external",
["internal", "parent", "sibling", "index"],
"type",
"side-effect"
],
"customGroups": [
{
"groupName": "react",
"selector": "external",
"elementNamePattern": "^react"
}
],
"newlinesBetween": "always"
}]
}Sorts export statements and export declarations.
/**
* Rule for sorting export statements
*/
const sortExports: Rule.RuleModule;
/**
* Configuration options for sort-exports rule
*/
interface SortExportsOptions {
type?: "alphabetical" | "line-length" | "natural" | "custom";
order?: "asc" | "desc";
ignoreCase?: boolean;
locales?: NonNullable<Intl.LocalesArgument>;
alphabet?: string;
specialCharacters?: "remove" | "trim" | "keep";
groups?: GroupsOptions<ExportGroup>;
partitionByComment?: PartitionByCommentOption;
partitionByNewLine?: boolean;
}
type ExportGroup = "default" | "named" | "type" | "unknown" | string;Usage Examples:
// Sort export statements alphabetically
{
"perfectionist/sort-exports": ["error", {
"type": "alphabetical",
"order": "asc"
}]
}
// Group exports by type
{
"perfectionist/sort-exports": ["error", {
"groups": ["default", "named", "type"]
}]
}Sorts specifiers within named import statements.
/**
* Rule for sorting named import specifiers
*/
const sortNamedImports: Rule.RuleModule;
/**
* Configuration options for sort-named-imports rule
*/
interface SortNamedImportsOptions {
type?: "alphabetical" | "line-length" | "natural" | "custom";
order?: "asc" | "desc";
ignoreCase?: boolean;
locales?: NonNullable<Intl.LocalesArgument>;
alphabet?: string;
specialCharacters?: "remove" | "trim" | "keep";
ignoreAlias?: boolean;
groupKind?: "mixed" | "values-first" | "types-first";
}Usage Examples:
// Sort named imports alphabetically
{
"perfectionist/sort-named-imports": ["error", {
"type": "alphabetical",
"order": "asc"
}]
}
// Group type imports first
{
"perfectionist/sort-named-imports": ["error", {
"groupKind": "types-first"
}]
}Sorts specifiers within named export statements.
/**
* Rule for sorting named export specifiers
*/
const sortNamedExports: Rule.RuleModule;
/**
* Configuration options for sort-named-exports rule
*/
interface SortNamedExportsOptions {
type?: "alphabetical" | "line-length" | "natural" | "custom";
order?: "asc" | "desc";
ignoreCase?: boolean;
locales?: NonNullable<Intl.LocalesArgument>;
alphabet?: string;
specialCharacters?: "remove" | "trim" | "keep";
groupKind?: "mixed" | "values-first" | "types-first";
}Usage Examples:
// Sort named exports by line length
{
"perfectionist/sort-named-exports": ["error", {
"type": "line-length",
"order": "desc"
}]
}/**
* Regex pattern configuration for matching imports/exports
*/
type RegexOption = SingleRegexOption[] | SingleRegexOption;
type SingleRegexOption =
| {
pattern: string;
flags?: string;
}
| string;
/**
* Partition by comment configuration
*/
type PartitionByCommentOption =
| {
block?: RegexOption | boolean;
line?: RegexOption | boolean;
}
| RegexOption
| boolean;
/**
* Group configuration with newlines and comments
*/
type GroupsOptions<T> = (
| (GroupNewlinesBetweenOption & GroupCommentAboveOption)
| GroupNewlinesBetweenOption
| GroupCommentAboveOption
| T[]
| T
)[];
interface GroupNewlinesBetweenOption {
newlinesBetween: "ignore" | "always" | "never" | number;
}
interface GroupCommentAboveOption {
commentAbove: string;
}
/**
* Custom groups configuration
*/
type CustomGroupsOption<SingleCustomGroup = object> = ({
newlinesInside?: "always" | "never" | number;
fallbackSort?: FallbackSortOption;
order?: "asc" | "desc";
groupName: string;
type?: "alphabetical" | "line-length" | "natural" | "custom";
} & (AnyOfCustomGroup<SingleCustomGroup> | SingleCustomGroup))[];
interface AnyOfCustomGroup<SingleCustomGroup> {
anyOf: SingleCustomGroup[];
}
interface FallbackSortOption {
order?: "asc" | "desc";
type: "alphabetical" | "line-length" | "natural" | "custom";
}
/**
* Deprecated custom groups format
*/
type DeprecatedCustomGroupsOption = Record<string, string[] | string>;