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

Alphabetical breaks when using group

Open danielvandenberg95 opened this issue 4 years ago • 3 comments

In functionfunction expandSlot(input, groups) { the following statement is found:

    if (groups.hasOwnProperty(slot.group)) {
      return expandSlot(groups[slot.group], groups);
    } // ignore undefined groups


    return [];
  }

If slot has sorting info, this is discarded. This causes you to be unable to alphabetically sort groups. I'm assuming this is done to use the info from the groups array, as that one has some information that now does not need to be configured any more. A fix would be:

  if (slot.group) {
    if (groups.hasOwnProperty(slot.group)) {
      let copy = {...groups[slot.group], ...slot};
      delete copy.group;
      return expandSlot(copy, groups);
    } // ignore undefined groups


    return [];
  }

Another (separate) issue that comes to light after fixing this, is that the function

function areMembersInCorrectOrder(first, second) {
  return first.acceptableSlots.some(function (a) {
    return second.acceptableSlots.some(function (b) {
      return a.index === b.index && areSlotsAlphabeticallySorted(a, b) ? first.name.localeCompare(second.name) <= 0 : a.index <= b.index;
    });
  });
}

is not suitable for anonymous members (with undefined as first.name). A suggested fix for this would be rewriting the function areSlotsAlphabeticallySorted to become:

function areSlotsAlphabeticallySorted(a, b) {
  return a.sort === 'alphabetical' && b.sort === 'alphabetical' && a.name && b.name;
}

After fixing these two issues the plugin seems to work properly for sorting members alphabetically.

danielvandenberg95 avatar Jan 11 '21 08:01 danielvandenberg95

Any update in this topic @bryanrsmith?

jesusiglesias avatar Jun 01 '21 20:06 jesusiglesias

No. Happy to review a PR if anyone wants to open one.

bryanrsmith avatar Jun 01 '21 20:06 bryanrsmith

@danielvandenberg95 Could you do a PR with the fix?

jesusiglesias avatar Jun 03 '21 06:06 jesusiglesias