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()`",
30 url
: "https://eslint.org/docs/rules/no-extra-bind"
37 unexpected
: "The function binding is unnecessary."
42 const sourceCode
= context
.getSourceCode();
46 * Checks if a node is free of side effects.
48 * This check is stricter than it needs to be, in order to keep the implementation simple.
49 * @param {ASTNode} node A node to check.
50 * @returns {boolean} True if the node is known to be side-effect free, false otherwise.
52 function isSideEffectFree(node
) {
53 return SIDE_EFFECT_FREE_NODE_TYPES
.has(node
.type
);
57 * Reports a given function node.
58 * @param {ASTNode} node A node to report. This is a FunctionExpression or
59 * an ArrowFunctionExpression.
62 function report(node
) {
63 const memberNode
= node
.parent
;
64 const callNode
= memberNode
.parent
.type
=== "ChainExpression"
65 ? memberNode
.parent
.parent
70 messageId
: "unexpected",
71 loc
: memberNode
.property
.loc
,
74 if (!isSideEffectFree(callNode
.arguments
[0])) {
79 * The list of the first/last token pair of a removal range.
80 * This is two parts because closing parentheses may exist between the method name and arguments.
81 * E.g. `(function(){}.bind ) (obj)`
82 * ^^^^^ ^^^^^ < removal ranges
83 * E.g. `(function(){}?.['bind'] ) ?.(obj)`
84 * ^^^^^^^^^^ ^^^^^^^ < removal ranges
89 // `.`, `?.`, or `[` token.
90 sourceCode
.getTokenAfter(
92 astUtils
.isNotClosingParenToken
95 // property name or `]` token.
96 sourceCode
.getLastToken(memberNode
)
100 // `?.` or `(` token of arguments.
101 sourceCode
.getTokenAfter(
103 astUtils
.isNotClosingParenToken
106 // `)` token of arguments.
107 sourceCode
.getLastToken(callNode
)
110 const firstTokenToRemove
= tokenPairs
[0][0];
111 const lastTokenToRemove
= tokenPairs
[1][1];
113 if (sourceCode
.commentsExistBetween(firstTokenToRemove
, lastTokenToRemove
)) {
117 return tokenPairs
.map(([start
, end
]) =>
118 fixer
.removeRange([start
.range
[0], end
.range
[1]]));
124 * Checks whether or not a given function node is the callee of `.bind()`
127 * e.g. `(function() {}.bind(foo))`
128 * @param {ASTNode} node A node to report. This is a FunctionExpression or
129 * an ArrowFunctionExpression.
130 * @returns {boolean} `true` if the node is the callee of `.bind()` method.
132 function isCalleeOfBindMethod(node
) {
133 if (!astUtils
.isSpecificMemberAccess(node
.parent
, null, "bind")) {
137 // The node of `*.bind` member access.
138 const bindNode
= node
.parent
.parent
.type
=== "ChainExpression"
143 bindNode
.parent
.type
=== "CallExpression" &&
144 bindNode
.parent
.callee
=== bindNode
&&
145 bindNode
.parent
.arguments
.length
=== 1 &&
146 bindNode
.parent
.arguments
[0].type
!== "SpreadElement"
151 * Adds a scope information object to the stack.
152 * @param {ASTNode} node A node to add. This node is a FunctionExpression
153 * or a FunctionDeclaration node.
156 function enterFunction(node
) {
158 isBound
: isCalleeOfBindMethod(node
),
165 * Removes the scope information object from the top of the stack.
166 * At the same time, this reports the function node if the function has
167 * `.bind()` and the `this` keywords found.
168 * @param {ASTNode} node A node to remove. This node is a
169 * FunctionExpression or a FunctionDeclaration node.
172 function exitFunction(node
) {
173 if (scopeInfo
.isBound
&& !scopeInfo
.thisFound
) {
177 scopeInfo
= scopeInfo
.upper
;
181 * Reports a given arrow function if the function is callee of `.bind()`
183 * @param {ASTNode} node A node to report. This node is an
184 * ArrowFunctionExpression.
187 function exitArrowFunction(node
) {
188 if (isCalleeOfBindMethod(node
)) {
194 * Set the mark as the `this` keyword was found in this scope.
197 function markAsThisFound() {
199 scopeInfo
.thisFound
= true;
204 "ArrowFunctionExpression:exit": exitArrowFunction
,
205 FunctionDeclaration
: enterFunction
,
206 "FunctionDeclaration:exit": exitFunction
,
207 FunctionExpression
: enterFunction
,
208 "FunctionExpression:exit": exitFunction
,
209 ThisExpression
: markAsThisFound