prefer-destructuring.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @fileoverview Prefer destructuring from arrays and objects
  3. * @author Alex LaFroscia
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ type: "AssignmentExpression" });
  14. //------------------------------------------------------------------------------
  15. // Rule Definition
  16. //------------------------------------------------------------------------------
  17. /** @type {import('../shared/types').Rule} */
  18. module.exports = {
  19. meta: {
  20. type: "suggestion",
  21. docs: {
  22. description: "Require destructuring from arrays and/or objects",
  23. recommended: false,
  24. url: "https://eslint.org/docs/rules/prefer-destructuring"
  25. },
  26. fixable: "code",
  27. schema: [
  28. {
  29. /*
  30. * old support {array: Boolean, object: Boolean}
  31. * new support {VariableDeclarator: {}, AssignmentExpression: {}}
  32. */
  33. oneOf: [
  34. {
  35. type: "object",
  36. properties: {
  37. VariableDeclarator: {
  38. type: "object",
  39. properties: {
  40. array: {
  41. type: "boolean"
  42. },
  43. object: {
  44. type: "boolean"
  45. }
  46. },
  47. additionalProperties: false
  48. },
  49. AssignmentExpression: {
  50. type: "object",
  51. properties: {
  52. array: {
  53. type: "boolean"
  54. },
  55. object: {
  56. type: "boolean"
  57. }
  58. },
  59. additionalProperties: false
  60. }
  61. },
  62. additionalProperties: false
  63. },
  64. {
  65. type: "object",
  66. properties: {
  67. array: {
  68. type: "boolean"
  69. },
  70. object: {
  71. type: "boolean"
  72. }
  73. },
  74. additionalProperties: false
  75. }
  76. ]
  77. },
  78. {
  79. type: "object",
  80. properties: {
  81. enforceForRenamedProperties: {
  82. type: "boolean"
  83. }
  84. },
  85. additionalProperties: false
  86. }
  87. ],
  88. messages: {
  89. preferDestructuring: "Use {{type}} destructuring."
  90. }
  91. },
  92. create(context) {
  93. const enabledTypes = context.options[0];
  94. const enforceForRenamedProperties = context.options[1] && context.options[1].enforceForRenamedProperties;
  95. let normalizedOptions = {
  96. VariableDeclarator: { array: true, object: true },
  97. AssignmentExpression: { array: true, object: true }
  98. };
  99. if (enabledTypes) {
  100. normalizedOptions = typeof enabledTypes.array !== "undefined" || typeof enabledTypes.object !== "undefined"
  101. ? { VariableDeclarator: enabledTypes, AssignmentExpression: enabledTypes }
  102. : enabledTypes;
  103. }
  104. //--------------------------------------------------------------------------
  105. // Helpers
  106. //--------------------------------------------------------------------------
  107. /**
  108. * Checks if destructuring type should be checked.
  109. * @param {string} nodeType "AssignmentExpression" or "VariableDeclarator"
  110. * @param {string} destructuringType "array" or "object"
  111. * @returns {boolean} `true` if the destructuring type should be checked for the given node
  112. */
  113. function shouldCheck(nodeType, destructuringType) {
  114. return normalizedOptions &&
  115. normalizedOptions[nodeType] &&
  116. normalizedOptions[nodeType][destructuringType];
  117. }
  118. /**
  119. * Determines if the given node is accessing an array index
  120. *
  121. * This is used to differentiate array index access from object property
  122. * access.
  123. * @param {ASTNode} node the node to evaluate
  124. * @returns {boolean} whether or not the node is an integer
  125. */
  126. function isArrayIndexAccess(node) {
  127. return Number.isInteger(node.property.value);
  128. }
  129. /**
  130. * Report that the given node should use destructuring
  131. * @param {ASTNode} reportNode the node to report
  132. * @param {string} type the type of destructuring that should have been done
  133. * @param {Function|null} fix the fix function or null to pass to context.report
  134. * @returns {void}
  135. */
  136. function report(reportNode, type, fix) {
  137. context.report({
  138. node: reportNode,
  139. messageId: "preferDestructuring",
  140. data: { type },
  141. fix
  142. });
  143. }
  144. /**
  145. * Determines if a node should be fixed into object destructuring
  146. *
  147. * The fixer only fixes the simplest case of object destructuring,
  148. * like: `let x = a.x`;
  149. *
  150. * Assignment expression is not fixed.
  151. * Array destructuring is not fixed.
  152. * Renamed property is not fixed.
  153. * @param {ASTNode} node the node to evaluate
  154. * @returns {boolean} whether or not the node should be fixed
  155. */
  156. function shouldFix(node) {
  157. return node.type === "VariableDeclarator" &&
  158. node.id.type === "Identifier" &&
  159. node.init.type === "MemberExpression" &&
  160. !node.init.computed &&
  161. node.init.property.type === "Identifier" &&
  162. node.id.name === node.init.property.name;
  163. }
  164. /**
  165. * Fix a node into object destructuring.
  166. * This function only handles the simplest case of object destructuring,
  167. * see {@link shouldFix}.
  168. * @param {SourceCodeFixer} fixer the fixer object
  169. * @param {ASTNode} node the node to be fixed.
  170. * @returns {Object} a fix for the node
  171. */
  172. function fixIntoObjectDestructuring(fixer, node) {
  173. const rightNode = node.init;
  174. const sourceCode = context.getSourceCode();
  175. // Don't fix if that would remove any comments. Only comments inside `rightNode.object` can be preserved.
  176. if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(rightNode.object).length) {
  177. return null;
  178. }
  179. let objectText = sourceCode.getText(rightNode.object);
  180. if (astUtils.getPrecedence(rightNode.object) < PRECEDENCE_OF_ASSIGNMENT_EXPR) {
  181. objectText = `(${objectText})`;
  182. }
  183. return fixer.replaceText(
  184. node,
  185. `{${rightNode.property.name}} = ${objectText}`
  186. );
  187. }
  188. /**
  189. * Check that the `prefer-destructuring` rules are followed based on the
  190. * given left- and right-hand side of the assignment.
  191. *
  192. * Pulled out into a separate method so that VariableDeclarators and
  193. * AssignmentExpressions can share the same verification logic.
  194. * @param {ASTNode} leftNode the left-hand side of the assignment
  195. * @param {ASTNode} rightNode the right-hand side of the assignment
  196. * @param {ASTNode} reportNode the node to report the error on
  197. * @returns {void}
  198. */
  199. function performCheck(leftNode, rightNode, reportNode) {
  200. if (
  201. rightNode.type !== "MemberExpression" ||
  202. rightNode.object.type === "Super" ||
  203. rightNode.property.type === "PrivateIdentifier"
  204. ) {
  205. return;
  206. }
  207. if (isArrayIndexAccess(rightNode)) {
  208. if (shouldCheck(reportNode.type, "array")) {
  209. report(reportNode, "array", null);
  210. }
  211. return;
  212. }
  213. const fix = shouldFix(reportNode)
  214. ? fixer => fixIntoObjectDestructuring(fixer, reportNode)
  215. : null;
  216. if (shouldCheck(reportNode.type, "object") && enforceForRenamedProperties) {
  217. report(reportNode, "object", fix);
  218. return;
  219. }
  220. if (shouldCheck(reportNode.type, "object")) {
  221. const property = rightNode.property;
  222. if (
  223. (property.type === "Literal" && leftNode.name === property.value) ||
  224. (property.type === "Identifier" && leftNode.name === property.name && !rightNode.computed)
  225. ) {
  226. report(reportNode, "object", fix);
  227. }
  228. }
  229. }
  230. /**
  231. * Check if a given variable declarator is coming from an property access
  232. * that should be using destructuring instead
  233. * @param {ASTNode} node the variable declarator to check
  234. * @returns {void}
  235. */
  236. function checkVariableDeclarator(node) {
  237. // Skip if variable is declared without assignment
  238. if (!node.init) {
  239. return;
  240. }
  241. // We only care about member expressions past this point
  242. if (node.init.type !== "MemberExpression") {
  243. return;
  244. }
  245. performCheck(node.id, node.init, node);
  246. }
  247. /**
  248. * Run the `prefer-destructuring` check on an AssignmentExpression
  249. * @param {ASTNode} node the AssignmentExpression node
  250. * @returns {void}
  251. */
  252. function checkAssignmentExpression(node) {
  253. if (node.operator === "=") {
  254. performCheck(node.left, node.right, node);
  255. }
  256. }
  257. //--------------------------------------------------------------------------
  258. // Public
  259. //--------------------------------------------------------------------------
  260. return {
  261. VariableDeclarator: checkVariableDeclarator,
  262. AssignmentExpression: checkAssignmentExpression
  263. };
  264. }
  265. };