1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const astUtils = require("./utils/ast-utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function 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 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function isValidThisArg(expectedThis, thisArg, sourceCode) {
|
42 | if (!expectedThis) {
|
43 | return astUtils.isNullOrUndefined(thisArg);
|
44 | }
|
45 | return astUtils.equalTokens(expectedThis, thisArg, sourceCode);
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | module.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 | };
|