grouped-accessor-pairs.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @fileoverview Rule to require grouped accessor pairs in object literals and classes
  3. * @author Milos Djermanovic
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Typedefs
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Property name if it can be computed statically, otherwise the list of the tokens of the key node.
  15. * @typedef {string|Token[]} Key
  16. */
  17. /**
  18. * Accessor nodes with the same key.
  19. * @typedef {Object} AccessorData
  20. * @property {Key} key Accessor's key
  21. * @property {ASTNode[]} getters List of getter nodes.
  22. * @property {ASTNode[]} setters List of setter nodes.
  23. */
  24. //------------------------------------------------------------------------------
  25. // Helpers
  26. //------------------------------------------------------------------------------
  27. /**
  28. * Checks whether or not the given lists represent the equal tokens in the same order.
  29. * Tokens are compared by their properties, not by instance.
  30. * @param {Token[]} left First list of tokens.
  31. * @param {Token[]} right Second list of tokens.
  32. * @returns {boolean} `true` if the lists have same tokens.
  33. */
  34. function areEqualTokenLists(left, right) {
  35. if (left.length !== right.length) {
  36. return false;
  37. }
  38. for (let i = 0; i < left.length; i++) {
  39. const leftToken = left[i],
  40. rightToken = right[i];
  41. if (leftToken.type !== rightToken.type || leftToken.value !== rightToken.value) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. /**
  48. * Checks whether or not the given keys are equal.
  49. * @param {Key} left First key.
  50. * @param {Key} right Second key.
  51. * @returns {boolean} `true` if the keys are equal.
  52. */
  53. function areEqualKeys(left, right) {
  54. if (typeof left === "string" && typeof right === "string") {
  55. // Statically computed names.
  56. return left === right;
  57. }
  58. if (Array.isArray(left) && Array.isArray(right)) {
  59. // Token lists.
  60. return areEqualTokenLists(left, right);
  61. }
  62. return false;
  63. }
  64. /**
  65. * Checks whether or not a given node is of an accessor kind ('get' or 'set').
  66. * @param {ASTNode} node A node to check.
  67. * @returns {boolean} `true` if the node is of an accessor kind.
  68. */
  69. function isAccessorKind(node) {
  70. return node.kind === "get" || node.kind === "set";
  71. }
  72. //------------------------------------------------------------------------------
  73. // Rule Definition
  74. //------------------------------------------------------------------------------
  75. /** @type {import('../shared/types').Rule} */
  76. module.exports = {
  77. meta: {
  78. type: "suggestion",
  79. docs: {
  80. description: "Require grouped accessor pairs in object literals and classes",
  81. recommended: false,
  82. url: "https://eslint.org/docs/rules/grouped-accessor-pairs"
  83. },
  84. schema: [
  85. {
  86. enum: ["anyOrder", "getBeforeSet", "setBeforeGet"]
  87. }
  88. ],
  89. messages: {
  90. notGrouped: "Accessor pair {{ formerName }} and {{ latterName }} should be grouped.",
  91. invalidOrder: "Expected {{ latterName }} to be before {{ formerName }}."
  92. }
  93. },
  94. create(context) {
  95. const order = context.options[0] || "anyOrder";
  96. const sourceCode = context.getSourceCode();
  97. /**
  98. * Reports the given accessor pair.
  99. * @param {string} messageId messageId to report.
  100. * @param {ASTNode} formerNode getter/setter node that is defined before `latterNode`.
  101. * @param {ASTNode} latterNode getter/setter node that is defined after `formerNode`.
  102. * @returns {void}
  103. * @private
  104. */
  105. function report(messageId, formerNode, latterNode) {
  106. context.report({
  107. node: latterNode,
  108. messageId,
  109. loc: astUtils.getFunctionHeadLoc(latterNode.value, sourceCode),
  110. data: {
  111. formerName: astUtils.getFunctionNameWithKind(formerNode.value),
  112. latterName: astUtils.getFunctionNameWithKind(latterNode.value)
  113. }
  114. });
  115. }
  116. /**
  117. * Creates a new `AccessorData` object for the given getter or setter node.
  118. * @param {ASTNode} node A getter or setter node.
  119. * @returns {AccessorData} New `AccessorData` object that contains the given node.
  120. * @private
  121. */
  122. function createAccessorData(node) {
  123. const name = astUtils.getStaticPropertyName(node);
  124. const key = (name !== null) ? name : sourceCode.getTokens(node.key);
  125. return {
  126. key,
  127. getters: node.kind === "get" ? [node] : [],
  128. setters: node.kind === "set" ? [node] : []
  129. };
  130. }
  131. /**
  132. * Merges the given `AccessorData` object into the given accessors list.
  133. * @param {AccessorData[]} accessors The list to merge into.
  134. * @param {AccessorData} accessorData The object to merge.
  135. * @returns {AccessorData[]} The same instance with the merged object.
  136. * @private
  137. */
  138. function mergeAccessorData(accessors, accessorData) {
  139. const equalKeyElement = accessors.find(a => areEqualKeys(a.key, accessorData.key));
  140. if (equalKeyElement) {
  141. equalKeyElement.getters.push(...accessorData.getters);
  142. equalKeyElement.setters.push(...accessorData.setters);
  143. } else {
  144. accessors.push(accessorData);
  145. }
  146. return accessors;
  147. }
  148. /**
  149. * Checks accessor pairs in the given list of nodes.
  150. * @param {ASTNode[]} nodes The list to check.
  151. * @param {Function} shouldCheck – Predicate that returns `true` if the node should be checked.
  152. * @returns {void}
  153. * @private
  154. */
  155. function checkList(nodes, shouldCheck) {
  156. const accessors = nodes
  157. .filter(shouldCheck)
  158. .filter(isAccessorKind)
  159. .map(createAccessorData)
  160. .reduce(mergeAccessorData, []);
  161. for (const { getters, setters } of accessors) {
  162. // Don't report accessor properties that have duplicate getters or setters.
  163. if (getters.length === 1 && setters.length === 1) {
  164. const [getter] = getters,
  165. [setter] = setters,
  166. getterIndex = nodes.indexOf(getter),
  167. setterIndex = nodes.indexOf(setter),
  168. formerNode = getterIndex < setterIndex ? getter : setter,
  169. latterNode = getterIndex < setterIndex ? setter : getter;
  170. if (Math.abs(getterIndex - setterIndex) > 1) {
  171. report("notGrouped", formerNode, latterNode);
  172. } else if (
  173. (order === "getBeforeSet" && getterIndex > setterIndex) ||
  174. (order === "setBeforeGet" && getterIndex < setterIndex)
  175. ) {
  176. report("invalidOrder", formerNode, latterNode);
  177. }
  178. }
  179. }
  180. }
  181. return {
  182. ObjectExpression(node) {
  183. checkList(node.properties, n => n.type === "Property");
  184. },
  185. ClassBody(node) {
  186. checkList(node.body, n => n.type === "MethodDefinition" && !n.static);
  187. checkList(node.body, n => n.type === "MethodDefinition" && n.static);
  188. }
  189. };
  190. }
  191. };