no-unused-private-class-members.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @fileoverview Rule to flag declared but unused private class members
  3. * @author Tim van der Lippe
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "problem",
  13. docs: {
  14. description: "Disallow unused private class members",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-unused-private-class-members"
  17. },
  18. schema: [],
  19. messages: {
  20. unusedPrivateClassMember: "'{{classMemberName}}' is defined but never used."
  21. }
  22. },
  23. create(context) {
  24. const trackedClasses = [];
  25. /**
  26. * Check whether the current node is in a write only assignment.
  27. * @param {ASTNode} privateIdentifierNode Node referring to a private identifier
  28. * @returns {boolean} Whether the node is in a write only assignment
  29. * @private
  30. */
  31. function isWriteOnlyAssignment(privateIdentifierNode) {
  32. const parentStatement = privateIdentifierNode.parent.parent;
  33. const isAssignmentExpression = parentStatement.type === "AssignmentExpression";
  34. if (!isAssignmentExpression &&
  35. parentStatement.type !== "ForInStatement" &&
  36. parentStatement.type !== "ForOfStatement" &&
  37. parentStatement.type !== "AssignmentPattern") {
  38. return false;
  39. }
  40. // It is a write-only usage, since we still allow usages on the right for reads
  41. if (parentStatement.left !== privateIdentifierNode.parent) {
  42. return false;
  43. }
  44. // For any other operator (such as '+=') we still consider it a read operation
  45. if (isAssignmentExpression && parentStatement.operator !== "=") {
  46. /*
  47. * However, if the read operation is "discarded" in an empty statement, then
  48. * we consider it write only.
  49. */
  50. return parentStatement.parent.type === "ExpressionStatement";
  51. }
  52. return true;
  53. }
  54. //--------------------------------------------------------------------------
  55. // Public
  56. //--------------------------------------------------------------------------
  57. return {
  58. // Collect all declared members up front and assume they are all unused
  59. ClassBody(classBodyNode) {
  60. const privateMembers = new Map();
  61. trackedClasses.unshift(privateMembers);
  62. for (const bodyMember of classBodyNode.body) {
  63. if (bodyMember.type === "PropertyDefinition" || bodyMember.type === "MethodDefinition") {
  64. if (bodyMember.key.type === "PrivateIdentifier") {
  65. privateMembers.set(bodyMember.key.name, {
  66. declaredNode: bodyMember,
  67. isAccessor: bodyMember.type === "MethodDefinition" &&
  68. (bodyMember.kind === "set" || bodyMember.kind === "get")
  69. });
  70. }
  71. }
  72. }
  73. },
  74. /*
  75. * Process all usages of the private identifier and remove a member from
  76. * `declaredAndUnusedPrivateMembers` if we deem it used.
  77. */
  78. PrivateIdentifier(privateIdentifierNode) {
  79. const classBody = trackedClasses.find(classProperties => classProperties.has(privateIdentifierNode.name));
  80. // Can't happen, as it is a parser to have a missing class body, but let's code defensively here.
  81. if (!classBody) {
  82. return;
  83. }
  84. // In case any other usage was already detected, we can short circuit the logic here.
  85. const memberDefinition = classBody.get(privateIdentifierNode.name);
  86. if (memberDefinition.isUsed) {
  87. return;
  88. }
  89. // The definition of the class member itself
  90. if (privateIdentifierNode.parent.type === "PropertyDefinition" ||
  91. privateIdentifierNode.parent.type === "MethodDefinition") {
  92. return;
  93. }
  94. /*
  95. * Any usage of an accessor is considered a read, as the getter/setter can have
  96. * side-effects in its definition.
  97. */
  98. if (memberDefinition.isAccessor) {
  99. memberDefinition.isUsed = true;
  100. return;
  101. }
  102. // Any assignments to this member, except for assignments that also read
  103. if (isWriteOnlyAssignment(privateIdentifierNode)) {
  104. return;
  105. }
  106. const wrappingExpressionType = privateIdentifierNode.parent.parent.type;
  107. const parentOfWrappingExpressionType = privateIdentifierNode.parent.parent.parent.type;
  108. // A statement which only increments (`this.#x++;`)
  109. if (wrappingExpressionType === "UpdateExpression" &&
  110. parentOfWrappingExpressionType === "ExpressionStatement") {
  111. return;
  112. }
  113. /*
  114. * ({ x: this.#usedInDestructuring } = bar);
  115. *
  116. * But should treat the following as a read:
  117. * ({ [this.#x]: a } = foo);
  118. */
  119. if (wrappingExpressionType === "Property" &&
  120. parentOfWrappingExpressionType === "ObjectPattern" &&
  121. privateIdentifierNode.parent.parent.value === privateIdentifierNode.parent) {
  122. return;
  123. }
  124. // [...this.#unusedInRestPattern] = bar;
  125. if (wrappingExpressionType === "RestElement") {
  126. return;
  127. }
  128. // [this.#unusedInAssignmentPattern] = bar;
  129. if (wrappingExpressionType === "ArrayPattern") {
  130. return;
  131. }
  132. /*
  133. * We can't delete the memberDefinition, as we need to keep track of which member we are marking as used.
  134. * In the case of nested classes, we only mark the first member we encounter as used. If you were to delete
  135. * the member, then any subsequent usage could incorrectly mark the member of an encapsulating parent class
  136. * as used, which is incorrect.
  137. */
  138. memberDefinition.isUsed = true;
  139. },
  140. /*
  141. * Post-process the class members and report any remaining members.
  142. * Since private members can only be accessed in the current class context,
  143. * we can safely assume that all usages are within the current class body.
  144. */
  145. "ClassBody:exit"() {
  146. const unusedPrivateMembers = trackedClasses.shift();
  147. for (const [classMemberName, { declaredNode, isUsed }] of unusedPrivateMembers.entries()) {
  148. if (isUsed) {
  149. continue;
  150. }
  151. context.report({
  152. node: declaredNode,
  153. loc: declaredNode.key.loc,
  154. messageId: "unusedPrivateClassMember",
  155. data: {
  156. classMemberName: `#${classMemberName}`
  157. }
  158. });
  159. }
  160. }
  161. };
  162. }
  163. };