sort-keys
要求对对象键进行排序
在声明多个属性时,一些开发人员更喜欢按字母顺序对属性名称进行排序,以便以后更容易找到和/或区分必要的属性。其他人则认为它增加了复杂性并成为维护的负担。
规则详情
此规则检查对象表达式的所有属性定义并验证所有变量是否按字母顺序排序。
此规则的错误代码示例:
/*eslint sort-keys: "error"*/
/*eslint-env es6*/
let obj = {a: 1, c: 3, b: 2};
let obj = {a: 1, "c": 3, b: 2};
// Case-sensitive by default.
let obj = {a: 1, b: 2, C: 3};
// Non-natural order by default.
let obj = {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 obj = {a: 1, ["c"]: 3, b: 2};
let obj = {a: 1, [S]: 3, b: 2};
此规则的正确代码示例:
/*eslint sort-keys: "error"*/
/*eslint-env es6*/
let obj = {a: 1, b: 2, c: 3};
let obj = {a: 1, "b": 2, c: 3};
// Case-sensitive by default.
let obj = {C: 3, a: 1, b: 2};
// Non-natural order by default.
let obj = {1: a, 10: b, 2: c};
// This rule checks computed properties which have a simple name as well.
let obj = {a: 1, ["b"]: 2, c: 3};
let obj = {a: 1, [b]: 2, c: 3};
// This rule ignores computed properties which have a non-simple name.
let obj = {a: 1, [c + d]: 3, b: 2};
let obj = {a: 1, ["c" + "d"]: 3, b: 2};
let obj = {a: 1, [`${c}`]: 3, b: 2};
let obj = {a: 1, [tag`c`]: 3, b: 2};
// This rule does not report unsorted properties that are separated by a spread property.
let obj = {b: 1, ...c, a: 2};
选项
{
"sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
}
R23avpFXMH7I8cq18eC63BjxVI5TnHhw6jiT9sqwc5wq0u2qD3Ub/D75oLssFizltIVGqmmmqiz8NCpRUsh5z/m/2u42m/M2t8o/MHRNO/o=
- xBfWnuC+yXM0fglJc8PgG5HpBEuWbbbqjMo+IdYG1+W/Y4hwkw68qeaFjFYWWJIkVLFr7EQBSqNcCRUGhTWugSxR6dogRw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pkOfhOqT2Qhb19B3jx/wZ0XdPocg8sLusI2bbtmE1SawjqrC3ljKJT4ZAL5zbkL6oQt07ro6AkTr9x6KpShYQABLsD502mzdIJ01jsWt6h5/ifZHpENRFHqqrvgnBOO/uRmg26i5kUnaM7P1uDf32ldwF0K/gCRSR6n0jIrgAOxEcVI9tD4HgHcUfF5KRLt4myFG0oshooPrRE1Ff95CuOz2SASk3aBduhlamCBcv+t2G7CyM3HMtzRVT16ZEGhi8RapQ3wwRvzjADrjgtRwKO/fSvCnPK0BSOL1ssYQbSE8y6t1Bo4qtHXDy7pp6KfDwjK0fDNcERFs8FFFBA1NcpKYP4c=
nSK/0njx991jeJkRq+5OvQ==
G11Co0nRaeAHmX28NRJ5TIAsXXFSKwYCAtOvRxbPrF3fy9hFc7ZuIr0eFMPrqxXiqsiIkb6KTgN1eD9Lr1e4px4Mg0wat68LQWwaTLUpH1c=
G11Co0nRaeAHmX28NRJ5TCrMHDqxkWxemaUvxzSMV0GtU/bj1Is+e53p/BdbkZH1K15IER4FFyKmLKes5UtJ97gg7kHCSvtMmHcxKDZo4hk=
desc
KL5FlrHUDxK/6isYUa7b8ncbdEHyTseYli+pDHPy/CWHWlalG7JoHArMckig4Nkvqg4t6XVcVos/z+265lHntQ==
/*eslint sort-keys: ["error", "desc"]*/
/*eslint-env es6*/
let obj = {b: 2, c: 3, a: 1};
let obj = {"b": 2, c: 3, a: 1};
// Case-sensitive by default.
let obj = {C: 1, b: 3, a: 2};
// Non-natural order by default.
let obj = {10: b, 2: c, 1: a};
KL5FlrHUDxK/6isYUa7b8hhuuMbT2k1Hft84O4J49GVFAxcM6Q6LgzngAu703Pz98KnkcjEgMM4fEg/AZZ6qdw==
/*eslint sort-keys: ["error", "desc"]*/
/*eslint-env es6*/
let obj = {c: 3, b: 2, a: 1};
let obj = {c: 3, "b": 2, a: 1};
// Case-sensitive by default.
let obj = {b: 3, a: 2, C: 1};
// Non-natural order by default.
let obj = {2: c, 10: b, 1: a};
insensitive
rl0Lh0wQekjn+PbP3EQMiX+fH+6lssIXewm+zmftaUYsCWRi5/+lPsRNGtzgqcjMUVH67jIJq3kbTtCiJef4yRVa6ZvXNb1WAp3QLajWx/A=
/*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
/*eslint-env es6*/
let obj = {a: 1, c: 3, C: 4, b: 2};
let obj = {a: 1, C: 3, c: 4, b: 2};
rl0Lh0wQekjn+PbP3EQMiX+fH+6lssIXewm+zmftaUaO31rWrDlpcuYzlvlFdleYKm7B1bJt7QffAopGudj+vEpggz2AN9ugLmaAj+iNmMg=
/*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
/*eslint-env es6*/
let obj = {a: 1, b: 2, c: 3, C: 4};
let obj = {a: 1, b: 2, C: 3, c: 4};
natural
C+ar8DBi3r6qtUwtngoqo7TbGwE81FL8pV7Eiu/sXObKf1jfnDi/sHT+9q7oZBxaqXbi8g1r3hAcNrERx9O4+A==
/*eslint sort-keys: ["error", "asc", {natural: true}]*/
/*eslint-env es6*/
let obj = {1: a, 10: c, 2: b};
C+ar8DBi3r6qtUwtngoqo7TbGwE81FL8pV7Eiu/sXOb2GQ6DMTsgzPUXAFJhNawnOXcySLEN7ipSe9K0hJrOeg==
/*eslint sort-keys: ["error", "asc", {natural: true}]*/
/*eslint-env es6*/
let obj = {1: a, 2: b, 10: c};
minKeys
5iYCCj5dgwEiNhK5TQiVdLiN+/mw422dF3rMUZ4pR6/zRIxN9ua0xYKfDzBS5s9n0cPpPfmoai5O1esBgKe+pA==
/*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
/*eslint-env es6*/
// 4 keys
let obj = {
b: 2,
a: 1, // not sorted correctly (should be 1st key)
c: 3,
d: 4,
};
// 5 keys
let obj = {
2: 'a',
1: 'b', // not sorted correctly (should be 1st key)
3: 'c',
4: 'd',
5: 'e',
};
5iYCCj5dgwEiNhK5TQiVdLiN+/mw422dF3rMUZ4pR69nMjtZyjTozwQkp7Mo0m0lEdYqlLoh9TtiWvl8HJVqHg==
/*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
/*eslint-env es6*/
// 3 keys
let obj = {
b: 2,
a: 1,
c: 3,
};
// 2 keys
let obj = {
2: 'b',
1: 'a',
};
allowLineSeparatedGroups
dZ86HSB/jCU6AURdNRMGUPqDu5Qxv1XYDumKtTanGu91MTn9v13yiJ7clJiOMf0yyE+Gio/NQYtI6AkcfBh0/07KVw1kjTQUBEzqrPi2ndY=
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
/*eslint-env es6*/
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
};
dZ86HSB/jCU6AURdNRMGUPqDu5Qxv1XYDumKtTanGu91MTn9v13yiJ7clJiOMf0yTfOeQE5uhG8hSlG+4UPyPgKIdNjBJx/Cx4GrNLZUxow=
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
/*eslint-env es6*/
let obj = {
e: 1,
f: 2,
g: 3,
a: 4,
b: 5,
c: 6
}
let obj = {
b: 1,
// comment
a: 4,
c: 5,
}
let obj = {
c: 1,
d: 2,
b () {
},
e: 3,
}
let obj = {
c: 1,
d: 2,
// comment
// comment
b() {
},
e: 4
}
let obj = {
b,
[foo + bar]: 1,
a
}
let obj = {
b: 1
// comment before comma
,
a: 2
};
var obj = {
b: 1,
a: 2,
...z,
c: 3
}
何时不使用
1ca7+/etZakjfXBJtZ8Yi1uT2jkXodCWvLruhJOy29VoQoxxlMHyYBq/f/BbeVJUNzcd7hhGHzEui9CL1WIhIEKe/MYhZUEARbQtZRgEd4w=
兼容性
- sC+/tsaGFq/I8VuKhNqr8l4XVcrERVVaricVxUvueJCsR2teMGl+4lf/ORGS3Ms1cmrE8bI1bH4CBz71uEaXC2UQCyC9Ie1InbLrGiCokt7rQ0efrjIS5Wf3RZHtWgqRP8P7fFDzAfS9VbBp0uuHnw==