useSortedKeys
Summary
Section titled “Summary”- Rule available since:
v1.9.0
- Diagnostic Category:
assist/source/useSortedKeys
- The default severity of this rule is information.
How to enable in your editor
Section titled “How to enable in your editor”{ "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
Description
Section titled “Description”Sorts the keys of a JSON object in natural order
Examples
Section titled “Examples”{ "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 │ }
Options
Section titled “Options”This actions accepts following options
sortOrder
Section titled “sortOrder”This options supports natural
and lexicographic
values. Where as natural
is the default.
Following will apply the natural sort order.
{ "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 │
Following will apply the lexicographic sort order.
{ "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 │
How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}
Summary
Section titled “Summary”- Rule available since:
v2.0.0
- Diagnostic Category:
assist/source/useSortedKeys
- The default severity of this rule is information.
- Sources:
- Inspired from
perfectionist/sort-objects
- Inspired from
How to enable in your editor
Section titled “How to enable in your editor”{ "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
Description
Section titled “Description”Enforce ordering of a JS object properties.
This rule checks if keys of the object are sorted in a consistent way. Keys are sorted in a natural sort order. This rule will consider spread/calculated keys e.g [k]: 1 as non-sortable. Instead, whenever it encounters a non-sortable key, it will sort all the previous sortable keys up until the nearest non-sortable key, if one exist. This prevents breaking the override of certain keys using spread keys.
Examples
Section titled “Examples”{ 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 │
Options
Section titled “Options”This actions accepts following options
sortOrder
Section titled “sortOrder”This options supports natural
and lexicographic
values. Where as natural
is the default.
Following will apply the natural sort order.
{ "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 │
Following will apply the lexicographic sort order.
{ "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 │
How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}