Variable Sorting (sort-vars)

变量排序 (sort-vars)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --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;


This rule has an object option:


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.



This rule was introduced in ESLint 0.2.0.

该规则在 ESLint 0.2.0 被引入。