findElementRecursively function

Element? findElementRecursively(
  1. Element element,
  2. DartMemberPath path
)

Finds a Dart member element by its path in the given element so we do not need a visitor.

Implementation

Element? findElementRecursively(Element element, p.DartMemberPath path) {
  if (element.displayName == path.first && path.length == 1) {
    return element;
  }
  if (element.displayName.isNotEmpty) {
    path = path.withoutParent();
  }
  if (path.isEmpty) {
    return null;
  }
  for (var child in element.children) {
    var found = findElementRecursively(child, path);
    if (found != null) {
      return found;
    }
  }
  return null;
}