Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prettier-plugin-sort-members

seiyab7.3kMIT0.2.3

Usage

prettier, plugin, sort, members, javascript, typescript

readme

prettier-plugin-sort-members

Usage

npm install prettier-plugin-sort-members --save-dev
# or
yarn add prettier-plugin-sort-members --dev
# or
bun add prettier-plugin-sort-members --dev

Edit your prettierrc.

{
    // ...
    "plugins": ["prettier-plugin-sort-members"]
}

Optionally, configure options.

{
    // ...
    "plugins": ["prettier-plugin-sort-members"],
    "sortMembersAlphabetically": true
}

Overview

This plugin sorts members of your classes, interfaces, and type aliases.

// Before
class MyClass {
    d(): void {}
    e: null;
    c: string;
    a(): void {}
    b: number;
    constructor() {}
}

// After
class MyClass {
    e: null;
    c: string;
    b: number;
    constructor() {}
    d(): void {}
    a(): void {}
}

// After (with option sortMembersAlphabetically = true)
class MyClass {
    c: string;
    b: number;
    e: null;
    constructor() {}
    a(): void {}
    d(): void {}
}

The order respects default order of @typescript-eslint/member-ordering

Options

sortMembersAlphabetically

  • type: boolean
  • default: false

A boolean value to enable alphabetical ordering. Other criteria such as visibility still precedes even if set true.

keepGettersAndSettersTogether

  • type: boolean
  • default: false
  • since: 0.2.0

A boolean value to place getters and setters same group. You can use it to avoid conflicting with @typescript-eslint/adjacent-overload-signatures.

skipSortForSubclassOf (experimental)

  • type: Array<string>
  • default: []
  • since: 0.2.0

This option takes an array of strings representing the names of base classes. When set, the plugin will not sort members of any class that extends the specified base classes. It's particularly useful when any class that extends the specified base class should follow other ordering such as react/sort-comp

// example if you are using react/sort-comp
{
    "skipSortForSubclassOf": ["React.Component"]
}

Contributing

Reporting issues

You can submit an issue to report a bug, propose a feature or anything else. Feedback is welcome. To report a security vulnerability, please use our create an advisory form on GitHub.

Developing

prettier-plugin-sort-members is developed with Bun. Install Bun if you don't have it yet.

To get up and running, install dependencies and run tests:

bun install
bun test

Don't forget to add tests for your changes before you submit a PR.

changelog

Changelog


0.2.3

Bug fix

  • Handle TypeScript decorated method overloading (#72)

0.2.2

Feature

  • Support TypeScript accessor (#62).

Bug fix

  • Improve sortMembersAlphabetically in compatibility with @typescript-eslint/member-ordering's { order: "alphabetically" } (#67)

0.2.1

No changes from 0.2.1-rc.1

0.2.1-rc.1

Bug fix

  • Improve compatibility with @typescript-eslint/member-ordering (#57).
    • Note that @typescript-eslint/member-ordering is inconsistent a bit. For static methods, decorated ones go latter. On the other hand, for instance methods, decorated ones go former. From this version, prettier-plugin-sort-members also goes same way.

0.2.1-rc.0

Feature

  • keepGettersAndSettersTogether will couple setter with associated getter.

Others

  • Internal package updates and cleanup

0.2.0

Feature

  • Add new option keepGettersAndSettersTogether

0.2.0-rc.0

Feature

  • Add new option skipSortForSubclassOf

0.1.2

Bug fix

  • For type alias and interface, consider function members as members, not method (#40). This improves compatibility with @typescript-eslint/member-ordering's default setting.
    • Feel free to request an option if you feel former ordering is more consistent or come up with better rule.

0.1.1

Bug fix

  • Sort literal keys alphabetically if sortMembersAlphabetically is true (#34)

Others

  • Update dependencies

0.1.0

Nothing has changed since 0.1.0-rc.0

0.1.0-rc.0

Feature

  • Support babel-ts parser

Bug fix

  • Keep order for computed property if sortMembersAlphabetically is not true
  • Method type precedes static-ness and decorated-ness

0.0.8-alpha.0

Breaking

  • Add an option sortMembersAlphabetically
    • BREAKING: sortMembersAlphabetically is false by default. Earlier version works as it is set true
    • See README for detail

0.0.7-alpha.0

Feature

  • JavaScript (babel) support

Bug

  • Fix order of abstract field

Others

  • Changed approach how to plug into Prettier
    • I believe it can't affect users. Report if this plugin get not to work.