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(sort-objects): Adds ignore rule for objects passed as arguments to function calls #420

Merged
merged 12 commits into from
Dec 15, 2024

Conversation

hugop95
Copy link
Contributor

@hugop95 hugop95 commented Dec 13, 2024

Resolves #413

Changes

  • Adds a useConfigurationIf.callingFunctionNamePattern option to sort-objects. Object that are not parameters of a function will not be matched by configurations using this option.
  • Allows users to enter unsorted as type.

Example configuration:

{
  'perfectionist/sort-objects': [
    'error',
    {
      type: 'unsorted', // Don't sort objects passed to createSlice
      useConfigurationIf: {
        callingFunctionNamePattern: '^createSlice$',
      },
    },
    {
      type: 'alphabetical' // Fallback configuration
    }
  ],
}

What is the purpose of this pull request?

  • New Feature

@hugop95 hugop95 force-pushed the feat/sort-objects/function-names branch from eddaeb4 to 4eb6c9c Compare December 13, 2024 23:34
* Rules with `useConfigurationIf` will be able to use the `unsorted` type.
- `let options = { ...completeOptions, type }` is necessary for compilation: this allows the compiler to know that `options.type` can not be `unsorted`. I wish it would detect it without having to do that, but it doesn't. If you have a cleaner way, feel free to suggest.
- Same comment as the previous commit.
- It will return the list of matching context options instead of the first matched context options.
- This will allow us to add additional custom filters for `sort-objects` in next commit.
- Returned parents will have their types correctly inferred.
@hugop95 hugop95 force-pushed the feat/sort-objects/function-names branch from 4eb6c9c to 8eb9a79 Compare December 14, 2024 14:59
@hugop95 hugop95 force-pushed the feat/sort-objects/function-names branch from 8eb9a79 to 630e8c2 Compare December 15, 2024 10:14
@hugop95 hugop95 marked this pull request as ready for review December 15, 2024 10:25
@@ -129,6 +129,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useConfigurationIf) option.
Copy link
Owner

Choose a reason for hiding this comment

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

What do you think of the name 'no-sort' or 'preserve-order'? Or do you prefer 'unsorted'?

Copy link
Contributor Author

@hugop95 hugop95 Dec 15, 2024

Choose a reason for hiding this comment

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

@azat-io

  • I prefer unsorted and no-sort over preserve-order (I feel the first two are more explicit).
  • unsorted is already used in customGroups.
    type CustomGroup = (
    | {
    order?: SortClassesOptions[0]['order']
    type?: SortClassesOptions[0]['type']
    }
    | {
    type?: 'unsorted'
    }
    ) &
    (SingleCustomGroup | AnyOfCustomGroup) & {
    groupName: string
    }

I think it's better for consistency to keep the same name, so I would personally pick unsorted.

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, thank you 👍

@azat-io
Copy link
Owner

azat-io commented Dec 15, 2024

Do I understand correctly that we should remove the sort-objects scope from commit?

@hugop95
Copy link
Contributor Author

hugop95 commented Dec 15, 2024

Do I understand correctly that we should remove the sort-objects scope from commit?

Sorry, I'm not sure to understand! What do you mean?

@azat-io
Copy link
Owner

azat-io commented Dec 15, 2024

@hugop95 You added scope in commit message feat(sort-objects)

@hugop95
Copy link
Contributor Author

hugop95 commented Dec 15, 2024

@azat-io you can remove it you want, the PR does affects other rules as well (by adding the type: unsorted to some other rules too)

@azat-io azat-io merged commit 41c9f4f into azat-io:main Dec 15, 2024
8 checks passed
@hugop95 hugop95 deleted the feat/sort-objects/function-names branch December 15, 2024 22:34
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-objects - add ignore rule for objects passed as arguments to function calls
2 participants