Skip to content

Sorts properties and methods in classes alphabetically 🧹

License

Notifications You must be signed in to change notification settings

Firgrep/tidyclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tidyclass

🧹 tidyclass - A CLI tool to automatically sort TypeScript class members in a structured order.

Installation

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

Usage

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

Options

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

Sorting Order

Tidyclass organizes class members in the following structured order:

  1. Static public variables
  2. Static private variables
  3. Static public methods
  4. Static private methods
  5. Public instance variables
  6. Private instance variables
  7. Constructor
  8. Public instance methods
  9. Private instance methods

Each category is sorted alphabetically to ensure consistency. If the class structure changes, tidyclass updates it while maintaining logical order.

Example

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() {}
}

License

Apache 2.0

About

Sorts properties and methods in classes alphabetically 🧹

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published