]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/no-useless-call.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / no-useless-call.js
CommitLineData
eb39fafa
DC
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) {
6f036462
TL
20 const callee = astUtils.skipChainExpression(node.callee);
21
eb39fafa 22 return (
6f036462
TL
23 callee.type === "MemberExpression" &&
24 callee.property.type === "Identifier" &&
25 callee.computed === false &&
eb39fafa 26 (
6f036462
TL
27 (callee.property.name === "call" && node.arguments.length >= 1) ||
28 (callee.property.name === "apply" && node.arguments.length === 2 && node.arguments[1].type === "ArrayExpression")
eb39fafa
DC
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
34eeec05 52/** @type {import('../shared/types').Rule} */
eb39fafa
DC
53module.exports = {
54 meta: {
55 type: "suggestion",
56
57 docs: {
8f9d1d4d 58 description: "Disallow unnecessary calls to `.call()` and `.apply()`",
eb39fafa
DC
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
6f036462
TL
79 const callee = astUtils.skipChainExpression(node.callee);
80 const applied = astUtils.skipChainExpression(callee.object);
eb39fafa
DC
81 const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
82 const thisArg = node.arguments[0];
83
84 if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
6f036462 85 context.report({ node, messageId: "unnecessaryCall", data: { name: callee.property.name } });
eb39fafa
DC
86 }
87 }
88 };
89 }
90};