sort-vars
要求对同一声明块中的变量进行排序
在同一个代码块中声明多个变量时,一些开发者倾向于按字母顺序排序变量名,以便在以后更容易找到所需的变量。其他人则认为这增加了复杂性,并且维护起来成为负担。
🌐 When declaring multiple variables within the same block, some developers prefer to sort variable names alphabetically to be able to find necessary variable easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
规则详情
🌐 Rule Details
此规则检查所有变量声明块,并验证所有变量是否按字母顺序排序。该规则的默认配置为区分大小写。
🌐 This rule checks all variable declaration blocks and verifies that all variables are sorted alphabetically. The default configuration of the rule is case-sensitive.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint sort-vars: "error"*/
let b, a;
let c, D, e;
let f, F;
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint sort-vars: "error"*/
let a, b, c, d;
let _a = 10;
let _b = 20;
let E, e;
let G, f, h;
按字母顺序的列表从第一个变量开始维护,排除任何被认为是问题的变量。因此,以下代码将产生两个问题:
🌐 Alphabetical list is maintained starting from the first variable and excluding any that are considered problems. So the following code will produce two problems:
/*eslint sort-vars: "error"*/
let c, d, a, b;
但是这个,只会产生一个:
🌐 But this one, will only produce one:
/*eslint sort-vars: "error"*/
let c, d, a, e;
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
"ignoreCase": true(默认false)忽略变量顺序的大小写敏感性
ignoreCase
使用 { "ignoreCase": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreCase": true } option:
/*eslint sort-vars: ["error", { "ignoreCase": true }]*/
let a, A;
let c, D, e;
何时不使用
🌐 When Not To Use It
这条规则是一种格式偏好,不遵循它不会对你的代码质量造成负面影响。如果按字母顺序排列变量不是你编码标准的一部分,那么你可以不使用这条规则。
🌐 This rule is a formatting preference and not following it won’t negatively affect the quality of your code. If you alphabetizing variables isn’t a part of your coding standards, then you can leave this rule off.
相关规则
版本
此规则是在 ESLint v0.2.0 中引入。