1 | /**
|
2 | * @fileoverview Rule to flag use of duplicate keys in an object.
|
3 | * @author Ian Christian Myers
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | const GET_KIND = /^(?:init|get)$/;
|
19 | const SET_KIND = /^(?:init|set)$/;
|
20 |
|
21 | /**
|
22 | * The class which stores properties' information of an object.
|
23 | */
|
24 | class 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 |
|
85 | module.exports = {
|
86 | meta: {
|
87 | docs: {
|
88 | description: "disallow duplicate keys in object literals",
|
89 | category: "Possible Errors",
|
90 | recommended: true,
|
91 | url: "https://eslint.org/docs/rules/no-dupe-keys"
|
92 | },
|
93 |
|
94 | schema: [],
|
95 |
|
96 | messages: {
|
97 | unexpected: "Duplicate key '{{name}}'."
|
98 | }
|
99 | },
|
100 |
|
101 | create(context) {
|
102 | let info = null;
|
103 |
|
104 | return {
|
105 | ObjectExpression(node) {
|
106 | info = new ObjectInfo(info, node);
|
107 | },
|
108 | "ObjectExpression:exit"() {
|
109 | info = info.upper;
|
110 | },
|
111 |
|
112 | Property(node) {
|
113 | const name = astUtils.getStaticPropertyName(node);
|
114 |
|
115 | // Skip destructuring.
|
116 | if (node.parent.type !== "ObjectExpression") {
|
117 | return;
|
118 | }
|
119 |
|
120 | // Skip if the name is not static.
|
121 | if (!name) {
|
122 | return;
|
123 | }
|
124 |
|
125 | // Reports if the name is defined already.
|
126 | if (info.isPropertyDefined(node)) {
|
127 | context.report({
|
128 | node: info.node,
|
129 | loc: node.key.loc,
|
130 | messageId: "unexpected",
|
131 | data: { name }
|
132 | });
|
133 | }
|
134 |
|
135 | // Update info.
|
136 | info.defineProperty(node);
|
137 | }
|
138 | };
|
139 | }
|
140 | };
|