UNPKG

1.44 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag use of duplicate keys in an object.
3 * @author Ian Christian Myers
4 * @copyright 2013 Ian Christian Myers. All rights reserved.
5 * @copyright 2013 Nicholas C. Zakas. All rights reserved.
6 */
7
8"use strict";
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14module.exports = function(context) {
15
16 return {
17
18 "ObjectExpression": function(node) {
19
20 // Object that will be a map of properties--safe because we will
21 // prefix all of the keys.
22 var nodeProps = Object.create(null);
23
24 node.properties.forEach(function(property) {
25
26 if (property.type !== "Property") {
27 return;
28 }
29
30 var keyName = property.key.name || property.key.value,
31 key = property.kind + "-" + keyName,
32 checkProperty = (!property.computed || property.key.type === "Literal");
33
34 if (checkProperty) {
35 if (nodeProps[key]) {
36 context.report(node, property.loc.start, "Duplicate key '{{key}}'.", { key: keyName });
37 } else {
38 nodeProps[key] = true;
39 }
40 }
41 });
42
43 }
44 };
45
46};
47
48module.exports.schema = [];