no-dupe-class-members.js 3.6 KB

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