UNPKG

3.79 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag use of duplicate keys in an object.
3 * @author Ian Christian Myers
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("../ast-utils");
13
14//------------------------------------------------------------------------------
15// Helpers
16//------------------------------------------------------------------------------
17
18const GET_KIND = /^(?:init|get)$/;
19const SET_KIND = /^(?:init|set)$/;
20
21/**
22 * The class which stores properties' information of an object.
23 */
24class ObjectInfo {
25
26 /**
27 * @param {ObjectInfo|null} upper - The information of the outer object.
28 * @param {ASTNode} node - The ObjectExpression node of this information.
29 */
30 constructor(upper, node) {
31 this.upper = upper;
32 this.node = node;
33 this.properties = new Map();
34 }
35
36 /**
37 * Gets the information of the given Property node.
38 * @param {ASTNode} node - The Property node to get.
39 * @returns {{get: boolean, set: boolean}} The information of the property.
40 */
41 getPropertyInfo(node) {
42 const name = astUtils.getStaticPropertyName(node);
43
44 if (!this.properties.has(name)) {
45 this.properties.set(name, { get: false, set: false });
46 }
47 return this.properties.get(name);
48 }
49
50 /**
51 * Checks whether the given property has been defined already or not.
52 * @param {ASTNode} node - The Property node to check.
53 * @returns {boolean} `true` if the property has been defined.
54 */
55 isPropertyDefined(node) {
56 const entry = this.getPropertyInfo(node);
57
58 return (
59 (GET_KIND.test(node.kind) && entry.get) ||
60 (SET_KIND.test(node.kind) && entry.set)
61 );
62 }
63
64 /**
65 * Defines the given property.
66 * @param {ASTNode} node - The Property node to define.
67 * @returns {void}
68 */
69 defineProperty(node) {
70 const entry = this.getPropertyInfo(node);
71
72 if (GET_KIND.test(node.kind)) {
73 entry.get = true;
74 }
75 if (SET_KIND.test(node.kind)) {
76 entry.set = true;
77 }
78 }
79}
80
81//------------------------------------------------------------------------------
82// Rule Definition
83//------------------------------------------------------------------------------
84
85module.exports = {
86 meta: {
87 docs: {
88 description: "disallow duplicate keys in object literals",
89 category: "Possible Errors",
90 recommended: true
91 },
92
93 schema: []
94 },
95
96 create(context) {
97 let info = null;
98
99 return {
100 ObjectExpression(node) {
101 info = new ObjectInfo(info, node);
102 },
103 "ObjectExpression:exit"() {
104 info = info.upper;
105 },
106
107 Property(node) {
108 const name = astUtils.getStaticPropertyName(node);
109
110 // Skip destructuring.
111 if (node.parent.type !== "ObjectExpression") {
112 return;
113 }
114
115 // Skip if the name is not static.
116 if (!name) {
117 return;
118 }
119
120 // Reports if the name is defined already.
121 if (info.isPropertyDefined(node)) {
122 context.report({
123 node: info.node,
124 loc: node.key.loc,
125 message: "Duplicate key '{{name}}'.",
126 data: { name }
127 });
128 }
129
130 // Update info.
131 info.defineProperty(node);
132 }
133 };
134 }
135};