no-extra-bind.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @fileoverview Rule to flag unnecessary bind calls
  3. * @author Bence Dányi <bence@danyi.me>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const SIDE_EFFECT_FREE_NODE_TYPES = new Set(["Literal", "Identifier", "ThisExpression", "FunctionExpression"]);
  14. //------------------------------------------------------------------------------
  15. // Rule Definition
  16. //------------------------------------------------------------------------------
  17. module.exports = {
  18. meta: {
  19. type: "suggestion",
  20. docs: {
  21. description: "disallow unnecessary calls to `.bind()`",
  22. category: "Best Practices",
  23. recommended: false,
  24. url: "https://eslint.org/docs/rules/no-extra-bind"
  25. },
  26. schema: [],
  27. fixable: "code",
  28. messages: {
  29. unexpected: "The function binding is unnecessary."
  30. }
  31. },
  32. create(context) {
  33. const sourceCode = context.getSourceCode();
  34. let scopeInfo = null;
  35. /**
  36. * Checks if a node is free of side effects.
  37. *
  38. * This check is stricter than it needs to be, in order to keep the implementation simple.
  39. * @param {ASTNode} node A node to check.
  40. * @returns {boolean} True if the node is known to be side-effect free, false otherwise.
  41. */
  42. function isSideEffectFree(node) {
  43. return SIDE_EFFECT_FREE_NODE_TYPES.has(node.type);
  44. }
  45. /**
  46. * Reports a given function node.
  47. * @param {ASTNode} node A node to report. This is a FunctionExpression or
  48. * an ArrowFunctionExpression.
  49. * @returns {void}
  50. */
  51. function report(node) {
  52. context.report({
  53. node: node.parent.parent,
  54. messageId: "unexpected",
  55. loc: node.parent.property.loc.start,
  56. fix(fixer) {
  57. if (node.parent.parent.arguments.length && !isSideEffectFree(node.parent.parent.arguments[0])) {
  58. return null;
  59. }
  60. const firstTokenToRemove = sourceCode
  61. .getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken);
  62. const lastTokenToRemove = sourceCode.getLastToken(node.parent.parent);
  63. if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) {
  64. return null;
  65. }
  66. return fixer.removeRange([firstTokenToRemove.range[0], node.parent.parent.range[1]]);
  67. }
  68. });
  69. }
  70. /**
  71. * Checks whether or not a given function node is the callee of `.bind()`
  72. * method.
  73. *
  74. * e.g. `(function() {}.bind(foo))`
  75. * @param {ASTNode} node A node to report. This is a FunctionExpression or
  76. * an ArrowFunctionExpression.
  77. * @returns {boolean} `true` if the node is the callee of `.bind()` method.
  78. */
  79. function isCalleeOfBindMethod(node) {
  80. const parent = node.parent;
  81. const grandparent = parent.parent;
  82. return (
  83. grandparent &&
  84. grandparent.type === "CallExpression" &&
  85. grandparent.callee === parent &&
  86. grandparent.arguments.length === 1 &&
  87. grandparent.arguments[0].type !== "SpreadElement" &&
  88. parent.type === "MemberExpression" &&
  89. parent.object === node &&
  90. astUtils.getStaticPropertyName(parent) === "bind"
  91. );
  92. }
  93. /**
  94. * Adds a scope information object to the stack.
  95. * @param {ASTNode} node A node to add. This node is a FunctionExpression
  96. * or a FunctionDeclaration node.
  97. * @returns {void}
  98. */
  99. function enterFunction(node) {
  100. scopeInfo = {
  101. isBound: isCalleeOfBindMethod(node),
  102. thisFound: false,
  103. upper: scopeInfo
  104. };
  105. }
  106. /**
  107. * Removes the scope information object from the top of the stack.
  108. * At the same time, this reports the function node if the function has
  109. * `.bind()` and the `this` keywords found.
  110. * @param {ASTNode} node A node to remove. This node is a
  111. * FunctionExpression or a FunctionDeclaration node.
  112. * @returns {void}
  113. */
  114. function exitFunction(node) {
  115. if (scopeInfo.isBound && !scopeInfo.thisFound) {
  116. report(node);
  117. }
  118. scopeInfo = scopeInfo.upper;
  119. }
  120. /**
  121. * Reports a given arrow function if the function is callee of `.bind()`
  122. * method.
  123. * @param {ASTNode} node A node to report. This node is an
  124. * ArrowFunctionExpression.
  125. * @returns {void}
  126. */
  127. function exitArrowFunction(node) {
  128. if (isCalleeOfBindMethod(node)) {
  129. report(node);
  130. }
  131. }
  132. /**
  133. * Set the mark as the `this` keyword was found in this scope.
  134. * @returns {void}
  135. */
  136. function markAsThisFound() {
  137. if (scopeInfo) {
  138. scopeInfo.thisFound = true;
  139. }
  140. }
  141. return {
  142. "ArrowFunctionExpression:exit": exitArrowFunction,
  143. FunctionDeclaration: enterFunction,
  144. "FunctionDeclaration:exit": exitFunction,
  145. FunctionExpression: enterFunction,
  146. "FunctionExpression:exit": exitFunction,
  147. ThisExpression: markAsThisFound
  148. };
  149. }
  150. };