Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-useless-call.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @fileoverview A rule to disallow unnecessary `.call()` and `.apply()`.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. const astUtils = require("../util/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. return (
  17. node.callee.type === "MemberExpression" &&
  18. node.callee.property.type === "Identifier" &&
  19. node.callee.computed === false &&
  20. (
  21. (node.callee.property.name === "call" && node.arguments.length >= 1) ||
  22. (node.callee.property.name === "apply" && node.arguments.length === 2 && node.arguments[1].type === "ArrayExpression")
  23. )
  24. );
  25. }
  26. /**
  27. * Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`.
  28. * @param {ASTNode|null} expectedThis - The node that is the owner of the applied function.
  29. * @param {ASTNode} thisArg - The node that is given to the first argument of the `.call()`/`.apply()`.
  30. * @param {SourceCode} sourceCode - The ESLint source code object.
  31. * @returns {boolean} Whether or not `thisArg` is not changed by `.call()`/`.apply()`.
  32. */
  33. function isValidThisArg(expectedThis, thisArg, sourceCode) {
  34. if (!expectedThis) {
  35. return astUtils.isNullOrUndefined(thisArg);
  36. }
  37. return astUtils.equalTokens(expectedThis, thisArg, sourceCode);
  38. }
  39. //------------------------------------------------------------------------------
  40. // Rule Definition
  41. //------------------------------------------------------------------------------
  42. module.exports = {
  43. meta: {
  44. type: "suggestion",
  45. docs: {
  46. description: "disallow unnecessary calls to `.call()` and `.apply()`",
  47. category: "Best Practices",
  48. recommended: false,
  49. url: "https://eslint.org/docs/rules/no-useless-call"
  50. },
  51. schema: []
  52. },
  53. create(context) {
  54. const sourceCode = context.getSourceCode();
  55. return {
  56. CallExpression(node) {
  57. if (!isCallOrNonVariadicApply(node)) {
  58. return;
  59. }
  60. const applied = node.callee.object;
  61. const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
  62. const thisArg = node.arguments[0];
  63. if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
  64. context.report({ node, message: "unnecessary '.{{name}}()'.", data: { name: node.callee.property.name } });
  65. }
  66. }
  67. };
  68. }
  69. };