Skip to content

useSortedAttributes

¥Summary

¥How to enable in your editor

.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useSortedAttributes.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}

¥How to configure

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedAttributes": "on"
}
}
}
}

¥Description

强制 JSX 元素中的属性排序。

¥Enforce attribute sorting in JSX elements.

此规则检查 JSX 属性是否以一致的方式排序。属性按字母顺序排序,使用 自然排序。此规则不会将扩展属性视为可排序的属性。相反,每当遇到扩展属性时,它会对所有先前的非扩展属性进行排序,直到遇到最近的扩展属性(如果存在)。此设置可防止使用扩展属性破坏某些属性的重写。

¥This rule checks if the JSX props are sorted in a consistent way. Props are sorted alphabetically using a natural sort order. This rule will not consider spread props as sortable. Instead, whenever it encounters a spread prop, it will sort all the previous non spread props up until the nearest spread prop, if one exist. This prevents breaking the override of certain props using spread props.

¥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} lastName="Smith" />;
code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

1 - <Hello·lastName=Smith·firstName=John·{...this.props}·tel=0000·address=111·Main·Street··{...another.props}·lastName=Smith·/>;
1+ <Hello·firstName=John·lastName=Smith·{...this.props}·tel=0000·address=111·Main·Street··{...another.props}·lastName=Smith·/>;
2 2

code-block.jsx ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

1 - <Hello·lastName=Smith·firstName=John·{...this.props}·tel=0000·address=111·Main·Street··{...another.props}·lastName=Smith·/>;
1+ <Hello·lastName=Smith·firstName=John·{...this.props}·address=111·Main·Street·tel=0000··{...another.props}·lastName=Smith·/>;
2 2

¥Options

此操作接受以下选项

¥This actions accepts following options

此选项支持 naturallexicographic 值。natural 是默认设置。

¥This options supports natural and lexicographic values. Where as natural is the default.

以下将应用自然排序。

¥Following will apply the natural sort order.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedAttributes": {
"options": {
"sortOrder": "natural"
}
}
}
}
}
}
<Hello tel={5555555} {...this.props} opt1="John" opt2="" opt12="" opt11="" />;
code-block.jsx:1:1 assist/source/useSortedAttributes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The attributes are not sorted.

> 1 │ <Hello tel={5555555} {…this.props} opt1=“John” opt2="" opt12="" opt11="" />;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Safe fix: Sort the JSX props.

1 - <Hello·tel={5555555}·{...this.props}·opt1=John·opt2=""·opt12=""·opt11=""·/>;
1+ <Hello·tel={5555555}·{...this.props}·opt1=John·opt2=""·opt11=""·opt12=""·/>;
2 2

以下将应用字典序排序。

¥Following will apply the lexicographic sort order.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedAttributes": {
"options": {
"sortOrder": "lexicographic"
}
}
}
}
}
}
<Hello tel={5555555} {...this.props} opt1="John" opt2="" opt12="" opt11="" />;
code-block.jsx:1:1 assist/source/useSortedAttributes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The attributes are not sorted.

> 1 │ <Hello tel={5555555} {…this.props} opt1=“John” opt2="" opt12="" opt11="" />;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Safe fix: Sort the JSX props.

1 - <Hello·tel={5555555}·{...this.props}·opt1=John·opt2=""·opt12=""·opt11=""·/>;
1+ <Hello·tel={5555555}·{...this.props}·opt1=John·opt11=""·opt12=""·opt2=""·/>;
2 2

¥Related links