resolveHierarchy function Null safety

Future<DocumentHierarchy?> resolveHierarchy(
  1. Document document,
  2. Directory currentDirectory,
  3. void onError(
    1. JaelError e
    )?
)

Resolves the document hierarchy at a given node in the tree.

Implementation

Future<DocumentHierarchy?> resolveHierarchy(Document document,
    Directory currentDirectory, void Function(JaelError e)? onError) async {
  var extendsTemplates = Queue<Element>();
  String? parent;

  Document? doc = document;
  while (doc != null && (parent = getParent(doc, onError)) != null) {
    try {
      extendsTemplates.addFirst(doc.root);
      var file = currentDirectory.childFile(parent!);
      var parsed = parseDocument(await file.readAsString(),
          sourceUrl: file.uri, onError: onError)!;

      doc = await resolveIncludes(parsed, currentDirectory, onError);
    } on FileSystemException catch (e) {
      onError!(
          JaelError(JaelErrorSeverity.error, e.message, document.root.span));
      return null;
    }
  }

  if (doc == null) {
    return null;
  }
  return DocumentHierarchy(doc, extendsTemplates);
}