1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function(context) {
|
13 |
|
14 | var configuration = context.options[0] || {},
|
15 | ignoreCase = configuration.ignoreCase || false;
|
16 |
|
17 | return {
|
18 | "VariableDeclaration": function(node) {
|
19 | node.declarations.reduce(function(memo, decl) {
|
20 | if (decl.id.type === "ObjectPattern" || decl.id.type === "ArrayPattern") {
|
21 | return memo;
|
22 | }
|
23 |
|
24 | var lastVariableName = memo.id.name,
|
25 | currenVariableName = decl.id.name;
|
26 |
|
27 | if (ignoreCase) {
|
28 | lastVariableName = lastVariableName.toLowerCase();
|
29 | currenVariableName = currenVariableName.toLowerCase();
|
30 | }
|
31 |
|
32 | if (currenVariableName < lastVariableName) {
|
33 | context.report(decl, "Variables within the same declaration block should be sorted alphabetically");
|
34 | return memo;
|
35 | } else {
|
36 | return decl;
|
37 | }
|
38 | }, node.declarations[0]);
|
39 | }
|
40 | };
|
41 | };
|
42 |
|
43 | module.exports.schema = [
|
44 | {
|
45 | "type": "object",
|
46 | "properties": {
|
47 | "ignoreCase": {
|
48 | "type": "boolean"
|
49 | }
|
50 | },
|
51 | "additionalProperties": false
|
52 | }
|
53 | ];
|