]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-dupe-keys.js
2 * @fileoverview Rule to flag use of duplicate keys in an object.
3 * @author Ian Christian Myers
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const GET_KIND
= /^(?:init|get)$/u;
19 const SET_KIND
= /^(?:init|set)$/u;
22 * The class which stores properties' information of an object.
27 * @param {ObjectInfo|null} upper The information of the outer object.
28 * @param {ASTNode} node The ObjectExpression node of this information.
30 constructor(upper
, node
) {
33 this.properties
= new Map();
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.
41 getPropertyInfo(node
) {
42 const name
= astUtils
.getStaticPropertyName(node
);
44 if (!this.properties
.has(name
)) {
45 this.properties
.set(name
, { get: false, set: false });
47 return this.properties
.get(name
);
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.
55 isPropertyDefined(node
) {
56 const entry
= this.getPropertyInfo(node
);
59 (GET_KIND
.test(node
.kind
) && entry
.get) ||
60 (SET_KIND
.test(node
.kind
) && entry
.set)
65 * Defines the given property.
66 * @param {ASTNode} node The Property node to define.
69 defineProperty(node
) {
70 const entry
= this.getPropertyInfo(node
);
72 if (GET_KIND
.test(node
.kind
)) {
75 if (SET_KIND
.test(node
.kind
)) {
81 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
90 description
: "disallow duplicate keys in object literals",
92 url
: "https://eslint.org/docs/rules/no-dupe-keys"
98 unexpected
: "Duplicate key '{{name}}'."
106 ObjectExpression(node
) {
107 info
= new ObjectInfo(info
, node
);
109 "ObjectExpression:exit"() {
114 const name
= astUtils
.getStaticPropertyName(node
);
116 // Skip destructuring.
117 if (node
.parent
.type
!== "ObjectExpression") {
121 // Skip if the name is not static.
126 // Reports if the name is defined already.
127 if (info
.isPropertyDefined(node
)) {
131 messageId
: "unexpected",
137 info
.defineProperty(node
);