replaceBlocks function Null safety

Iterable<ElementChild> replaceBlocks(
  1. Element element,
  2. Map<String?, RegularElement> definedOverrides,
  3. void onError(
    1. JaelError e
    )?,
  4. bool replaceWithDefault,
  5. bool anyTemplatesRemain
)

Implementation

Iterable<ElementChild> replaceBlocks(
    Element element,
    Map<String?, RegularElement> definedOverrides,
    void Function(JaelError e)? onError,
    bool replaceWithDefault,
    bool anyTemplatesRemain) {
  if (element.tagName.name == 'block') {
    var nameAttr = element.attributes.firstWhereOrNull((a) => a.name == 'name');
    var name = nameAttr?.value?.compute(SymbolTable());

    if (name?.trim()?.isNotEmpty != true) {
      onError!(JaelError(
          JaelErrorSeverity.warning,
          'This <block> has no `name` attribute, and will be outputted as-is.',
          element.span));
      return [element];
    } else if (!definedOverrides.containsKey(name)) {
      if (element is RegularElement) {
        if (anyTemplatesRemain || !replaceWithDefault) {
          // If there are still templates remaining, this current block may eventually
          // be resolved. Keep it alive.

          // We can't get rid of the block itself, but it may have blocks as children...
          var inner = allChildrenOfRegularElement(element, definedOverrides,
              onError, replaceWithDefault, anyTemplatesRemain);

          return [
            RegularElement(
                element.lt,
                element.tagName,
                element.attributes,
                element.gt,
                inner,
                element.lt2,
                element.slash,
                element.tagName2,
                element.gt2)
          ];
        } else {
          // Otherwise, just return the default contents.
          return element.children;
        }
      } else {
        return [element];
      }
    } else {
      return allChildrenOfRegularElement(definedOverrides[name]!,
          definedOverrides, onError, replaceWithDefault, anyTemplatesRemain);
    }
  } else if (element is SelfClosingElement) {
    return [element];
  } else {
    return [
      replaceChildrenOfRegularElement(element as RegularElement,
          definedOverrides, onError, replaceWithDefault, anyTemplatesRemain)
    ];
  }
}