Blame view

node_modules/eslint/lib/rules/sort-vars.js 1.87 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  /**
   * @fileoverview Rule to require sorting of variables within a single Variable Declaration block
   * @author Ilya Volodin
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = {
      meta: {
          docs: {
              description: "require variables within the same declaration block to be sorted",
              category: "Stylistic Issues",
              recommended: false
          },
  
          schema: [
              {
                  type: "object",
                  properties: {
                      ignoreCase: {
                          type: "boolean"
                      }
                  },
                  additionalProperties: false
              }
          ]
      },
  
      create(context) {
  
          const configuration = context.options[0] || {},
              ignoreCase = configuration.ignoreCase || false;
  
          return {
              VariableDeclaration(node) {
                  const idDeclarations = node.declarations.filter(decl => decl.id.type === "Identifier");
  
                  idDeclarations.slice(1).reduce((memo, decl) => {
                      let lastVariableName = memo.id.name,
                          currenVariableName = decl.id.name;
  
                      if (ignoreCase) {
                          lastVariableName = lastVariableName.toLowerCase();
                          currenVariableName = currenVariableName.toLowerCase();
                      }
  
                      if (currenVariableName < lastVariableName) {
                          context.report({ node: decl, message: "Variables within the same declaration block should be sorted alphabetically." });
                          return memo;
                      }
                      return decl;
  
                  }, idDeclarations[0]);
              }
          };
      }
  };