UNPKG

990 BJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag use of duplicate keys in an object.
3 * @author Ian Christian Myers
4 */
5
6//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 return {
15
16 "ObjectExpression": function(node) {
17
18 // Object that will be a map of properties--safe because we will
19 // prefix all of the keys.
20 var nodeProps = {};
21
22 node.properties.forEach(function(property) {
23 var keyName = property.key.name || property.key.value;
24 var key = property.kind + "-" + keyName;
25
26 if (nodeProps[key]) {
27 context.report(node, "Duplicate key '{{key}}'.", { key: keyName });
28 } else {
29 nodeProps[key] = true;
30 }
31 });
32
33 }
34 };
35
36};