Skip to content

seiyab/prettier-plugin-sort-members

Repository files navigation

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.

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.