Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-dupe-class-members.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. *
  48. * @param {ASTNode} node - A node to get the name.
  49. * @returns {string} The name text of the node.
  50. */
  51. function getName(node) {
  52. switch (node.type) {
  53. case "Identifier": return node.name;
  54. case "Literal": return String(node.value);
  55. /* istanbul ignore next: syntax error */
  56. default: return "";
  57. }
  58. }
  59. return {
  60. // Initializes the stack of state of member declarations.
  61. Program() {
  62. stack = [];
  63. },
  64. // Initializes state of member declarations for the class.
  65. ClassBody() {
  66. stack.push(Object.create(null));
  67. },
  68. // Disposes the state for the class.
  69. "ClassBody:exit"() {
  70. stack.pop();
  71. },
  72. // Reports the node if its name has been declared already.
  73. MethodDefinition(node) {
  74. if (node.computed) {
  75. return;
  76. }
  77. const name = getName(node.key);
  78. const state = getState(name, node.static);
  79. let isDuplicate = false;
  80. if (node.kind === "get") {
  81. isDuplicate = (state.init || state.get);
  82. state.get = true;
  83. } else if (node.kind === "set") {
  84. isDuplicate = (state.init || state.set);
  85. state.set = true;
  86. } else {
  87. isDuplicate = (state.init || state.get || state.set);
  88. state.init = true;
  89. }
  90. if (isDuplicate) {
  91. context.report({ node, messageId: "unexpected", data: { name } });
  92. }
  93. }
  94. };
  95. }
  96. };