scope.js 891 B

123456789101112131415161718192021222324252627282930
  1. module.exports = {
  2. getScope
  3. }
  4. /**
  5. * Gets the scope for the current node
  6. * @param {RuleContext} context The rule context
  7. * @param {ESNode} currentNode The node to get the scope of
  8. * @returns { import('eslint').Scope.Scope } The scope information for this node
  9. */
  10. function getScope(context, currentNode) {
  11. // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
  12. const inner = currentNode.type !== 'Program'
  13. const scopeManager = context.getSourceCode().scopeManager
  14. /** @type {ESNode | null} */
  15. let node = currentNode
  16. for (; node; node = /** @type {ESNode | null} */ (node.parent)) {
  17. const scope = scopeManager.acquire(node, inner)
  18. if (scope) {
  19. if (scope.type === 'function-expression-name') {
  20. return scope.childScopes[0]
  21. }
  22. return scope
  23. }
  24. }
  25. return scopeManager.scopes[0]
  26. }