useSortedAttributes (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.0.0 - Diagnostic Category:
assist/source/useSortedAttributes - Sources:
- Same as
react/jsx-sort-props
- Same as
How to enable in your editor
Section titled “How to enable in your editor”{ "editor.codeActionsOnSave": { "source.action.useSortedAttributes.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedAttributes.biome": true, "source.fixAll.biome": true }}source.action.useSortedAttributes.biome How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedAttributes": "on" } } }}Description
Section titled “Description”Sort JSX attributes by name.
By default, the action uses natural order,
which compares numbers by value so that prop9 comes before prop10.
A spread attribute such as {...properties} can provide or replace any attribute. The
action therefore treats each spread as a boundary and sorts only the named attributes on
each side. It never moves an attribute across a spread.
Examples
Section titled “Examples”<Hello lastName="Smith" firstName="John" />;code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <Hello·lastName="Smith"·firstName="John"·/>;
1 │ + <Hello·firstName="John"·lastName="Smith"·/>;
2 2 │
<Hello lastName="Smith" firstName="John" {...this.props} tel="0000" address="111 Main Street" {...another.props} />;code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <Hello·lastName="Smith"·firstName="John"·{...this.props}·tel="0000"·address="111·Main·Street"·{...another.props}·/>;
1 │ + <Hello·firstName="John"·lastName="Smith"·{...this.props}·tel="0000"·address="111·Main·Street"·{...another.props}·/>;
2 2 │
code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <Hello·lastName="Smith"·firstName="John"·{...this.props}·tel="0000"·address="111·Main·Street"·{...another.props}·/>;
1 │ + <Hello·lastName="Smith"·firstName="John"·{...this.props}·address="111·Main·Street"·tel="0000"·{...another.props}·/>;
2 2 │
Options
Section titled “Options”sortOrder
Section titled “sortOrder”Selects natural or lexicographic ordering. Natural ordering compares numbers by value
and is the default. Lexicographic ordering compares names character by character.
The following configuration uses natural order:
{ "assist": { "actions": { "source": { "useSortedAttributes": { "level": "on", "options": { "sortOrder": "natural" } } } } }}<Hello {...this.props} opt1="" opt2="" opt12="" opt11="" />;code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <Hello·{...this.props}·opt1=""·opt2=""·opt12=""·opt11=""·/>;
1 │ + <Hello·{...this.props}·opt1=""·opt2=""·opt11=""·opt12=""·/>;
2 2 │
The following configuration uses lexicographic order:
{ "assist": { "actions": { "source": { "useSortedAttributes": { "level": "on", "options": { "sortOrder": "lexicographic" } } } } }}<Hello {...this.props} opt1="" opt2="" opt12="" opt11="" />;code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <Hello·{...this.props}·opt1=""·opt2=""·opt12=""·opt11=""·/>;
1 │ + <Hello·{...this.props}·opt1=""·opt11=""·opt12=""·opt2=""·/>;
2 2 │
sortFirst
Section titled “sortFirst”A list of attribute names that should be sorted before all other attributes,
in the order they appear in this list. The remaining attributes are sorted
after the listed ones. This is useful to keep attributes such as key first.
{ "assist": { "actions": { "source": { "useSortedAttributes": { "level": "on", "options": { "sortFirst": [ "key" ] } } } } }}<Hello firstName="John" key={id} lastName="Smith" />;code-block.jsx:1:1 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <Hello firstName="John" key={id} lastName="Smith" />;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the JSX props.
1 │ - <Hello·firstName="John"·key={id}·lastName="Smith"·/>;
1 │ + <Hello·key={id}·firstName="John"·lastName="Smith"·/>;
2 2 │
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.