Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

prefer-destructuring.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. module.exports = {
  18. meta: {
  19. type: "suggestion",
  20. docs: {
  21. description: "require destructuring from arrays and/or objects",
  22. category: "ECMAScript 6",
  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. // eslint-disable-next-line jsdoc/require-description
  108. /**
  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 (rightNode.type !== "MemberExpression" || rightNode.object.type === "Super") {
  201. return;
  202. }
  203. if (isArrayIndexAccess(rightNode)) {
  204. if (shouldCheck(reportNode.type, "array")) {
  205. report(reportNode, "array", null);
  206. }
  207. return;
  208. }
  209. const fix = shouldFix(reportNode)
  210. ? fixer => fixIntoObjectDestructuring(fixer, reportNode)
  211. : null;
  212. if (shouldCheck(reportNode.type, "object") && enforceForRenamedProperties) {
  213. report(reportNode, "object", fix);
  214. return;
  215. }
  216. if (shouldCheck(reportNode.type, "object")) {
  217. const property = rightNode.property;
  218. if (
  219. (property.type === "Literal" && leftNode.name === property.value) ||
  220. (property.type === "Identifier" && leftNode.name === property.name && !rightNode.computed)
  221. ) {
  222. report(reportNode, "object", fix);
  223. }
  224. }
  225. }
  226. /**
  227. * Check if a given variable declarator is coming from an property access
  228. * that should be using destructuring instead
  229. * @param {ASTNode} node the variable declarator to check
  230. * @returns {void}
  231. */
  232. function checkVariableDeclarator(node) {
  233. // Skip if variable is declared without assignment
  234. if (!node.init) {
  235. return;
  236. }
  237. // We only care about member expressions past this point
  238. if (node.init.type !== "MemberExpression") {
  239. return;
  240. }
  241. performCheck(node.id, node.init, node);
  242. }
  243. /**
  244. * Run the `prefer-destructuring` check on an AssignmentExpression
  245. * @param {ASTNode} node the AssignmentExpression node
  246. * @returns {void}
  247. */
  248. function checkAssignmentExpression(node) {
  249. if (node.operator === "=") {
  250. performCheck(node.left, node.right, node);
  251. }
  252. }
  253. //--------------------------------------------------------------------------
  254. // Public
  255. //--------------------------------------------------------------------------
  256. return {
  257. VariableDeclarator: checkVariableDeclarator,
  258. AssignmentExpression: checkAssignmentExpression
  259. };
  260. }
  261. };