is-shadowed.js 880 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. /**
  3. * Finds the eslint-scope reference in the given scope.
  4. * @param {Object} scope The scope to search.
  5. * @param {ASTNode} node The identifier node.
  6. * @returns {Reference|undefined} Returns the found reference or null if none were found.
  7. */
  8. function findReference(scope, node) {
  9. const references = scope.references
  10. .filter(reference => reference.identifier === node);
  11. if (references.length === 1) {
  12. return references[0];
  13. }
  14. }
  15. /**
  16. * Checks if the given identifier node is shadowed in the given scope.
  17. * @param {Object} scope The current scope.
  18. * @param {string} node The identifier node to check
  19. * @returns {boolean} Whether or not the name is shadowed.
  20. */
  21. function isShadowed(scope, node) {
  22. const reference = findReference(scope, node);
  23. return (
  24. reference?.resolved
  25. && reference.resolved.defs.length > 0
  26. );
  27. }
  28. module.exports = isShadowed;