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: Customizable sort-array #422

Open
2 tasks done
hugop95 opened this issue Dec 17, 2024 · 9 comments
Open
2 tasks done

Feature: Customizable sort-array #422

hugop95 opened this issue Dec 17, 2024 · 9 comments
Labels
enhancement New feature or request

Comments

@hugop95
Copy link
Contributor

hugop95 commented Dec 17, 2024

Describe the rule

I often build enums and associated types from constant arrays in my projects:

export const NOTIFICATION_OPTIONS = ["userAdded", "userModified"] as const;
export type NotificationOption = typeof NOTIFICATION_OPTIONS[number];

I would like all of those arrays to be sorted.

Today, sort-array-includes and sort-sets don't affect them.

Current rules

sort-array-includes and sort-sets generally do the same things: they sort arrays under different condition. This feature proposal is also related to sorting arrays, but with another different condition.

Proposal

Create a new rule sort-array while allowing users to choose the configuration they want depending on their needs. sort-array-includes and sort-sets could be deprecated until next major version release.

Example configuration with useConfigurationIf (placeholder option names)

{
  'perfectionist/sort-array': [
    'error',
    {
      useConfigurationIf: {
        anyOf: [
		  {
			isSetDeclaration: true // Sort sets
		  },
		  {
			isArrayDeclaration: true,
			calledMethodMatchesPattern: "^includes$" // Sort array includes
		  },
		  {
			isConstantDeclaration: true,
			hasNoCalledMethod: true // Sort array enums
		  }
		]
      },
    },
	{
	  type: 'unsorted' // Fallback configuration: don't sort anything else
	}
  ],
}

Advantages

  • Highly customizable.
  • Easy to add additional options later on.

Cons

  • Requires configuring.

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@hugop95 hugop95 added the enhancement New feature or request label Dec 17, 2024
@hugop95
Copy link
Contributor Author

hugop95 commented Dec 19, 2024

@azat-io @OlivierZal

What do you think about this? Curious to hear your opinions?

@azat-io
Copy link
Owner

azat-io commented Dec 19, 2024

Looks good. But it's going to be a breaking change. And I'd rather do it later. Since we had a major release recently.

@hugop95
Copy link
Contributor Author

hugop95 commented Dec 19, 2024

@azat-io We could keep sort-array-includes and sort-sets while also having sort-arrays, users would not have to migrate immediately.

@OlivierZal
Copy link
Contributor

OlivierZal commented Dec 19, 2024

@hugop95, I like the idea very much: powerful and I confess I would use it since there are many contexts where order doesn't matter.

Just wondering if sort-arrays isn't too scary, because an array is ordered by definition, and it's maybe to narrow to reflect it could also sort sets and others. Maybe sort-array-like?

@azat-io
Copy link
Owner

azat-io commented Dec 19, 2024

I think it's better to have two separate sort-arrays and sort-sets rules.

@azat-io
Copy link
Owner

azat-io commented Dec 19, 2024

@hugop95 What would be the default settings for this rule?

@hugop95
Copy link
Contributor Author

hugop95 commented Dec 19, 2024

@OlivierZal Placeholder name for now! We can think about that later!

@azat-io A configuration that does what sort-array-includes/sort-sets do by default. Maybe also sorts const arrays of string as well?

@azat-io
Copy link
Owner

azat-io commented Dec 20, 2024

I'm a little concerned that this is a pretty insecure feature and the order of the arrays matters. And code safety is a top priority for us.

So it makes sense to discuss how it will work.

@hugop95
Copy link
Contributor Author

hugop95 commented Dec 20, 2024

@azat-io

I'm a little concerned that this is a pretty insecure feature and the order of the arrays matters

By default, the rule would do the same as what sort-array-includes does today:

  • Only target arrays called with includes directly.
  • However, we allow the user to override the useConfigurationIf option to choose exactly what arrays they want to target thanks to filters such as:
    • Is the array a constant
    • Is the array an array of string
    • A pattern to match the function called on this array

For example:

Default configuration to sort the same as array-includes

{
  'perfectionist/sort-array': [
    'error',
    {
      useConfigurationIf: {
        anyOf: [
		  {
			isArrayDeclaration: true,
			calledMethodMatchesPattern: "^includes$" // Sort array includes
		  }
		]
      },
    },
    {
	  type: 'unsorted' // Fallback configuration: don't sort anything else
    }
  ],
}

The main drawbacks is that:

  • It's less "direct" than a sort-array-includes rule that will by default target only those types of arrays.
  • If a user wants to override useConfigurationIf, they have to remember to keep the anyOf part of the default configuration to keep targeting the array.includes() elements.

Advantages:

  • High customization for users who want it.

Let me know if something isn't clear!

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
Development

No branches or pull requests

3 participants