2 * @fileoverview A rule to suggest using arrow functions as callbacks.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
13 * Checks whether or not a given variable is a function name.
14 * @param {eslint-scope.Variable} variable A variable to check.
15 * @returns {boolean} `true` if the variable is a function name.
17 function isFunctionName(variable) {
18 return variable && variable.defs[0].type === "FunctionName";
22 * Checks whether or not a given MetaProperty node equals to a given value.
23 * @param {ASTNode} node A MetaProperty node to check.
24 * @param {string} metaName The name of `MetaProperty.meta`.
25 * @param {string} propertyName The name of `MetaProperty.property`.
26 * @returns {boolean} `true` if the node is the specific value.
28 function checkMetaProperty(node, metaName, propertyName) {
29 return node.meta.name === metaName && node.property.name === propertyName;
33 * Gets the variable object of `arguments` which is defined implicitly.
34 * @param {eslint-scope.Scope} scope A scope to get.
35 * @returns {eslint-scope.Variable} The found variable object.
37 function getVariableOfArguments(scope) {
38 const variables = scope.variables;
40 for (let i = 0; i < variables.length; ++i) {
41 const variable = variables[i];
43 if (variable.name === "arguments") {
46 * If there was a parameter which is named "arguments", the
47 * implicit "arguments" is not defined.
48 * So does fast return with null.
50 return (variable.identifiers.length === 0) ? variable : null;
54 /* istanbul ignore next */
59 * Checkes whether or not a given node is a callback.
60 * @param {ASTNode} node A node to check.
62 * {boolean} retv.isCallback - `true` if the node is a callback.
63 * {boolean} retv.isLexicalThis - `true` if the node is with `.bind(this)`.
65 function getCallbackInfo(node) {
66 const retv = { isCallback: false, isLexicalThis: false };
67 let currentNode = node;
68 let parent = node.parent;
71 switch (parent.type) {
73 // Checks parents recursively.
75 case "LogicalExpression":
76 case "ConditionalExpression":
79 // Checks whether the parent node is `.bind(this)` call.
80 case "MemberExpression":
81 if (parent.object === currentNode &&
82 !parent.property.computed &&
83 parent.property.type === "Identifier" &&
84 parent.property.name === "bind" &&
85 parent.parent.type === "CallExpression" &&
86 parent.parent.callee === parent
88 retv.isLexicalThis = (
89 parent.parent.arguments.length === 1 &&
90 parent.parent.arguments[0].type === "ThisExpression"
92 parent = parent.parent;
98 // Checks whether the node is a callback.
99 case "CallExpression":
100 case "NewExpression":
101 if (parent.callee !== currentNode) {
102 retv.isCallback = true;
110 currentNode = parent;
111 parent = parent.parent;
114 /* istanbul ignore next */
115 throw new Error("unreachable");
119 * Checks whether a simple list of parameters contains any duplicates. This does not handle complex
120 * parameter lists (e.g. with destructuring), since complex parameter lists are a SyntaxError with duplicate
121 * parameter names anyway. Instead, it always returns `false` for complex parameter lists.
122 * @param {ASTNode[]} paramsList The list of parameters for a function
123 * @returns {boolean} `true` if the list of parameters contains any duplicates
125 function hasDuplicateParams(paramsList) {
126 return paramsList.every(param => param.type === "Identifier") && paramsList.length !== new Set(paramsList.map(param => param.name)).size;
129 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
138 description: "require using arrow functions for callbacks",
139 category: "ECMAScript 6",
141 url: "https://eslint.org/docs/rules/prefer-arrow-callback"
148 allowNamedFunctions: {
157 additionalProperties: false
165 const options = context.options[0] || {};
167 const allowUnboundThis = options.allowUnboundThis !== false; // default to true
168 const allowNamedFunctions = options.allowNamedFunctions;
169 const sourceCode = context.getSourceCode();
172 * {Array<{this: boolean, super: boolean, meta: boolean}>}
173 * - this - A flag which shows there are one or more ThisExpression.
174 * - super - A flag which shows there are one or more Super.
175 * - meta - A flag which shows there are one or more MethProperty.
180 * Pushes new function scope with all `false` flags.
183 function enterScope() {
184 stack.push({ this: false, super: false, meta: false });
188 * Pops a function scope from the stack.
189 * @returns {{this: boolean, super: boolean, meta: boolean}} The information of the last scope.
191 function exitScope() {
197 // Reset internal state.
202 // If there are below, it cannot replace with arrow functions merely.
204 const info = stack[stack.length - 1];
212 const info = stack[stack.length - 1];
220 const info = stack[stack.length - 1];
222 if (info && checkMetaProperty(node, "new", "target")) {
227 // To skip nested scopes.
228 FunctionDeclaration: enterScope,
229 "FunctionDeclaration:exit": exitScope,
232 FunctionExpression: enterScope,
233 "FunctionExpression:exit"(node) {
234 const scopeInfo = exitScope();
236 // Skip named function expressions
237 if (allowNamedFunctions && node.id && node.id.name) {
242 if (node.generator) {
246 // Skip recursive functions.
247 const nameVar = context.getDeclaredVariables(node)[0];
249 if (isFunctionName(nameVar) && nameVar.references.length > 0) {
253 // Skip if it's using arguments.
254 const variable = getVariableOfArguments(context.getScope());
256 if (variable && variable.references.length > 0) {
260 // Reports if it's a callback which can replace with arrows.
261 const callbackInfo = getCallbackInfo(node);
263 if (callbackInfo.isCallback &&
264 (!allowUnboundThis || !scopeInfo.this || callbackInfo.isLexicalThis) &&
270 message: "Unexpected function expression.",
272 if ((!callbackInfo.isLexicalThis && scopeInfo.this) || hasDuplicateParams(node.params)) {
275 * If the callback function does not have .bind(this) and contains a reference to `this`, there
276 * is no way to determine what `this` should be, so don't perform any fixes.
277 * If the callback function has duplicates in its list of parameters (possible in sloppy mode),
278 * don't replace it with an arrow function, because this is a SyntaxError with arrow functions.
283 const paramsLeftParen = node.params.length ? sourceCode.getTokenBefore(node.params[0]) : sourceCode.getTokenBefore(node.body, 1);
284 const paramsRightParen = sourceCode.getTokenBefore(node.body);
285 const asyncKeyword = node.async ? "async " : "";
286 const paramsFullText = sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]);
287 const arrowFunctionText = `${asyncKeyword}${paramsFullText} => ${sourceCode.getText(node.body)}`;
290 * If the callback function has `.bind(this)`, replace it with an arrow function and remove the binding.
291 * Otherwise, just replace the arrow function itself.
293 const replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node;
296 * If the replaced node is part of a BinaryExpression, LogicalExpression, or MemberExpression, then
297 * the arrow function needs to be parenthesized, because `foo || () => {}` is invalid syntax even
298 * though `foo || function() {}` is valid.
300 const needsParens = replacedNode.parent.type !== "CallExpression" && replacedNode.parent.type !== "ConditionalExpression";
301 const replacementText = needsParens ? `(${arrowFunctionText})` : arrowFunctionText;
303 return fixer.replaceText(replacedNode, replacementText);