no-useless-call.js 2.7 KB

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