sort-vars
要求对同一声明块中的变量进行排序
此规则报告的一些问题可通过 --fix
命令行选项自动修复
在同一块中声明多个变量时,一些开发者更喜欢按字母顺序对变量名称进行排序,以便以后更容易找到必要的变量。其他人则认为它增加了复杂性并成为维护的负担。
¥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"*/
var b, a;
var a, B, c;
var a, A;
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint sort-vars: "error"*/
var a, b, c, d;
var _a = 10;
var _b = 20;
var A, a;
var B, a, c;
从第一个变量开始维护按字母顺序排列的列表,排除任何被视为问题的变量。所以下面的代码会产生两个问题:
¥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"*/
var c, d, a, b;
但是这个,只会产生一个:
¥But this one, will only produce one:
/*eslint sort-vars: "error"*/
var c, d, a, e;
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"ignoreCase": true
(默认false
)忽略变量顺序的大小写敏感¥
"ignoreCase": true
(defaultfalse
) ignores the case-sensitivity of the variables order
ignoreCase
使用 { "ignoreCase": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "ignoreCase": true }
option:
/*eslint sort-vars: ["error", { "ignoreCase": true }]*/
var a, A;
var a, B, c;
何时不使用
¥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 中引入。