useSortedKeys
Podsumowanie
Dział zatytułowany „Podsumowanie”- Reguła dostępna od:
v1.9.0 - Kategoria diagnostyczna:
assist/source/useSortedKeys - Domyślna ważność tej reguły to informacja.
Jak włączyć w edytorze
Dział zatytułowany „Jak włączyć w edytorze”{ "editor.codeActionsOnSave": { "source.action.useSortedKeys.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedKeys.biome": true, "source.fixAll.biome": true }}source.action.useSortedKeys.biome Jak skonfigurować
Dział zatytułowany „Jak skonfigurować”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}Sortuj klucze obiektu JSON w porządku naturalnym.
Porządek naturalny oznacza,
że wielkie litery występują przed małymi literami (np. A < a <
B < b) i liczby są porównywane w sposób ludzki (np. 9 < 10).
Przykłady
Dział zatytułowany „Przykłady”{ "vase": "fancy", "nested": { "omega": "bar", "alpha": "foo" }}code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ {
2 │ - ····“vase”:·“fancy”,
3 │ - ····“nested”:·{
2 │ + ····“nested”:·{
4 3 │ “omega”: “bar”,
5 4 │ “alpha”: “foo”
6 │ - ····}
5 │ + ····},
6 │ + ····“vase”:·“fancy”
7 7 │ }
8 8 │
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
2 2 │ “vase”: “fancy”,
3 3 │ “nested”: {
4 │ - ········“omega”:·“bar”,
5 │ - ········“alpha”:·“foo”
4 │ + ········“alpha”:·“foo”,
5 │ + ········“omega”:·“bar”
6 6 │ }
7 7 │ }
Ta akcja przyjmuje następujące opcje
sortOrder
Dział zatytułowany „sortOrder”Ta opcja obsługuje wartości natural i lexicographic. Gdzie natural jest domyślne.
Poniższe zastosuje porządek sortowania naturalnego.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "natural" } } } } }}{ "val13": 1, "val1": 1, "val2": 1, "val21": 1, "val11": 1,}code-block.json:7:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a property but instead found ’}‘.
5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
│ ^
8 │
ℹ Expected a property here.
5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
│ ^
8 │
Poniższe zastosuje porządek sortowania leksykograficznego.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "lexicographic" } } } } }}{ "val13": 1, "val1": 1, "val2": 1, "val21": 1, "val11": 1,}code-block.json:7:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a property but instead found ’}‘.
5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
│ ^
8 │
ℹ Expected a property here.
5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
│ ^
8 │
Podsumowanie
Dział zatytułowany „Podsumowanie”- Reguła dostępna od:
v2.0.0 - Kategoria diagnostyczna:
assist/source/useSortedKeys - Domyślna ważność tej reguły to informacja.
- Źródła:
- Inspirowane z
perfectionist/sort-objects
- Inspirowane z
Jak włączyć w edytorze
Dział zatytułowany „Jak włączyć w edytorze”{ "editor.codeActionsOnSave": { "source.action.useSortedKeys.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedKeys.biome": true, "source.fixAll.biome": true }}source.action.useSortedKeys.biome Jak skonfigurować
Dział zatytułowany „Jak skonfigurować”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}Sortuj właściwości obiektu JS w porządku naturalnym.
Porządek naturalny oznacza,
że wielkie litery występują przed małymi literami (np. A < a <
B < b) i liczby są porównywane w sposób ludzki (np. 9 < 10).
Ta reguła będzie traktować klucze spread/obliczane np. [k]: 1 jako nie-sortowalne. Zamiast tego, gdy napotka nie-sortowalny klucz, posortuje wszystkie poprzednie sortowalne klucze aż do najbliższego nie-sortowalnego klucza, jeśli taki istnieje. To zapobiega łamaniu nadpisywania pewnych kluczy używając kluczy spread.
Sortowanie kluczy obiektu technicznie zmienia semantykę
programu. Wpływa na wynik operacji takich jak
Object.getOwnPropertyNames. Od ES2020, operacje takie jak pętle for-in,
Object.keys, i JSON.stringify są gwarantowane do przetwarzania
kluczy stringowych w kolejności wstawiania.
W przypadkach gdzie kolejność takich operacji jest ważna, możesz wyłączyć akcję asystenta używając komentarza tłumiącego:
// biome-ignore assist/source/useSortedKeys
Przykłady
Dział zatytułowany „Przykłady”{ x: 1, a: 2,};code-block.js:3:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a semicolon or an implicit semicolon after a statement, but found none
1 │ {
2 │ x: 1,
> 3 │ a: 2,
│ ^
4 │ };
5 │
ℹ An explicit or implicit semicolon is expected here…
1 │ {
2 │ x: 1,
> 3 │ a: 2,
│ ^
4 │ };
5 │
ℹ …Which is required to end this statement
1 │ {
> 2 │ x: 1,
│ ^^
> 3 │ a: 2,
│ ^^
4 │ };
5 │
{ x: 1, ...f, y: 4, a: 2, [calculated()]: true, b: 3, a: 1,};code-block.js:3:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected an expression but instead found ’…‘.
1 │ {
2 │ x: 1,
> 3 │ …f,
│ ^^^
4 │ y: 4,
5 │ a: 2,
ℹ Expected an expression here.
1 │ {
2 │ x: 1,
> 3 │ …f,
│ ^^^
4 │ y: 4,
5 │ a: 2,
{ get aab() { return this._aab; }, set aac(v) { this._aac = v; }, w: 1, x: 1, ...g, get aaa() { return ""; }, u: 1, v: 1, [getProp()]: 2, o: 1, p: 1, q: 1,}code-block.js:2:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a semicolon or an implicit semicolon after a statement, but found none
1 │ {
> 2 │ get aab() {
│ ^^^
3 │ return this._aab;
4 │ },
ℹ An explicit or implicit semicolon is expected here…
1 │ {
> 2 │ get aab() {
│ ^^^
3 │ return this._aab;
4 │ },
ℹ …Which is required to end this statement
1 │ {
> 2 │ get aab() {
│ ^^^^^^^
3 │ return this._aab;
4 │ },
code-block.js:2:13 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a semicolon or an implicit semicolon after a statement, but found none
1 │ {
> 2 │ get aab() {
│ ^
3 │ return this._aab;
4 │ },
ℹ An explicit or implicit semicolon is expected here…
1 │ {
> 2 │ get aab() {
│ ^
3 │ return this._aab;
4 │ },
ℹ …Which is required to end this statement
1 │ {
> 2 │ get aab() {
│ ^^^^^^^
3 │ return this._aab;
4 │ },
code-block.js:3:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Illegal return statement outside of a function
1 │ {
2 │ get aab() {
> 3 │ return this._aab;
│ ^^^^^^^^^^^^^^^^^
4 │ },
5 │ set aac(v) {
code-block.js:4:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a statement but instead found ’,
set aac(v)‘.
2 │ get aab() {
3 │ return this._aab;
> 4 │ },
│ ^
> 5 │ set aac(v) {
│ ^^^^^^^^^^
6 │ this._aac = v;
7 │ },
ℹ Expected a statement here.
2 │ get aab() {
3 │ return this._aab;
> 4 │ },
│ ^
> 5 │ set aac(v) {
│ ^^^^^^^^^^
6 │ this._aac = v;
7 │ },
code-block.js:7:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a statement but instead found ’,
w: 1,
x: 1,
…g,
get aaa()‘.
5 │ set aac(v) {
6 │ this._aac = v;
> 7 │ },
│ ^
> 8 │ w: 1,
> 9 │ x: 1,
> 10 │ …g,
> 11 │ get aaa() {
│ ^^^^^^^^^
12 │ return "";
13 │ },
ℹ Expected a statement here.
5 │ set aac(v) {
6 │ this._aac = v;
> 7 │ },
│ ^
> 8 │ w: 1,
> 9 │ x: 1,
> 10 │ …g,
> 11 │ get aaa() {
│ ^^^^^^^^^
12 │ return "";
13 │ },
code-block.js:12:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Illegal return statement outside of a function
10 │ …g,
11 │ get aaa() {
> 12 │ return "";
│ ^^^^^^^^^^
13 │ },
14 │ u: 1,
code-block.js:13:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Expected a statement but instead found ’,
u: 1,
v: 1,
[getProp()]: 2,
o: 1,
p: 1,
q: 1,‘.
11 │ get aaa() {
12 │ return "";
> 13 │ },
│ ^
> 14 │ u: 1,
…
> 18 │ p: 1,
> 19 │ q: 1,
│ ^^^^^
20 │ }
21 │
ℹ Expected a statement here.
11 │ get aaa() {
12 │ return "";
> 13 │ },
│ ^
> 14 │ u: 1,
…
> 18 │ p: 1,
> 19 │ q: 1,
│ ^^^^^
20 │ }
21 │
Ta akcja przyjmuje następujące opcje
sortOrder
Dział zatytułowany „sortOrder”Ta opcja obsługuje wartości natural i lexicographic. Gdzie natural jest domyślne.
Poniższe zastosuje porządek sortowania naturalnego.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "natural" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js:2:5 assist/source/useSortedKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The object properties are not sorted by key.
1 │ const obj = {
> 2 │ val13: 1,
│ ^^^^^^^^^
> 3 │ val1: 1,
> 4 │ val2: 1,
> 5 │ val21: 1,
> 6 │ val11: 1,
│ ^^^^^^^^^
7 │ };
8 │
ℹ Safe fix: Sort the object properties by key.
1 1 │ const obj = {
2 │ - ····val13:·1,
3 │ - ····val1:·1,
4 │ - ····val2:·1,
5 │ - ····val21:·1,
6 │ - ····val11:·1,
2 │ + ····val1:·1,
3 │ + ····val2:·1,
4 │ + ····val11:·1,
5 │ + ····val13:·1,
6 │ + ····val21:·1,
7 7 │ };
8 8 │
Poniższe zastosuje porządek sortowania leksykograficznego.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "lexicographic" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js:2:5 assist/source/useSortedKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The object properties are not sorted by key.
1 │ const obj = {
> 2 │ val13: 1,
│ ^^^^^^^^^
> 3 │ val1: 1,
> 4 │ val2: 1,
> 5 │ val21: 1,
> 6 │ val11: 1,
│ ^^^^^^^^^
7 │ };
8 │
ℹ Safe fix: Sort the object properties by key.
1 1 │ const obj = {
2 │ - ····val13:·1,
3 │ - ····val1:·1,
4 │ - ····val2:·1,
5 │ - ····val21:·1,
6 │ - ····val11:·1,
2 │ + ····val1:·1,
3 │ + ····val2:·1,
4 │ + ····val11:·1,
5 │ + ····val13:·1,
6 │ + ····val21:·1,
7 7 │ };
8 8 │
Copyright (c) 2023-present Biome Developers and Contributors.