UNPKG

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