Przejdź do głównej zawartości

useSortedAttributes

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

Wymusza sortowanie atrybutów w elementach JSX.

Ta reguła sprawdza, czy właściwości JSX są posortowane w spójny sposób. Właściwości są sortowane alfabetycznie używając naturalnego porządku sortowania. Ta reguła nie będzie traktować spread props jako sortowalnych. Zamiast tego, gdy napotka spread prop, posortuje wszystkie poprzednie nie-spread props aż do najbliższego spread prop, jeśli taki istnieje. To zapobiega łamaniu nadpisywania pewnych właściwości używając spread props.

<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

Ta akcja akceptuje następujące opcje

Ta opcja obsługuje wartości natural i lexicographic. Gdzie natural jest domyślną.

Poniższe zastosuje naturalny porządek sortowania.

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

Poniższe zastosuje leksykograficzny porządek sortowania.

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·opt2=""·opt11=""·opt12=""·/>;
2 2