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