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

feat: adds fallbackSort option to all rules #457

Merged
merged 26 commits into from
Feb 11, 2025

Conversation

hugop95
Copy link
Contributor

@hugop95 hugop95 commented Jan 31, 2025

Resolves #429

Most of the code added are documentation and tests.

The two commits responsible for the implementation are:

Description

This PR adds a fallbackSort option to all rules:

Typings

{
  fallbackSort: {
    type: 'alphabetical' | 'natural' | 'line-length' | 'custom'
    order: 'asc' | 'desc'
  }[]
}

This option is currently useful for the line-length sort type: this enables users to define an additional sorting algorithm when two elements have the same length.

The option allows an array in case additional sorting types which can result in elements being equals are added later (such as sorting by usage).

What is the purpose of this pull request?

  • New Feature

@hugop95 hugop95 changed the title feat: adds fallbackSort option feat: adds fallbackSort option to all rules Jan 31, 2025
@hugop95 hugop95 force-pushed the feat/fallbackSort branch 7 times, most recently from da2686b to efca022 Compare February 6, 2025 12:50
@hugop95 hugop95 force-pushed the feat/fallbackSort branch 3 times, most recently from efca022 to 7ef7972 Compare February 8, 2025 21:49
@hugop95 hugop95 marked this pull request as ready for review February 8, 2025 21:58
{
type: 'line-length',
order: 'desc'
fallbackSort: [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it should be an array? Why not object?

Copy link
Contributor Author

@hugop95 hugop95 Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io 👍 I changed it to an object here: 4d4b494 (#457)

At first, I wanted an array because we might need to define multiple fallback sorts later, if we implement #434 for example (users might want to sort by usage > sort by line-length > sort by alphabetical order, in which case we would need an array).

But today, we don't need it yet, so it makes sense to directly use an object. We can later allow fallbackSort: { type: string; order?: 'asc' | 'desc' } | { type: string; order?: 'asc' | 'desc' }[] if needed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples in the documentation use arrays ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io Oops! Thank you, fixed in 8f6d97a.

[
'aa',
'b',
'c',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.

But I think the tests would be better if we also checked that sorting by line length works and is prioritized.

{
  code: dedent`
    [
      'c',
      'bbb',
      'a',
    ].includes(value)
  `,
  output: dedent`
    [
      'bbb',
      'a',
      'c',
    ].includes(value)
  `,
}

Copy link
Contributor Author

@hugop95 hugop95 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io Updated in 5d102cc.

@azat-io
Copy link
Owner

azat-io commented Feb 11, 2025

Nice feature, thank you!

@azat-io azat-io merged commit f639d94 into azat-io:main Feb 11, 2025
8 checks passed
@hugop95 hugop95 deleted the feat/fallbackSort branch February 11, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: sort items shared a same length after sort by line-length
2 participants