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

Feature: (sort-objects) option for destructuring only #126

Open
2 tasks done
jl-compileit opened this issue Apr 12, 2024 · 5 comments · May be fixed by #131
Open
2 tasks done

Feature: (sort-objects) option for destructuring only #126

jl-compileit opened this issue Apr 12, 2024 · 5 comments · May be fixed by #131
Labels
enhancement New feature or request

Comments

@jl-compileit
Copy link

jl-compileit commented Apr 12, 2024

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

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@jl-compileit jl-compileit added the enhancement New feature or request label Apr 12, 2024
@jl-compileit jl-compileit changed the title Feature: option for destructuring only Feature: (sort-objects) option for destructuring only Apr 12, 2024
@KID-joker
Copy link
Contributor

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

Does ignore-pattern solve your problem?

@jl-compileit
Copy link
Author

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.

@KID-joker
Copy link
Contributor

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

@jl-compileit
Copy link
Author

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

@KID-joker
Copy link
Contributor

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants