]>
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
) {
20 const callee
= astUtils
.skipChainExpression(node
.callee
);
23 callee
.type
=== "MemberExpression" &&
24 callee
.property
.type
=== "Identifier" &&
25 callee
.computed
=== false &&
27 (callee
.property
.name
=== "call" && node
.arguments
.length
>= 1) ||
28 (callee
.property
.name
=== "apply" && node
.arguments
.length
=== 2 && node
.arguments
[1].type
=== "ArrayExpression")
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()`.
41 function isValidThisArg(expectedThis
, thisArg
, sourceCode
) {
43 return astUtils
.isNullOrUndefined(thisArg
);
45 return astUtils
.equalTokens(expectedThis
, thisArg
, sourceCode
);
48 //------------------------------------------------------------------------------
50 //------------------------------------------------------------------------------
52 /** @type {import('../shared/types').Rule} */
58 description
: "disallow unnecessary calls to `.call()` and `.apply()`",
60 url
: "https://eslint.org/docs/rules/no-useless-call"
66 unnecessaryCall
: "Unnecessary '.{{name}}()'."
71 const sourceCode
= context
.getSourceCode();
74 CallExpression(node
) {
75 if (!isCallOrNonVariadicApply(node
)) {
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];
84 if (isValidThisArg(expectedThis
, thisArg
, sourceCode
)) {
85 context
.report({ node
, messageId
: "unnecessaryCall", data
: { name
: callee
.property
.name
} });