eslint-plugin-typescript-sort-keys

repository·master·Indexed 41 days ago

https://github.com/infctr/eslint-plugin-typescript-sort-keys

An ESLint plugin for TypeScript that enforces alphabetical sorting of keys in interfaces and string enums. Version 3.3.0 provides two fixable rules: `typescript-sort-keys/interface` and `typescript-sort-keys/string-enum`. It supports configurable sort directions (ascending or descending), case-sensitivity, natural sorting for numeric strings, and an option to ensure required interface properties appear first.

Tokens
3.5K
Snippets
20
Records
21
Agent score
92%

What's inside eslint-plugin-typescript-sort-keys

  1. Install eslint-plugin-typescript-sort-keys

    master

    To use this plugin, you must have eslint, typescript, and @typescript-eslint/parser installed. You can then install the plugin using your preferred package manager.

    Note: If you have installed ESLint globally, you must also install eslint-plugin-typescript-sort-keys globally.

    yarn add -D eslint typescript @typescript-eslint/parser
    yarn add -D eslint-plugin-typescript-sort-keys
  2. Configure eslint-plugin-typescript-sort-keys in ESLint

    master

    To use the plugin, configure your .eslintrc file to use the @typescript-eslint/parser and include typescript-sort-keys in the plugins array. You can omit the eslint-plugin- prefix when naming the plugin.

    To enable all rules with their recommended default settings, use the plugin:typescript-sort-keys/recommended extension.

    {
      "parser": "@typescript-eslint/parser",
      "plugins": ["typescript-sort-keys"],
      "extends": ["plugin:typescript-sort-keys/recommended"]
    }
  3. Configure the string-enum rule options

    master

    The typescript-sort-keys/string-enum rule accepts an array of options. The first element is the severity ("error", "warn", etc.), the second is the sort direction, and the third is an optional configuration object.

    Sort Direction

    • "asc" (default): Enforces ascending order.
    • "desc": Enforces descending order.

    Configuration Object

    • caseSensitive (boolean, default: true): If true, enforces case-sensitive sorting. If false, sorting is case-insensitive.
    • natural (boolean, default: false): If true, enforces natural order. Natural order sorts strings containing letters and numbers as a human would (e.g., 10 comes after 3).
    {
      "typescript-sort-keys/string-enum": ["error", "asc", { "caseSensitive": true, "natural": false }]
    }
  4. Configure typescript-sort-keys rules manually

    master

    You can enable specific rules individually in your .eslintrc configuration under the rules section. For example, to enforce sorting for both interfaces and string enums as errors:

    {
      "rules": {
        "typescript-sort-keys/interface": "error",
        "typescript-sort-keys/string-enum": "error"
      }
    }
  5. Configure the `typescript-sort-keys/interface` rule

    master

    The typescript-sort-keys/interface rule enforces alphabetical sorting of property keys within TypeScript interface declarations. This helps maintain consistency and makes it easier to locate properties in large interfaces.

    Configuration Structure

    The rule accepts an array containing a severity level, an optional sort direction, and an optional configuration object:

    {
      "typescript-sort-keys/interface": [
        "error",
        "asc",
        {
          "caseSensitive": true,
          "natural": false,
          "requiredFirst": false
        }
      ]
    }

    Sort Direction

    • "asc" (default): Enforces properties in ascending order.
    • "desc": Enforces properties in descending order.

    Configuration Options

    • caseSensitive (default: true): If true, enforces case-sensitive sorting (e.g., C comes before a). If false, sorting is case-insensitive.
    • natural (default: false): If true, uses natural sorting for strings containing numbers (e.g., 1, 2, 10 instead of 1, 10, 2).
    • requiredFirst (default: false): If true, enforces that all required properties appear before optional properties (?).
    {
      "typescript-sort-keys/interface": [
        "error",
        "asc",
        { "caseSensitive": true, "natural": false, "requiredFirst": false }
      ]
    }
  6. Configure case-sensitive and case-insensitive sorting

    master

    You can control how the rule handles character casing using the caseSensitive option.

    Case-Sensitive (Default)

    With caseSensitive: true, uppercase letters are sorted according to their ASCII/Unicode values (typically appearing before lowercase letters).

    Correct:

    interface U {
      C: T
      a: T
      b: T
    }

    Case-Insensitive

    With caseSensitive: false, the rule ignores casing when determining order.

    Correct:

    /* eslint typescript-sort-keys/interface: ["error", "asc", { caseSensitive: false }] */
    interface U {
      a: T
      b: T
      c: T
      C: T
    }
    /* eslint typescript-sort-keys/interface: ["error", "asc", { caseSensitive: false }] */
    interface U {
      a: T
      b: T
      c: T
      C: T
    }
  7. Configure required properties to appear first

    master

    The requiredFirst option allows you to group required properties at the top of an interface, followed by optional properties.

    Correct with requiredFirst: true:

    /* eslint typescript-sort-keys/interface: ["error", "asc", { requiredFirst: true }] */
    interface U {
      a: T
      d: T
      b?: T
      c?: T
    }
    /* eslint typescript-sort-keys/interface: ["error", "asc", { requiredFirst: true }] */
    interface U {
      a: T
      d: T
      b?: T
      c?: T
    }
  8. Configure natural sorting for numeric strings

    master

    The natural option determines how strings containing numbers are sorted.

    • When natural: false (default), sorting is strictly alphabetical/lexicographical. This means 10 comes before 2 because '1' comes before '2'.
    • When natural: true, the rule uses human-like natural sorting where numbers are treated numerically. This means 2 comes before 10.

    Correct with natural: true:

    /* eslint typescript-sort-keys/interface: ["error", "asc", { natural: true }] */
    interface U {
      1: T
      2: T
      10: T
    }
    /* eslint typescript-sort-keys/interface: ["error", "asc", { natural: true }] */
    interface U {
      1: T
      2: T
      10: T
    }
  9. Use case-insensitive sorting with string-enum

    master

    To ignore casing when sorting enum members, set the caseSensitive option to false in the rule configuration.

    {
      "typescript-sort-keys/string-enum": ["error", "asc", { "caseSensitive": false }]
    }

    // Correct code for { caseSensitive: false } // Note: The order depends on how the specific case-insensitive algorithm handles the characters. enum U { a = 'T', b = 'T', c = 'T', C = 'T', }

  10. Use `typescript-sort-keys/interface` for ascending and descending order

    master

    The typescript-sort-keys/interface rule can be configured to enforce either ascending (asc) or descending (desc) property order.

    Ascending Order (asc)

    By default, properties must follow an ascending alphabetical order.

    Correct:

    interface U {
      a: T
      b: T
      c: T
    }

    Descending Order (desc)

    When configured with "desc", properties must follow a descending alphabetical order.

    Correct:

    /* eslint typescript-sort-keys/interface: ["error", "desc"] */
    interface U {
      c: T
      b: T
      a: T
    }
    /* eslint typescript-sort-keys/interface: ["error", "asc"] */
    interface U {
      a: T
      b: T
      c: T
    }
  11. Configure sorting parameters for rules

    master

    The plugin rules accept a SortingParamsOptions object to fine-tune how keys are sorted. The available configuration keys are:

    • caseSensitive: A boolean determining if sorting should respect character casing.
    • natural: A boolean determining if natural sorting (e.g., treating '2' as less than '10') should be used.
    • requiredFirst: A boolean used to specify if certain keys must appear first.
    // Example shape of sorting parameters
    {
      caseSensitive: boolean,
      natural: boolean,
      requiredFirst: boolean
    }