bryanrsmith / eslint-plugin-sort-class-members

An ESLint rule for enforcing consistent ES6 class member order
119 stars 22 forks source link

Match non getters / setters methods #45

Open RuiGau opened 6 years ago

RuiGau commented 6 years ago

Firstly, thanks for the awesome plugin 👍 .

My goal

I would like to sort public not static not constructor not accessor methods alphabetically in ES6 classes.

What I tried

How can I match methods that are not accessors in "groups" ?

I would like to set a configuration like that

"groups": {
    "conventional-public-non-static-nor-accessor-nor-constructor-methods": [{ 
        "name": "/(?!(constructor|[^_])).+/", 
        "type": "method", 
        "static": false, 
        "sort": "alphabetical" 
    }],
}

to place this slot like that

"[conventional-private-getters]",
"[static-properties]",
"[static-methods]",
"[properties]",
"[conventional-private-properties]",
"constructor",
"[conventional-public-non-static-nor-accessor-nor-constructor-methods]",
"[conventional-private-methods]",

But this only matches public non static non constructor methods, it still matches getters and setters.

Can i create such a group ? Or maybe I'm doing it wrong and there's a simpler way I don't find ?

wjhurley commented 1 year ago

If you handle accessor-pairs separately, then they won't be matched by your conventional-public-non-static-nor-accessor-nor-constructor-methods groups. I tested with this configuration:

        'sort-class-members/sort-class-members': [
            2,
            {
                accessorPairPositioning: 'getThenSet',
                groups: {
                    'accessor-pairs': [
                        { accessorPair: true },
                    ],
                    methods: [
                        {
                            sort: 'alphabetical',
                            type: 'method',
                        },
                    ],
                    properties: [
                        {
                            sort: 'alphabetical',
                            type: 'property',
                        },
                    ],
                    'static-methods': [
                        {
                            sort: 'alphabetical',
                            static: true,
                            type: 'method',
                        },
                    ],
                    'static-properties': [
                        {
                            sort: 'alphabetical',
                            static: true,
                            type: 'property',
                        },
                    ],
                },
                order: [
                    '[static-properties]',
                    '[static-methods]',
                    '[properties]',
                    'constructor',
                    '[accessor-pairs]',
                    '[methods]',
                ],
            },
        ],

and this class:

export abstract class Foo<T> {
    protected abstract readonly _foo: T;

    public readonly bar: string;

    public readonly baz: string;

    protected constructor(bar: string) {
        this.bar = bar;
        this.baz = bar.trim();
    }

    protected abstract parse(baz?: string): T;

    protected abstract stringify(): string;

    public abstract get zebra(): Foo<T>['_foo'];

    public abstract set zebra(value: T);
}

and you can see from my screenshot that even though zebra() would be fine alphabetically, there's an error because getters/setters should be above methods.

image