项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

115 line
3.6 KiB

  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. docs: {
  12. description: "disallow duplicate class members",
  13. category: "ECMAScript 6",
  14. recommended: true,
  15. url: "https://eslint.org/docs/rules/no-dupe-class-members"
  16. },
  17. schema: [],
  18. messages: {
  19. unexpected: "Duplicate name '{{name}}'."
  20. }
  21. },
  22. create(context) {
  23. let stack = [];
  24. /**
  25. * Gets state of a given member name.
  26. * @param {string} name - A name of a member.
  27. * @param {boolean} isStatic - A flag which specifies that is a static member.
  28. * @returns {Object} A state of a given member name.
  29. * - retv.init {boolean} A flag which shows the name is declared as normal member.
  30. * - retv.get {boolean} A flag which shows the name is declared as getter.
  31. * - retv.set {boolean} A flag which shows the name is declared as setter.
  32. */
  33. function getState(name, isStatic) {
  34. const stateMap = stack[stack.length - 1];
  35. const key = `$${name}`; // to avoid "__proto__".
  36. if (!stateMap[key]) {
  37. stateMap[key] = {
  38. nonStatic: { init: false, get: false, set: false },
  39. static: { init: false, get: false, set: false }
  40. };
  41. }
  42. return stateMap[key][isStatic ? "static" : "nonStatic"];
  43. }
  44. /**
  45. * Gets the name text of a given node.
  46. *
  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. };