question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enhance jsx-sort-props to allow for non-alphabetically sorted groupings

See original GitHub issue

Is there any interest in including a rule like the following in this plugin?

https://github.com/ericmatthys/eslint-plugin-jsx-grouped-sort-props

This is a rule that allows sorting of jsx props in groups without requiring an alphabetical sort. My motivation was wanting specific props sorted first (e.g. key and ref), then all other non-callback props in any order, then all callback props in any order at the end. The rule allows specific prop names in an array or various pre-configured groups (e.g. html, reserved, callback). If there is a spread, it validates the prop order before and after the spread independently. For the use case above, the configuration would look like:

[
  'key',
  'ref',
  { group: 'other' },
  { group: 'callback' }
]

Valid:

<Button
  ref="btn"
  isLoading={true}
  onClick={onClick}
/>
<Button
  ref="btn"
  onClick={onClick}
  {...props}
  isLoading={true}
/>

Invalid:

<Button
  isLoading={true}
  ref="btn"
  onClick={onClick}
/>
<Button
  ref="btn"
  onClick={onClick}
  isLoading={true}
/>
<Button
  ref="btn"
  {...props}
  onClick={onClick}
  isLoading={true}
/>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
Daniel15commented, Sep 12, 2016

Sounds like an enhanced version of jsx-sort-props. There’s already a callbacksLast configuration option.

0reactions
ljharbcommented, Feb 2, 2022

I’ve rethought my opinion here.

While this would be a possible (feasible) option on jsx-sort-props, I think it would not be a good idea. Enforcing a magic, arbitrary sorting of props that isn’t based on something you can see by looking directly at the prop names would be incredibly confusing, and relying on the autofixer to understand what the “right” thing doesn’t remove that problem.

I think that it’s best that props either be unordered, or, ordered by simple-to-understand rules and categories, which currently include “reserved by react” props like key and ref etc, “alphabetical”, “boolean shorthand”, and “callbacks”. Arguably, this is already too complex.

I’m going to close this for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enhance jsx-sort-props to allow for non-alphabetically sorted ...
I think a good solution to this would be to have sortFirst and sortLast , which both take arrays of strings or regexes....
Read more >
Sort objects in an array alphabetically on one property of the ...
sort () method to sort this array of objects by the DepartmentName property of each object? The array.sort() method works just fine when...
Read more >
Sorting search results in SharePoint | Microsoft Learn
Sort search results by managed property value: Enables you to sort the search result based on the value of one or more managed...
Read more >
Radix sort: No comparisons required - LogRocket Blog
We cover a special kind of sorting algorithm called radix sort, exploring how it works and how to implement it with JavaScript.
Read more >
Sorting in JavaScript Grid control - Syncfusion
To sort multiple columns, press and hold the CTRL key and click the column header. You can clear sorting of any one of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found