CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-eslint-plugin-perfectionist

ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

class-module-rules.mddocs/

Class Module Rules

Rules for organizing class members, module exports, and decorators.

Capabilities

Sort Classes

Sorts class members with support for different member types, visibility modifiers, and custom grouping.

/**
 * Rule for sorting class members with advanced grouping options
 */
const sortClasses: Rule.RuleModule;

/**
 * Configuration options for sort-classes rule
 */
interface SortClassesOptions {
  /** 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;
  /** Custom class member groups */
  groups?: GroupsOptions<ClassGroup>;
  /** Custom groups for fine-tuned control */
  customGroups?: CustomGroupsOption<ClassSingleCustomGroup>;
  /** Partition class members by comments */
  partitionByComment?: PartitionByCommentOption;
  /** Partition class members by newlines */
  partitionByNewLine?: boolean;
}

type ClassGroup = 
  | "index-signature" | "static-property" | "private-property" | "property"
  | "constructor" | "static-method" | "private-method" | "method"
  | "accessor-property" | "get-method" | "set-method"
  | "static-accessor-property" | "static-get-method" | "static-set-method"
  | "private-accessor-property" | "private-get-method" | "private-set-method"
  | "unknown" | string;

interface ClassSingleCustomGroup {
  selector?: ClassSelector;
  modifiers?: ClassModifier[];
  elementNamePattern?: RegexOption;
}

type ClassSelector = 
  | "index-signature" | "property" | "accessor-property" | "constructor"
  | "method" | "get-method" | "set-method";

type ClassModifier = 
  | "abstract" | "static" | "private" | "protected" | "public"
  | "readonly" | "optional" | "override" | "declare" | "decorated";

Usage Examples:

// Basic class member sorting
{
  "perfectionist/sort-classes": ["error", {
    "type": "alphabetical",
    "order": "asc"
  }]
}

// Advanced grouping by member type and visibility
{
  "perfectionist/sort-classes": ["error", {
    "groups": [
      "index-signature",
      ["static-property", "private-property", "property"],
      "constructor",
      ["static-method", "private-method", "method"],
      ["get-method", "set-method"]
    ],
    "customGroups": [
      {
        "groupName": "lifecycle",
        "selector": "method",
        "elementNamePattern": "^(componentDidMount|componentWillUnmount)$"
      }
    ]
  }]
}

Sort Modules

Sorts module-level exports and statements.

/**
 * Rule for sorting module exports and statements
 */
const sortModules: Rule.RuleModule;

/**
 * Configuration options for sort-modules rule
 */
interface SortModulesOptions {
  type?: "alphabetical" | "line-length" | "natural" | "custom";
  order?: "asc" | "desc";
  ignoreCase?: boolean;
  locales?: NonNullable<Intl.LocalesArgument>;
  alphabet?: string;
  specialCharacters?: "remove" | "trim" | "keep";
  newlinesBetween?: "ignore" | "always" | "never" | number;
  groups?: GroupsOptions<ModuleGroup>;
  customGroups?: CustomGroupsOption<ModuleSingleCustomGroup>;
  partitionByComment?: PartitionByCommentOption;
  partitionByNewLine?: boolean;
}

type ModuleGroup = 
  | "declare-namespace" | "declare-module" | "declare-global"
  | "export-namespace" | "export-interface" | "export-type" | "export-default"
  | "export-named" | "export-all" | "import-namespace" | "import-default"
  | "import-named" | "import-all" | "require" | "unknown" | string;

interface ModuleSingleCustomGroup {
  selector?: ModuleSelector;
  modifiers?: ModuleModifier[];
  elementNamePattern?: RegexOption;
}

type ModuleSelector = 
  | "import" | "export" | "require" | "declare"
  | "namespace" | "interface" | "type" | "function" | "class";

type ModuleModifier = 
  | "default" | "named" | "namespace" | "type" | "value"
  | "declare" | "export" | "async" | "inline";

Usage Examples:

// Sort module statements by type
{
  "perfectionist/sort-modules": ["error", {
    "groups": [
      ["import-namespace", "import-default", "import-named"],
      "require",
      ["export-namespace", "export-default", "export-named"],
      ["declare-namespace", "declare-module"]
    ]
  }]
}

Sort Decorators

Sorts decorators applied to classes, methods, and properties.

/**
 * Rule for sorting decorators
 */
const sortDecorators: Rule.RuleModule;

/**
 * Configuration options for sort-decorators rule
 */
interface SortDecoratorsOptions {
  type?: "alphabetical" | "line-length" | "natural" | "custom";
  order?: "asc" | "desc";
  ignoreCase?: boolean;
  locales?: NonNullable<Intl.LocalesArgument>;
  alphabet?: string;
  specialCharacters?: "remove" | "trim" | "keep";
  groups?: GroupsOptions<DecoratorGroup>;
  customGroups?: CustomGroupsOption<DecoratorSingleCustomGroup>;
}

type DecoratorGroup = "unknown" | string;

interface DecoratorSingleCustomGroup {
  elementNamePattern?: RegexOption;
}

Usage Examples:

// Sort decorators alphabetically
{
  "perfectionist/sort-decorators": ["error", {
    "type": "alphabetical"
  }]
}

// Custom decorator grouping
{
  "perfectionist/sort-decorators": ["error", {
    "customGroups": [
      {
        "groupName": "validation",
        "elementNamePattern": "^(IsString|IsNumber|IsEmail)$"
      },
      {
        "groupName": "framework",
        "elementNamePattern": "^(Component|Injectable|Controller)$"
      }
    ],
    "groups": ["framework", "validation", "unknown"]
  }]
}

Types

Class and Module Sorting Types

/**
 * Base class/module sorting configuration
 */
interface ClassModuleSortingOptions {
  type?: "alphabetical" | "line-length" | "natural" | "custom";
  order?: "asc" | "desc";
  ignoreCase?: boolean;
  locales?: NonNullable<Intl.LocalesArgument>;
  alphabet?: string;
  specialCharacters?: "remove" | "trim" | "keep";
}

/**
 * Group configuration for class/module elements
 */
type GroupsOptions<T> = (
  | (GroupNewlinesBetweenOption & GroupCommentAboveOption)
  | GroupNewlinesBetweenOption
  | GroupCommentAboveOption
  | T[]
  | T
)[];

interface GroupNewlinesBetweenOption {
  newlinesBetween: "ignore" | "always" | "never" | number;
}

interface GroupCommentAboveOption {
  commentAbove: string;
}

/**
 * Custom group configuration for class/module elements
 */
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";
}

/**
 * Regex pattern for matching class/module elements
 */
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;

docs

alphabet-utility.md

class-module-rules.md

collection-rules.md

import-export-rules.md

index.md

jsx-variable-rules.md

object-rules.md

plugin-configuration.md

typescript-rules.md

tile.json