Blame view

node_modules/eslint/lib/rules/no-dupe-keys.js 1.41 KB
c39994410   Ryan Glover   wip converting to...
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
  /**
   * @fileoverview Rule to flag use of duplicate keys in an object.
   * @author Ian Christian Myers
   * @copyright 2013 Ian Christian Myers. All rights reserved.
   * @copyright 2013 Nicholas C. Zakas. All rights reserved.
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      return {
  
          "ObjectExpression": function(node) {
  
              // Object that will be a map of properties--safe because we will
              // prefix all of the keys.
              var nodeProps = Object.create(null);
  
              node.properties.forEach(function(property) {
  
                  if (property.type !== "Property") {
                      return;
                  }
  
                  var keyName = property.key.name || property.key.value,
                      key = property.kind + "-" + keyName,
                      checkProperty = (!property.computed || property.key.type === "Literal");
  
                  if (checkProperty) {
                      if (nodeProps[key]) {
                          context.report(node, property.loc.start, "Duplicate key '{{key}}'.", { key: keyName });
                      } else {
                          nodeProps[key] = true;
                      }
                  }
              });
  
          }
      };
  
  };
  
  module.exports.schema = [];