or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

alphabet-utility.mdclass-module-rules.mdcollection-rules.mdimport-export-rules.mdindex.mdjsx-variable-rules.mdobject-rules.mdplugin-configuration.mdtypescript-rules.md
tile.json

object-rules.mddocs/

Object Rules

Rules for sorting object properties, TypeScript object types, and destructured objects.

Capabilities

Sort Objects

Sorts object properties with support for destructured objects, styled components, and conditional configuration.

/**
 * Rule for sorting object properties and destructured objects
 */
const sortObjects: Rule.RuleModule;

/**
 * Configuration options for sort-objects rule
 */
interface SortObjectsOptions {
  /** 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 object member groups */
  groups?: GroupsOptions<ObjectGroup>;
  /** Custom groups for fine-tuned control */
  customGroups?: CustomGroupsOption<SingleCustomGroup> | DeprecatedCustomGroupsOption;
  /** Partition objects by comments */
  partitionByComment?: PartitionByCommentOption;
  /** Partition objects by newlines */
  partitionByNewLine?: boolean;
  /** Enable sorting for styled components */
  styledComponents?: boolean;
  /** Enable sorting for destructured objects */
  destructuredObjects?: { groups: boolean } | boolean;
  /** Enable sorting for object declarations */
  objectDeclarations?: boolean;
  /** Pattern to ignore specific objects */
  ignorePattern?: RegexOption;
  /** Conditional configuration based on context */
  useConfigurationIf?: {
    callingFunctionNamePattern?: RegexOption;
    allNamesMatchPattern?: RegexOption;
  };
  /** @deprecated Use destructuredObjects and objectDeclarations */
  destructureOnly?: boolean;
}

type ObjectGroup = 
  | "method" | "property" | "member" | "multiline"
  | "optional" | "required" | "unknown"
  | string;

interface SingleCustomGroup {
  selector?: ObjectSelector;
  modifiers?: ObjectModifier[];
  elementNamePattern?: RegexOption;
  elementValuePattern?: RegexOption;
}

type ObjectSelector = "method" | "property" | "member" | "multiline";
type ObjectModifier = "optional" | "required" | "multiline";

Usage Examples:

// Basic object property sorting
{
  "perfectionist/sort-objects": ["error", {
    "type": "alphabetical",
    "order": "asc"
  }]
}

// Advanced configuration with groups
{
  "perfectionist/sort-objects": ["error", {
    "type": "natural",
    "groups": ["method", "property"],
    "customGroups": [
      {
        "groupName": "id",
        "selector": "property",
        "elementNamePattern": "^id$"
      }
    ],
    "styledComponents": true,
    "destructuredObjects": { "groups": true }
  }]
}

// Conditional sorting based on function context
{
  "perfectionist/sort-objects": ["error", {
    "useConfigurationIf": {
      "callingFunctionNamePattern": "^(config|options)$"
    }
  }]
}

Sort Object Types

Sorts TypeScript object type properties and interface-like type definitions.

/**
 * Rule for sorting TypeScript object type properties
 */
const sortObjectTypes: Rule.RuleModule;

/**
 * Configuration options for sort-object-types rule
 */
interface SortObjectTypesOptions {
  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<ObjectTypeGroup>;
  customGroups?: CustomGroupsOption<ObjectTypeSingleCustomGroup>;
  partitionByComment?: PartitionByCommentOption;
  partitionByNewLine?: boolean;
}

type ObjectTypeGroup = 
  | "method" | "property" | "member" | "multiline"
  | "optional" | "required" | "unknown"
  | string;

interface ObjectTypeSingleCustomGroup {
  selector?: ObjectTypeSelector;
  modifiers?: ObjectTypeModifier[];
  elementNamePattern?: RegexOption;
}

type ObjectTypeSelector = "method" | "property" | "member" | "multiline";
type ObjectTypeModifier = "optional" | "required" | "multiline";

Usage Examples:

// Sort TypeScript object type properties
{
  "perfectionist/sort-object-types": ["error", {
    "type": "alphabetical",
    "groups": ["required", "optional"]
  }]
}

// Group by property types
{
  "perfectionist/sort-object-types": ["error", {
    "groups": ["method", "property"],
    "customGroups": [
      {
        "groupName": "callbacks",
        "selector": "method",
        "elementNamePattern": "^on[A-Z]"
      }
    ]
  }]
}

Types

Object Sorting Types

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

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

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

interface GroupCommentAboveOption {
  commentAbove: string;
}

/**
 * Custom group configuration for objects
 */
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 object properties
 */
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;

/**
 * Deprecated custom groups format
 */
type DeprecatedCustomGroupsOption = Record<string, string[] | string>;