eslint-plugin-perfectionist icon indicating copy to clipboard operation
eslint-plugin-perfectionist copied to clipboard

Feature: (sort-objects) option for destructuring only

Open jl-compileit opened this issue 1 year ago • 5 comments

What rule do you want to change?

sort-objects

Describe the problem

We have too many objects that we don't want sorted (e.g. XState machines where properties are grouped logically) to make sort-objects with ignore-pattern and/or ESLint overrides feasible. It would be nice to still have object destructuring available, particularly for React components where currently the props' type get sorted but the prop destructuring does not.

Code example

Input:

const data = {
  software: 'iOS',
  name: 'iPhone 14 Pro',
};
function Product({ software, name }) { ... }

Expected output:

const data = {
  software: 'iOS',
  name: 'iPhone 14 Pro',
};
function Product({ name, software }) { ... }

Additional comments

Not sure if it should be an option to sort-objects or a new/split rule. https://github.com/mskelton/eslint-plugin-sort has separate rules for destructuring-properties and object-properties.

Validations

  • [X] Read the docs.
  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

jl-compileit avatar Apr 12 '24 14:04 jl-compileit

"perfectionist/sort-objects": [
      "error",
      {
        "ignore-pattern": ["data"]
      }
]

Does ignore-pattern solve your problem?

KID-joker avatar Apr 13 '24 02:04 KID-joker

Does ignore-pattern solve your problem?

Unfortunately not; as I said we have many objects in many different places with many different names/no names that we don't want sorted.

jl-compileit avatar Apr 15 '24 08:04 jl-compileit

Okay, I know. How about ignore-pattern supports Function to customize?

KID-joker avatar Apr 15 '24 11:04 KID-joker

Okay, I know. How about ignore-pattern supports Function to customize?

Sure, as long as the function receives enough information to differentiate between "regular" objects and destructuring, regardless of what names they may have or the files they're in. A tiny example of things we want to ignore: https://github.com/statelyai/xstate/blob/main/examples/fetch/src/fetchMachine.ts

jl-compileit avatar Apr 16 '24 08:04 jl-compileit

Okay, I know. How about ignore-pattern supports Function to customize?

@azat-io What do you think of this solution?

KID-joker avatar Apr 16 '24 09:04 KID-joker

Thank you for your issue!

Many thanks to @KID-joker for helping to implement this feature.

See example here.

This feature was implemented in 4fa2b3ebd0c7aa747df2e5cb52faa51c95980391 and released in v3.0.0.

If you'd like, you can support the release with a retweet: https://x.com/azat_io_en/status/1815367279191761054

azat-io avatar Jul 22 '24 12:07 azat-io

I'm sorry, but the option had to be renamed to destructureOnly in v3.1.0.

The point is that we cannot use functions inside ESLint configs, because the options must be serialisable. New flat configs just don't support functions inside options.

We had to redesign the option a bit and make it a boolean value.

azat-io avatar Aug 02 '24 16:08 azat-io