useSortedKeys
¥Summary
-
规则生效日期:
v1.9.0¥Rule available since:
v1.9.0 -
诊断类别:
assist/source/useSortedKeys¥Diagnostic Category:
assist/source/useSortedKeys
如何在编辑器中启用
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 ¥How to configure
{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}¥Description
按自然顺序对 JSON 对象的键进行排序。
¥Sort the keys of a JSON object in natural order.
自然顺序 表示大写字母在前,小写字母在后(例如 A < a < B < b),数字的比较方式与人类的比较方式相同(例如 9 < 10)。
¥Natural order means
that uppercase letters come before lowercase letters (e.g. A < a <
B < b) and numbers are compared in a human way (e.g. 9 < 10).
¥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
此操作接受以下选项
¥This actions accepts following options
sortOrder
Section titled “sortOrder”此选项支持 natural 和 lexicographic 值。natural 是默认设置。
¥This options supports natural and lexicographic values. Where as natural is the default.
以下将应用自然排序。
¥Following will apply the natural sort order.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "natural" } } } } }}{ "val13": 1, "val1": 1, "val2": 1, "val21": 1, "val11": 1}code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ {
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.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "lexicographic" } } } } }}{ "val13": 1, "val1": 1, "val2": 1, "val21": 1, "val11": 1}code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ {
2 │ - ····“val13”:·1,
3 │ - ····“val1”:·1,
4 │ - ····“val2”:·1,
5 │ - ····“val21”:·1,
6 │ - ····“val11”:·1
2 │ + ····“val1”:·1,
3 │ + ····“val11”:·1,
4 │ + ····“val13”:·1,
5 │ + ····“val2”:·1,
6 │ + ····“val21”:·1
7 7 │ }
8 8 │
¥Related links
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
assist/source/useSortedKeys¥Diagnostic Category:
assist/source/useSortedKeys -
来源:
¥Sources:
-
灵感来自
perfectionist/sort-objects¥Inspired from
perfectionist/sort-objects
-
如何在编辑器中启用
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 ¥How to configure
{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}¥Description
按自然顺序对 JS 对象的属性进行排序。
¥Sort properties of a JS object in natural order.
自然顺序 表示大写字母在前,小写字母在后(例如 A < a < B < b),数字的比较方式与人类的比较方式相同(例如 9 < 10)。
¥Natural order means
that uppercase letters come before lowercase letters (e.g. A < a <
B < b) and numbers are compared in a human way (e.g. 9 < 10).
此规则会考虑扩展/计算键,例如 [k]:1 为不可排序。相反,每当遇到不可排序的键时,它会对所有先前的可排序键进行排序,直到遇到最近的不可排序键(如果存在)。此设置可防止使用扩展键破坏某些键的重写。
¥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.
对对象的键进行排序,从技术上讲会改变程序的语义。它会影响类似 Object.getOwnPropertyNames 的操作结果。自 ES2020 起,for-in 循环、Object.keys 和 JSON.stringify 等操作保证按插入顺序处理字符串键。
¥Sorting the keys of an object technically changes the semantics of the
program. It affects the result of operations like
Object.getOwnPropertyNames. Since ES2020, operations like for-in
loops, Object.keys, and JSON.stringify are guaranteed to process
string keys in insertion order.
在此类操作的顺序至关重要的情况下,你可以使用抑制注释来禁用辅助操作:
¥In cases where the order of such operations is important, you can disable the assist action using a suppression comment:
// biome-ignore assist/source/useSortedKeys
¥Examples
const obj = { x: 1, a: 2,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
2 │ - ··x:·1,
3 │ - ··a:·2,
2 │ + ··a:·2,
3 │ + ··x:·1,
4 4 │ };
5 5 │
const obj = { x: 1, ...f, y: 4, a: 2, [calculated()]: true, b: 3, a: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
2 2 │ x: 1,
3 3 │ …f,
4 │ - ··y:·4,
5 │ - ··a:·2,
4 │ + ··a:·2,
5 │ + ··y:·4,
6 6 │ [calculated()]: true,
7 │ - ··b:·3,
8 │ - ··a:·1,
7 │ + ··a:·1,
8 │ + ··b:·3,
9 9 │ };
10 10 │
const obj = { 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,}¥Options
此操作接受以下选项
¥This actions accepts following options
sortOrder
Section titled “sortOrder”此选项支持 natural 和 lexicographic 值。natural 是默认设置。
¥This options supports natural and lexicographic values. Where as natural is the default.
以下将应用自然排序。
¥Following will apply the natural sort order.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "natural" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
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.
{ "assist": { "actions": { "source": { "useSortedKeys": { "options": { "sortOrder": "lexicographic" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
2 │ - ····val13:·1,
3 │ - ····val1:·1,
4 │ - ····val2:·1,
5 │ - ····val21:·1,
6 │ - ····val11:·1,
2 │ + ····val1:·1,
3 │ + ····val11:·1,
4 │ + ····val13:·1,
5 │ + ····val2:·1,
6 │ + ····val21:·1,
7 7 │ };
8 8 │
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号