is-function-self-used-inside.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const {findVariable} = require('@eslint-community/eslint-utils');
  3. const getReferences = (scope, nodeOrName) => {
  4. const {references = []} = findVariable(scope, nodeOrName) || {};
  5. return references;
  6. };
  7. /**
  8. Check if `this`, `arguments`, or the function name is used inside of itself.
  9. @param {Node} functionNode - The function node.
  10. @param {Scope} functionScope - The scope of the function node.
  11. @returns {boolean}
  12. */
  13. function isFunctionSelfUsedInside(functionNode, functionScope) {
  14. /* c8 ignore next 3 */
  15. if (functionScope.block !== functionNode) {
  16. throw new Error('"functionScope" should be the scope of "functionNode".');
  17. }
  18. const {type, id} = functionNode;
  19. if (type === 'ArrowFunctionExpression') {
  20. return false;
  21. }
  22. if (functionScope.thisFound) {
  23. return true;
  24. }
  25. if (getReferences(functionScope, 'arguments').some(({from}) => from === functionScope)) {
  26. return true;
  27. }
  28. if (id && getReferences(functionScope, id).length > 0) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. module.exports = isFunctionSelfUsedInside;