Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. (
  22. (callee.property.name === "call" && node.arguments.length >= 1) ||
  23. (callee.property.name === "apply" && node.arguments.length === 2 && node.arguments[1].type === "ArrayExpression")
  24. )
  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. module.exports = {
  44. meta: {
  45. type: "suggestion",
  46. docs: {
  47. description: "disallow unnecessary calls to `.call()` and `.apply()`",
  48. category: "Best Practices",
  49. recommended: false,
  50. url: "https://eslint.org/docs/rules/no-useless-call"
  51. },
  52. schema: [],
  53. messages: {
  54. unnecessaryCall: "Unnecessary '.{{name}}()'."
  55. }
  56. },
  57. create(context) {
  58. const sourceCode = context.getSourceCode();
  59. return {
  60. CallExpression(node) {
  61. if (!isCallOrNonVariadicApply(node)) {
  62. return;
  63. }
  64. const callee = astUtils.skipChainExpression(node.callee);
  65. const applied = astUtils.skipChainExpression(callee.object);
  66. const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
  67. const thisArg = node.arguments[0];
  68. if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
  69. context.report({ node, messageId: "unnecessaryCall", data: { name: callee.property.name } });
  70. }
  71. }
  72. };
  73. }
  74. };