6041e9e371ce5e32bf007928e33c7bdbd348e7bb
[dotfiles/.git] / no-dupe-class-members.js
1 /**
2  * @fileoverview A rule to disallow duplicate name in class members.
3  * @author Toru Nagashima
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = {
13     meta: {
14         type: "problem",
15
16         docs: {
17             description: "disallow duplicate class members",
18             category: "ECMAScript 6",
19             recommended: true,
20             url: "https://eslint.org/docs/rules/no-dupe-class-members"
21         },
22
23         schema: [],
24
25         messages: {
26             unexpected: "Duplicate name '{{name}}'."
27         }
28     },
29
30     create(context) {
31         let stack = [];
32
33         /**
34          * Gets state of a given member name.
35          * @param {string} name A name of a member.
36          * @param {boolean} isStatic A flag which specifies that is a static member.
37          * @returns {Object} A state of a given member name.
38          *   - retv.init {boolean} A flag which shows the name is declared as normal member.
39          *   - retv.get {boolean} A flag which shows the name is declared as getter.
40          *   - retv.set {boolean} A flag which shows the name is declared as setter.
41          */
42         function getState(name, isStatic) {
43             const stateMap = stack[stack.length - 1];
44             const key = `$${name}`; // to avoid "__proto__".
45
46             if (!stateMap[key]) {
47                 stateMap[key] = {
48                     nonStatic: { init: false, get: false, set: false },
49                     static: { init: false, get: false, set: false }
50                 };
51             }
52
53             return stateMap[key][isStatic ? "static" : "nonStatic"];
54         }
55
56         /**
57          * Gets the name text of a given node.
58          * @param {ASTNode} node A node to get the name.
59          * @returns {string} The name text of the node.
60          */
61         function getName(node) {
62             switch (node.type) {
63                 case "Identifier": return node.name;
64                 case "Literal": return String(node.value);
65
66                 /* istanbul ignore next: syntax error */
67                 default: return "";
68             }
69         }
70
71         return {
72
73             // Initializes the stack of state of member declarations.
74             Program() {
75                 stack = [];
76             },
77
78             // Initializes state of member declarations for the class.
79             ClassBody() {
80                 stack.push(Object.create(null));
81             },
82
83             // Disposes the state for the class.
84             "ClassBody:exit"() {
85                 stack.pop();
86             },
87
88             // Reports the node if its name has been declared already.
89             MethodDefinition(node) {
90                 if (node.computed) {
91                     return;
92                 }
93
94                 const name = getName(node.key);
95                 const state = getState(name, node.static);
96                 let isDuplicate = false;
97
98                 if (node.kind === "get") {
99                     isDuplicate = (state.init || state.get);
100                     state.get = true;
101                 } else if (node.kind === "set") {
102                     isDuplicate = (state.init || state.set);
103                     state.set = true;
104                 } else {
105                     isDuplicate = (state.init || state.get || state.set);
106                     state.init = true;
107                 }
108
109                 if (isDuplicate) {
110                     context.report({ node, messageId: "unexpected", data: { name } });
111                 }
112             }
113         };
114     }
115 };