项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

82 lines
2.7 KiB

  1. /**
  2. * @fileoverview A rule to disallow unnecessary `.call()` and `.apply()`.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. const astUtils = require("../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. docs: {
  45. description: "disallow unnecessary calls to `.call()` and `.apply()`",
  46. category: "Best Practices",
  47. recommended: false,
  48. url: "https://eslint.org/docs/rules/no-useless-call"
  49. },
  50. schema: []
  51. },
  52. create(context) {
  53. const sourceCode = context.getSourceCode();
  54. return {
  55. CallExpression(node) {
  56. if (!isCallOrNonVariadicApply(node)) {
  57. return;
  58. }
  59. const applied = node.callee.object;
  60. const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
  61. const thisArg = node.arguments[0];
  62. if (isValidThisArg(expectedThis, thisArg, sourceCode)) {
  63. context.report({ node, message: "unnecessary '.{{name}}()'.", data: { name: node.callee.property.name } });
  64. }
  65. }
  66. };
  67. }
  68. };