eslint-plugin-sort-class-members icon indicating copy to clipboard operation
eslint-plugin-sort-class-members copied to clipboard

A group should only match if a former one didn't match already

Open DamienCassou opened this issue 4 years ago • 4 comments

Here is a typical sorting we want:

class Foo extends MyFrameworkObject {
  _initialize() { ... }

  doSomething() { ... }
  _partOfSomething() { ... }
}

More explicitely:

  • _initialize() should always come first (this is a kind of constructor)
  • then should come public methods
  • then should come private methods except _initialize()

I don't know how to specify that private methods but _initialize() should come last. Whatever I try, I always get an error message saying that doSomething() should arrive before _initialize(). Here is an example configuration that doesn't work:

[
    2,
    {
	order: [
	    "[initialize-methods]",
	    "[public-methods]",
	    "[private-methods]",
	],
	groups: {
	    "initialize-methods": [
		{
		    name: "/^_initialize/",
		    type: "method",
		},
	    ],
	    "public-methods": [
		{
		    name: "/^[^_].+/",
		    type: "method",
		    static: false,
		},
	    ],
	    "private-methods": [
		{
		    name: "/^_.+/",
		    type: "method",
		    static: false,
		},
	    ],
	},
    },
]

DamienCassou avatar Jan 20 '21 20:01 DamienCassou

I have another similar problem: some classes have rendering methods whose name contains "render". A rendering method can be either public or private. I want this order:

  1. [initialize-methods]
  2. [public-non-rendering-methods]
  3. [public-rendering-methods]
  4. [private-rendering-methods]
  5. [private-non-rendering-methods]

From what I understand, I can't implement this scheme for the same reason as the one explained in the issue description message.

DamienCassou avatar Jan 22 '21 13:01 DamienCassou

I think that what I need is ordering between groups to specify which ones match more precisely in case of conflict. For example:

{
    groups: {
        "public-methods": [
            {
                name: "/^[^_].+/",
                type: "method",
                static: false,
                order: 2
            },
        ],
        "public-rendering-methods": [
            {
                name: "/^[^_].+render.+/",
                type: "method",
                static: false,
                order: 1
            },
        ]
    }
}

This means that, if a method matches "public-rendering-methods", then this method is not considered a member of "public-methods" because "public-rendering-methods" has a smaller order. Said differently, the order is used to sort the groups and the first one matching wins.

DamienCassou avatar Jan 22 '21 13:01 DamienCassou

ping

DamienCassou avatar Feb 01 '21 08:02 DamienCassou

Hi @DamienCassou, I'd be happy to review a PR if you know the change you'd like to make.

bryanrsmith avatar Feb 01 '21 16:02 bryanrsmith