]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-useless-call.js
2 * @fileoverview A rule to disallow unnecessary `.call()` and `.apply()`.
3 * @author Toru Nagashima
8 const astUtils
= require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
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()`.
19 function isCallOrNonVariadicApply(node
) {
21 node
.callee
.type
=== "MemberExpression" &&
22 node
.callee
.property
.type
=== "Identifier" &&
23 node
.callee
.computed
=== false &&
25 (node
.callee
.property
.name
=== "call" && node
.arguments
.length
>= 1) ||
26 (node
.callee
.property
.name
=== "apply" && node
.arguments
.length
=== 2 && node
.arguments
[1].type
=== "ArrayExpression")
33 * Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`.
34 * @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
35 * @param {ASTNode} thisArg The node that is given to the first argument of the `.call()`/`.apply()`.
36 * @param {SourceCode} sourceCode The ESLint source code object.
37 * @returns {boolean} Whether or not `thisArg` is not changed by `.call()`/`.apply()`.
39 function isValidThisArg(expectedThis
, thisArg
, sourceCode
) {
41 return astUtils
.isNullOrUndefined(thisArg
);
43 return astUtils
.equalTokens(expectedThis
, thisArg
, sourceCode
);
46 //------------------------------------------------------------------------------
48 //------------------------------------------------------------------------------
55 description
: "disallow unnecessary calls to `.call()` and `.apply()`",
56 category
: "Best Practices",
58 url
: "https://eslint.org/docs/rules/no-useless-call"
64 unnecessaryCall
: "Unnecessary '.{{name}}()'."
69 const sourceCode
= context
.getSourceCode();
72 CallExpression(node
) {
73 if (!isCallOrNonVariadicApply(node
)) {
77 const applied
= node
.callee
.object
;
78 const expectedThis
= (applied
.type
=== "MemberExpression") ? applied
.object
: null;
79 const thisArg
= node
.arguments
[0];
81 if (isValidThisArg(expectedThis
, thisArg
, sourceCode
)) {
82 context
.report({ node
, messageId
: "unnecessaryCall", data
: { name
: node
.callee
.property
.name
} });