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{ "code_actions_on_format": { "source.action.useSortedKeys.biome": true, }}
{ "editor.codeActionsOnSave": { "source.action.useSortedKeys.biome": "explicit", }}
Use the source action code
source.action.useSortedKeys.biome
Description
Section titled DescriptionSorts 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 │ }
How to configure
Section titled How to configure{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}
Summary
Section titled Summary- 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{ "code_actions_on_format": { "source.action.useSortedKeys.biome": true, }}
{ "editor.codeActionsOnSave": { "source.action.useSortedKeys.biome": "explicit", }}
Use the source action code
source.action.useSortedKeys.biome
Description
Section titled DescriptionEnforce 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.
Source: https://perfectionist.dev/rules/sort-objects
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 │
How to configure
Section titled How to configure{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}