parse function Null safety

Map<String, List<String>> parse(
  1. List<Node> nodes
)

Implementation

Map<String, List<String>> parse(List<Node> nodes) {
    final Map<String, List<String>> codePerFile = <String, List<String>>{};
    for (final Node node in nodes) codePerFile[node.filename] ??= <String>[];
    nodes.where((Node node) => node.type == NodeType.enumeration).forEach(_parseEnum);
    nodes.where((Node node) => node.type == NodeType.type).forEach(_parseType);
    nodes.where((Node node) => node.type == NodeType.message || node.type == NodeType.request).forEach(_parseCommand);
    for (final String filename in codePerFile.keys) {
        codePerFile[filename] = <String>[
            "import 'package:packme/packme.dart';",
            '',
            ...enums.values.where((Enum item) => item.filename == filename)
                .fold(<String>[], (Iterable<String> a, Enum b) => a.toList() + b.output()),
            ...types.values.where((Message item) => item.filename == filename)
                .fold(<String>[], (Iterable<String> a, Message b) => a.toList() + b.output()),
            ...messages.values.where((Message item) => item.filename == filename)
                .fold(<String>[], (Iterable<String> a, Message b) => a.toList() + b.output()),
            if (messages.entries.where((MapEntry<int, Message> entry) => entry.value.filename == filename).isNotEmpty) ...<String>[
                'final Map<int, PackMeMessage Function()> ${validName(filename)}MessageFactory = <int, PackMeMessage Function()>{',
                ...messages.entries.where((MapEntry<int, Message> entry) => entry.value.filename == filename)
                    .map((MapEntry<int, Message> entry) => '${entry.key}: () => ${entry.value.name}.\$empty(),'),
                '};'
            ]
        ];

        /// Add additional import lines according to messages' references data.
        final List<FieldType> references = <FieldType>[];
        for (final Message message in messages.values.where((Message item) => item.filename == filename)) {
            references.addAll(message.references.where((FieldType reference) => !references.contains(reference)));
        }
        final Map<String, List<String>> imports = <String, List<String>>{};
        for (final FieldType reference in references) {
            /// Skip if enum/type declaration is in the same file.
            if (reference.filename == filename) continue;
            imports[reference.filename] ??= <String>[];
            if (!imports[reference.filename]!.contains(reference.name)) {
                imports[reference.filename]!.add(reference.name);
            }
        }
        final List<String> keys = imports.keys.toList();

        /// Sort imports properly.
        keys.sort((String a, String b) => '$a.generated.dart'.compareTo('$b.generated.dart'));
        for (final String key in keys.reversed) {
            imports[key]!.sort();
            codePerFile[filename]!.insert(1, "import '$key.generated.dart' show ${imports[key]!.join(', ')};");
        }
    }
    return codePerFile;
}