版本

sort-keys

要求对象键按顺序排序

在声明多个属性时,一些开发者更喜欢按字母顺序对属性名称进行排序,以便在以后更容易地查找和/或差异化必要的属性。另一些人则认为这增加了复杂性,并成为维护的负担。

规则详情

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

此规则的**错误**代码示例

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

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

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

// Non-natural order by default.
let 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")
let obj5 = {a: 1, ["c"]: 3, b: 2};
let obj6 = {a: 1, [S]: 3, b: 2};

此规则的**正确**代码示例

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

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

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

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

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

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

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

选项

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

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

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

第二个选项是一个对象,它具有以下属性。

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

列表示例

如果natural为 true,则排序将为 1 3 6 8 10

如果natural为 false,则排序将为 1 10 3 6 8

desc

"desc"选项的**错误**代码示例

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

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

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

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

"desc"选项的**正确**代码示例

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

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

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

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

caseSensitive

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

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

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

{caseSensitive: false}选项的**正确**代码示例

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

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

natural

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

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

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

{natural: true}选项的**正确**代码示例

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

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

minKeys

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

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

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

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

{minKeys: 4}选项的**正确**代码示例

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

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

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

allowLineSeparatedGroups

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

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

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

    },
    a: 3
}

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

    z () {

    },
    y: 3
}

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

    z () {

    },
    // comment
    y: 3,
}

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

{allowLineSeparatedGroups: true}选项的**正确**代码示例

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

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

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

let obj2 = {
    b: 1,

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

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

    b () {

    },
    e: 3,
}

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

    // comment
    b() {

    },
    e: 4
}

let obj5 = {
    b,

    [foo + bar]: 1,
    a
}

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

    ,
    a: 2
};

var obj7 = {
    b: 1,

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

ignoreComputedKeys

{ignoreComputedKeys: true}选项的**正确**代码示例

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

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

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

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

何时不使用它

如果您不想收到有关属性顺序的通知,那么禁用此规则是安全的。

兼容性

版本

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

资源

更改语言