Index

sort-keys

要求对对象键进行排序

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

在声明多个属性时,一些开发者倾向于按字母顺序排列属性名称,以便在以后更容易找到和/或比较必要的属性。其他人则认为这会增加复杂性,成为维护的负担。

🌐 When declaring multiple properties, some developers prefer to sort property names alphabetically to more easily find and/or diff necessary properties at a later time. Others feel that it adds complexity and becomes burden to maintain.

规则详情

🌐 Rule Details

此规则检查对象表达式的所有属性定义并验证所有变量是否按字母顺序排序。

🌐 This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint sort-keys: "error"*/

const obj1 = {a: 1, c: 3, b: 2};
const obj2 = {a: 1, "c": 3, b: 2};

// Case-sensitive by default.
const obj3 = {a: 1, b: 2, C: 3};

// Non-natural order by default.
const obj4 = {1: a, 2: c, 10: b};

// This rule checks computed properties which have a simple name as well.
// Simple names are names which are expressed by an Identifier node or a Literal node.
const S = Symbol("s")
const obj5 = {a: 1, ["c"]: 3, b: 2};
const obj6 = {a: 1, [S]: 3, b: 2};

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint sort-keys: "error"*/

const obj1 = {a: 1, b: 2, c: 3};
const obj2 = {a: 1, "b": 2, c: 3};

// Case-sensitive by default.
const obj3 = {C: 3, a: 1, b: 2};

// Non-natural order by default.
const obj4 = {1: a, 10: b, 2: c};

// This rule checks computed properties which have a simple name as well.
const obj5 = {a: 1, ["b"]: 2, c: 3};
const obj6 = {a: 1, [b]: 2, c: 3};

// This rule ignores computed properties which have a non-simple name.
const obj7 = {a: 1, [c + d]: 3, b: 2};
const obj8 = {a: 1, ["c" + "d"]: 3, b: 2};
const obj9 = {a: 1, [`${c}`]: 3, b: 2};
const obj10 = {a: 1, [tag`c`]: 3, b: 2};

// This rule does not report unsorted properties that are separated by a spread property.
const obj11 = {b: 1, ...c, a: 2};

选项

🌐 Options

{
    "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
}

第一个选项是 "asc""desc"

🌐 The 1st option is "asc" or "desc".

  • "asc"(default)- 强制属性按升序排列。
  • "desc" - 强制属性按降序排列。

第二个选项是具有以下属性的对象。

🌐 The 2nd option is an object which has the following properties.

  • caseSensitive - 如果 true,强制属性按区分大小写的顺序排列。默认值是 true
  • minKeys - 指定一个对象应该具有的最少键数,以便该对象的未排序键会产生错误。默认值为 2,这意味着默认情况下,所有具有未排序键的对象都会导致代码检查错误。
  • natural - 如果 true,则强制属性按自然顺序排列。默认是 false。自然顺序比较包含字母和数字组合的字符串,就像人类排序的方式一样。它基本上是按数字排序,而不是按字母顺序。因此,在自然排序中,数字 10 会排在数字 3 之后。
  • allowLineSeparatedGroups - 如果 true,该规则允许通过换行符对对象键进行分组。换句话说,在属性后添加空行会重置键的排序。默认是 false
  • ignoreComputedKeys - 如果 true,该规则会忽略所有计算属性键,并且不会报告它们分隔的未排序属性。计算属性键会重置随后非计算属性键的排序。默认值是 false

列表示例:

🌐 Example for a list:

natural 作为 true,排序将是 1 3 6 8 10

🌐 With natural as true, the ordering would be 1 3 6 8 10

natural 作为 false,排序将会是 1 10 3 6 8

🌐 With natural as false, the ordering would be 1 10 3 6 8

desc

针对 "desc" 选项的错误代码示例:

🌐 Examples of incorrect code for the "desc" option:

在线运行
/*eslint sort-keys: ["error", "desc"]*/

const obj1 = {b: 2, c: 3, a: 1};
const obj2 = {"b": 2, c: 3, a: 1};

// Case-sensitive by default.
const obj3 = {C: 1, b: 3, a: 2};

// Non-natural order by default.
const obj4 = {10: b, 2: c, 1: a};

适用于 "desc" 选项的正确代码示例:

🌐 Examples of correct code for the "desc" option:

在线运行
/*eslint sort-keys: ["error", "desc"]*/

const obj1 = {c: 3, b: 2, a: 1};
const obj2 = {c: 3, "b": 2, a: 1};

// Case-sensitive by default.
const obj3 = {b: 3, a: 2, C: 1};

// Non-natural order by default.
const obj4 = {2: c, 10: b, 1: a};

caseSensitive

针对 {caseSensitive: false} 选项的错误代码示例:

🌐 Examples of incorrect code for the {caseSensitive: false} option:

在线运行
/*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/

const obj1 = {a: 1, c: 3, C: 4, b: 2};
const obj2 = {a: 1, C: 3, c: 4, b: 2};

适用于 {caseSensitive: false} 选项的正确代码示例:

🌐 Examples of correct code for the {caseSensitive: false} option:

在线运行
/*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/

const obj1 = {a: 1, b: 2, c: 3, C: 4};
const obj2 = {a: 1, b: 2, C: 3, c: 4};

natural

针对 {natural: true} 选项的错误代码示例:

🌐 Examples of incorrect code for the {natural: true} option:

在线运行
/*eslint sort-keys: ["error", "asc", {natural: true}]*/

const obj = {1: a, 10: c, 2: b};

适用于 {natural: true} 选项的正确代码示例:

🌐 Examples of correct code for the {natural: true} option:

在线运行
/*eslint sort-keys: ["error", "asc", {natural: true}]*/

const obj = {1: a, 2: b, 10: c};

minKeys

针对 {minKeys: 4} 选项的错误代码示例:

🌐 Examples of incorrect code for the {minKeys: 4} option:

在线运行
/*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/

// 4 keys
const obj1 = {
    b: 2,
    a: 1, // not sorted correctly (should be 1st key)
    c: 3,
    d: 4,
};

// 5 keys
const obj2 = {
    2: 'a',
    1: 'b', // not sorted correctly (should be 1st key)
    3: 'c',
    4: 'd',
    5: 'e',
};

适用于 {minKeys: 4} 选项的正确代码示例:

🌐 Examples of correct code for the {minKeys: 4} option:

在线运行
/*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/

// 3 keys
const obj1 = {
    b: 2,
    a: 1,
    c: 3,
};

// 2 keys
const obj2 = {
    2: 'b',
    1: 'a',
};

allowLineSeparatedGroups

针对 {allowLineSeparatedGroups: true} 选项的错误代码示例:

🌐 Examples of incorrect code for the {allowLineSeparatedGroups: true} option:

在线运行
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/

const obj1 = {
    b: 1,
    c () {

    },
    a: 3
}

const obj2 = {
    b: 1,
    c: 2,

    z () {

    },
    y: 3
}

const obj3 = {
    b: 1,
    c: 2,

    z () {

    },
    // comment
    y: 3,
}

const obj4 = {
    b: 1
    // comment before comma
    , a: 2
};

适用于 {allowLineSeparatedGroups: true} 选项的正确代码示例:

🌐 Examples of correct code for the {allowLineSeparatedGroups: true} option:

在线运行
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/

const obj1 = {
    e: 1,
    f: 2,
    g: 3,

    a: 4,
    b: 5,
    c: 6
}

const obj2 = {
    b: 1,

    // comment
    a: 4,
    c: 5,
}

const obj3 = {
    c: 1,
    d: 2,

    b () {

    },
    e: 3,
}

const obj4 = {
    c: 1,
    d: 2,
    // comment

    // comment
    b() {

    },
    e: 4
}

const obj5 = {
    b,

    [foo + bar]: 1,
    a
}

const obj6 = {
    b: 1
    // comment before comma

    ,
    a: 2
};

const obj7 = {
    b: 1,

    a: 2,
    ...z,
    c: 3
}

ignoreComputedKeys

适用于 {ignoreComputedKeys: true} 选项的正确代码示例:

🌐 Examples of correct code for the {ignoreComputedKeys: true} option:

在线运行
/*eslint sort-keys: ["error", "asc", {ignoreComputedKeys: true}]*/

const obj1 = {
    [b]: 1,
    a: 2
}

const obj2 = {
    c: 1,
    [b]: 2,
    a: 3
}

const obj3 = {
    c: 1,
    ["b"]: 2,
    a: 3
}

何时不使用

🌐 When Not To Use It

如果你不想通知属性的顺序,那么禁用此规则是安全的。

🌐 If you don’t want to notify about properties’ order, then it’s safe to disable this rule.

兼容性

🌐 Compatibility

版本

此规则是在 ESLint v3.3.0 中引入。

资源