8d4b6a8980dd4430683062e94b224d4ba0460ea0
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to require sorting of variables within a single Variable Declaration block
3  * @author Ilya Volodin
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
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                 var lastVariableName = memo.id.name,
21                     currenVariableName = decl.id.name;
22
23                 if (ignoreCase) {
24                     lastVariableName = lastVariableName.toLowerCase();
25                     currenVariableName = currenVariableName.toLowerCase();
26                 }
27
28                 if (currenVariableName < lastVariableName) {
29                     context.report(decl, "Variables within the same declaration block should be sorted alphabetically");
30                     return memo;
31                 } else {
32                     return decl;
33                 }
34             }, node.declarations[0]);
35         }
36     };
37 };