Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match non getters / setters methods #45

Open
RuiGau opened this issue Jan 23, 2018 · 1 comment
Open

Match non getters / setters methods #45

RuiGau opened this issue Jan 23, 2018 · 1 comment

Comments

@RuiGau
Copy link

RuiGau commented Jan 23, 2018

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants