2 * @fileoverview Rule to flag unnecessary bind calls
3 * @author Bence Dányi <bence@danyi.me>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
17 const SIDE_EFFECT_FREE_NODE_TYPES = new Set(["Literal", "Identifier", "ThisExpression", "FunctionExpression"]);
19 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
28 description: "disallow unnecessary calls to `.bind()`",
29 category: "Best Practices",
31 url: "https://eslint.org/docs/rules/no-extra-bind"
38 unexpected: "The function binding is unnecessary."
43 const sourceCode = context.getSourceCode();
47 * Checks if a node is free of side effects.
49 * This check is stricter than it needs to be, in order to keep the implementation simple.
50 * @param {ASTNode} node A node to check.
51 * @returns {boolean} True if the node is known to be side-effect free, false otherwise.
53 function isSideEffectFree(node) {
54 return SIDE_EFFECT_FREE_NODE_TYPES.has(node.type);
58 * Reports a given function node.
59 * @param {ASTNode} node A node to report. This is a FunctionExpression or
60 * an ArrowFunctionExpression.
63 function report(node) {
65 node: node.parent.parent,
66 messageId: "unexpected",
67 loc: node.parent.property.loc.start,
69 if (node.parent.parent.arguments.length && !isSideEffectFree(node.parent.parent.arguments[0])) {
73 const firstTokenToRemove = sourceCode
74 .getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken);
75 const lastTokenToRemove = sourceCode.getLastToken(node.parent.parent);
77 if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) {
81 return fixer.removeRange([firstTokenToRemove.range[0], node.parent.parent.range[1]]);
87 * Checks whether or not a given function node is the callee of `.bind()`
90 * e.g. `(function() {}.bind(foo))`
91 * @param {ASTNode} node A node to report. This is a FunctionExpression or
92 * an ArrowFunctionExpression.
93 * @returns {boolean} `true` if the node is the callee of `.bind()` method.
95 function isCalleeOfBindMethod(node) {
96 const parent = node.parent;
97 const grandparent = parent.parent;
101 grandparent.type === "CallExpression" &&
102 grandparent.callee === parent &&
103 grandparent.arguments.length === 1 &&
104 grandparent.arguments[0].type !== "SpreadElement" &&
105 parent.type === "MemberExpression" &&
106 parent.object === node &&
107 astUtils.getStaticPropertyName(parent) === "bind"
112 * Adds a scope information object to the stack.
113 * @param {ASTNode} node A node to add. This node is a FunctionExpression
114 * or a FunctionDeclaration node.
117 function enterFunction(node) {
119 isBound: isCalleeOfBindMethod(node),
126 * Removes the scope information object from the top of the stack.
127 * At the same time, this reports the function node if the function has
128 * `.bind()` and the `this` keywords found.
129 * @param {ASTNode} node A node to remove. This node is a
130 * FunctionExpression or a FunctionDeclaration node.
133 function exitFunction(node) {
134 if (scopeInfo.isBound && !scopeInfo.thisFound) {
138 scopeInfo = scopeInfo.upper;
142 * Reports a given arrow function if the function is callee of `.bind()`
144 * @param {ASTNode} node A node to report. This node is an
145 * ArrowFunctionExpression.
148 function exitArrowFunction(node) {
149 if (isCalleeOfBindMethod(node)) {
155 * Set the mark as the `this` keyword was found in this scope.
158 function markAsThisFound() {
160 scopeInfo.thisFound = true;
165 "ArrowFunctionExpression:exit": exitArrowFunction,
166 FunctionDeclaration: enterFunction,
167 "FunctionDeclaration:exit": exitFunction,
168 FunctionExpression: enterFunction,
169 "FunctionExpression:exit": exitFunction,
170 ThisExpression: markAsThisFound