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) {
64 const memberNode = node.parent;
65 const callNode = memberNode.parent.type === "ChainExpression"
66 ? memberNode.parent.parent
71 messageId: "unexpected",
72 loc: memberNode.property.loc,
75 if (!isSideEffectFree(callNode.arguments[0])) {
80 * The list of the first/last token pair of a removal range.
81 * This is two parts because closing parentheses may exist between the method name and arguments.
82 * E.g. `(function(){}.bind ) (obj)`
83 * ^^^^^ ^^^^^ < removal ranges
84 * E.g. `(function(){}?.['bind'] ) ?.(obj)`
85 * ^^^^^^^^^^ ^^^^^^^ < removal ranges
90 // `.`, `?.`, or `[` token.
91 sourceCode.getTokenAfter(
93 astUtils.isNotClosingParenToken
96 // property name or `]` token.
97 sourceCode.getLastToken(memberNode)
101 // `?.` or `(` token of arguments.
102 sourceCode.getTokenAfter(
104 astUtils.isNotClosingParenToken
107 // `)` token of arguments.
108 sourceCode.getLastToken(callNode)
111 const firstTokenToRemove = tokenPairs[0][0];
112 const lastTokenToRemove = tokenPairs[1][1];
114 if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) {
118 return tokenPairs.map(([start, end]) =>
119 fixer.removeRange([start.range[0], end.range[1]]));
125 * Checks whether or not a given function node is the callee of `.bind()`
128 * e.g. `(function() {}.bind(foo))`
129 * @param {ASTNode} node A node to report. This is a FunctionExpression or
130 * an ArrowFunctionExpression.
131 * @returns {boolean} `true` if the node is the callee of `.bind()` method.
133 function isCalleeOfBindMethod(node) {
134 if (!astUtils.isSpecificMemberAccess(node.parent, null, "bind")) {
138 // The node of `*.bind` member access.
139 const bindNode = node.parent.parent.type === "ChainExpression"
144 bindNode.parent.type === "CallExpression" &&
145 bindNode.parent.callee === bindNode &&
146 bindNode.parent.arguments.length === 1 &&
147 bindNode.parent.arguments[0].type !== "SpreadElement"
152 * Adds a scope information object to the stack.
153 * @param {ASTNode} node A node to add. This node is a FunctionExpression
154 * or a FunctionDeclaration node.
157 function enterFunction(node) {
159 isBound: isCalleeOfBindMethod(node),
166 * Removes the scope information object from the top of the stack.
167 * At the same time, this reports the function node if the function has
168 * `.bind()` and the `this` keywords found.
169 * @param {ASTNode} node A node to remove. This node is a
170 * FunctionExpression or a FunctionDeclaration node.
173 function exitFunction(node) {
174 if (scopeInfo.isBound && !scopeInfo.thisFound) {
178 scopeInfo = scopeInfo.upper;
182 * Reports a given arrow function if the function is callee of `.bind()`
184 * @param {ASTNode} node A node to report. This node is an
185 * ArrowFunctionExpression.
188 function exitArrowFunction(node) {
189 if (isCalleeOfBindMethod(node)) {
195 * Set the mark as the `this` keyword was found in this scope.
198 function markAsThisFound() {
200 scopeInfo.thisFound = true;
205 "ArrowFunctionExpression:exit": exitArrowFunction,
206 FunctionDeclaration: enterFunction,
207 "FunctionDeclaration:exit": exitFunction,
208 FunctionExpression: enterFunction,
209 "FunctionExpression:exit": exitFunction,
210 ThisExpression: markAsThisFound