expandEmptyNodes static method

  1. @visibleForTesting
Iterable<ProviderNode> expandEmptyNodes(
  1. List<ProviderNode> nodes
)

Recursively replaces any empty nodes with their children.

For example, this will expand the following list of nodes

[
  ProviderNode {
    providers: []
    children: [
      ProviderNode { providers: [a, b] },
      ProviderNode { providers: [c] },
    ],
  },
  ProviderNode { providers: [x, y, z] },
]

into

[
  ProviderNode { providers: [a, b] },
  ProviderNode { providers: [c] },
  ProviderNode { providers: [x, y, z] },
]

This transformation simplifies optimization and code generation, without affecting resolution of providers at run-time.

Implementation

@visibleForTesting
static Iterable<ProviderNode> expandEmptyNodes(List<ProviderNode> nodes) {
  return nodes.expand((node) {
    // Recursively expand empty children.
    final childrenWithProviders = expandEmptyNodes(node.children);
    if (node.providers.isEmpty) {
      // If this node has no providers, replace it with its children.
      return childrenWithProviders;
    } else {
      // Otherwise, copy the node with its expanded children.
      return [
        ProviderNode(
          node.start,
          node.end,
          providers: node.providers,
          children: childrenWithProviders.toList(),
        )
      ];
    }
  });
}