🧹 tidyclass - A CLI tool to automatically sort TypeScript class members in a structured order.
You can install tidyclass globally or use it as a local dependency in your project.
npm install -g tidyclass
or
npm install --save-dev tidyclass
Tip
It's recommended to commit your work before running the script.
Note
If you installed locally to your project, use npx tidyclass
.
To sort a single TypeScript file, run:
tidyclass path/to/file.ts
If no file is specified, tidyclass will process all TypeScript files in the project:
tidyclass
If you want to format the sorted files with Prettier, use the -p
or --prettier
option:
tidyclass -p
Opt classes out: To opt certain classes out of the member sorting, add the following JSDoc comment to the class.
/**
* @internal_sort skip
*/
class ThisClassWillNotBeSorted
CLI Option | Description |
---|---|
-p, --prettier |
Run Prettier on affected files after sorting |
Tidyclass organizes class members in the following structured order:
- Static public variables
- Static private variables
- Static public methods
- Static private methods
- Public instance variables
- Private instance variables
- Constructor
- Public instance methods
- Private instance methods
Each category is sorted alphabetically to ensure consistency. If the class structure changes, tidyclass updates it while maintaining logical order.
Before running tidyclass:
class MyClass {
public rivetingFunc() {}
private instanceVarB = 2;
static private staticFuncB() {}
public instanceVarA = 1;
private funcX() {}
private static staticVarB = 20;
private static staticVarA = 10;
public static staticFuncAA() {}
static public staticVarAB = 5;
private funcC() {}
public funcA() {}
constructor() {}
static private staticFuncA() {}
}
After running tidyclass (with formatting on):
class MyClass {
public static staticVarAB = 5;
private static staticVarA = 10;
private static staticVarB = 20;
public static staticFuncAA() {}
private static staticFuncA() {}
private static staticFuncB() {}
public instanceVarA = 1;
private instanceVarB = 2;
constructor() {}
public funcA() {}
public rivetingFunc() {}
private funcC() {}
private funcX() {}
}
Apache 2.0