1/** 2 * @fileoverview A rule to disallow unnecessary `.call()` and `.apply()`. 3 * @author Toru Nagashima 4 */ 5 6"use strict"; 7 8const astUtils = require("./utils/ast-utils"); 9 10//------------------------------------------------------------------------------ 11// Helpers 12//------------------------------------------------------------------------------ 13 14/** 15 * Checks whether or not a node is a `.call()`/`.apply()`. 16 * @param {ASTNode} node A CallExpression node to check. 17 * @returns {boolean} Whether or not the node is a `.call()`/`.apply()`. 18 */ 19function isCallOrNonVariadicApply(node) { 20 const callee = astUtils.skipChainExpression(node.callee); 21 22 return ( 23 callee.type === "MemberExpression" && 24 callee.property.type === "Identifier" && 25 callee.computed === false && 26 ( 27 (callee.property.name === "call" && node.arguments.length >= 1) || 28 (callee.property.name === "apply" && node.arguments.length === 2 && node.arguments[1].type === "ArrayExpression") 29 ) 30 ); 31} 32 33 34/** 35 * Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`. 36 * @param {ASTNode|null} expectedThis The node that is the owner of the applied function. 37 * @param {ASTNode} thisArg The node that is given to the first argument of the `.call()`/`.apply()`. 38 * @param {SourceCode} sourceCode The ESLint source code object. 39 * @returns {boolean} Whether or not `thisArg` is not changed by `.call()`/`.apply()`. 40 */ 41function isValidThisArg(expectedThis, thisArg, sourceCode) { 42 if (!expectedThis) { 43 return astUtils.isNullOrUndefined(thisArg); 44 } 45 return astUtils.equalTokens(expectedThis, thisArg, sourceCode); 46} 47 48//------------------------------------------------------------------------------ 49// Rule Definition 50//------------------------------------------------------------------------------ 51 52module.exports = { 53 meta: { 54 type: "suggestion", 55 56 docs: { 57 description: "disallow unnecessary calls to `.call()` and `.apply()`", 58 category: "Best Practices", 59 recommended: false, 60 url: "https://eslint.org/docs/rules/no-useless-call" 61 }, 62 63 schema: [], 64 65 messages: { 66 unnecessaryCall: "Unnecessary '.{{name}}()'." 67 } 68 }, 69 70 create(context) { 71 const sourceCode = context.getSourceCode(); 72 73 return { 74 CallExpression(node) { 75 if (!isCallOrNonVariadicApply(node)) { 76 return; 77 } 78 79 const callee = astUtils.skipChainExpression(node.callee); 80 const applied = astUtils.skipChainExpression(callee.object); 81 const expectedThis = (applied.type === "MemberExpression") ? applied.object : null; 82 const thisArg = node.arguments[0]; 83 84 if (isValidThisArg(expectedThis, thisArg, sourceCode)) { 85 context.report({ node, messageId: "unnecessaryCall", data: { name: callee.property.name } }); 86 } 87 } 88 }; 89 } 90}; 91