项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

151 rader
4.7 KiB

  1. /**
  2. * @fileoverview Rule to flag unnecessary bind calls
  3. * @author Bence Dányi <bence@danyi.me>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow unnecessary calls to `.bind()`",
  17. category: "Best Practices",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-extra-bind"
  20. },
  21. schema: [],
  22. fixable: "code",
  23. messages: {
  24. unexpected: "The function binding is unnecessary."
  25. }
  26. },
  27. create(context) {
  28. let scopeInfo = null;
  29. /**
  30. * Reports a given function node.
  31. *
  32. * @param {ASTNode} node - A node to report. This is a FunctionExpression or
  33. * an ArrowFunctionExpression.
  34. * @returns {void}
  35. */
  36. function report(node) {
  37. context.report({
  38. node: node.parent.parent,
  39. messageId: "unexpected",
  40. loc: node.parent.property.loc.start,
  41. fix(fixer) {
  42. const firstTokenToRemove = context.getSourceCode()
  43. .getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken);
  44. return fixer.removeRange([firstTokenToRemove.range[0], node.parent.parent.range[1]]);
  45. }
  46. });
  47. }
  48. /**
  49. * Checks whether or not a given function node is the callee of `.bind()`
  50. * method.
  51. *
  52. * e.g. `(function() {}.bind(foo))`
  53. *
  54. * @param {ASTNode} node - A node to report. This is a FunctionExpression or
  55. * an ArrowFunctionExpression.
  56. * @returns {boolean} `true` if the node is the callee of `.bind()` method.
  57. */
  58. function isCalleeOfBindMethod(node) {
  59. const parent = node.parent;
  60. const grandparent = parent.parent;
  61. return (
  62. grandparent &&
  63. grandparent.type === "CallExpression" &&
  64. grandparent.callee === parent &&
  65. grandparent.arguments.length === 1 &&
  66. parent.type === "MemberExpression" &&
  67. parent.object === node &&
  68. astUtils.getStaticPropertyName(parent) === "bind"
  69. );
  70. }
  71. /**
  72. * Adds a scope information object to the stack.
  73. *
  74. * @param {ASTNode} node - A node to add. This node is a FunctionExpression
  75. * or a FunctionDeclaration node.
  76. * @returns {void}
  77. */
  78. function enterFunction(node) {
  79. scopeInfo = {
  80. isBound: isCalleeOfBindMethod(node),
  81. thisFound: false,
  82. upper: scopeInfo
  83. };
  84. }
  85. /**
  86. * Removes the scope information object from the top of the stack.
  87. * At the same time, this reports the function node if the function has
  88. * `.bind()` and the `this` keywords found.
  89. *
  90. * @param {ASTNode} node - A node to remove. This node is a
  91. * FunctionExpression or a FunctionDeclaration node.
  92. * @returns {void}
  93. */
  94. function exitFunction(node) {
  95. if (scopeInfo.isBound && !scopeInfo.thisFound) {
  96. report(node);
  97. }
  98. scopeInfo = scopeInfo.upper;
  99. }
  100. /**
  101. * Reports a given arrow function if the function is callee of `.bind()`
  102. * method.
  103. *
  104. * @param {ASTNode} node - A node to report. This node is an
  105. * ArrowFunctionExpression.
  106. * @returns {void}
  107. */
  108. function exitArrowFunction(node) {
  109. if (isCalleeOfBindMethod(node)) {
  110. report(node);
  111. }
  112. }
  113. /**
  114. * Set the mark as the `this` keyword was found in this scope.
  115. *
  116. * @returns {void}
  117. */
  118. function markAsThisFound() {
  119. if (scopeInfo) {
  120. scopeInfo.thisFound = true;
  121. }
  122. }
  123. return {
  124. "ArrowFunctionExpression:exit": exitArrowFunction,
  125. FunctionDeclaration: enterFunction,
  126. "FunctionDeclaration:exit": exitFunction,
  127. FunctionExpression: enterFunction,
  128. "FunctionExpression:exit": exitFunction,
  129. ThisExpression: markAsThisFound
  130. };
  131. }
  132. };