compileAllModules method

Map<String, String> compileAllModules()

Compile every non-base module in the program, returning a map of moduleName → dartSource.

Use for library packages that have no entry point. Base modules (std, std_collections, etc.) are skipped. Each module is compiled independently, so the result can be written to separate files.

Modules that fail to compile (even after the raw fallback) are omitted from the result and recorded in failedModules; check that map to detect partial output.

Implementation

Map<String, String> compileAllModules() {
  failedModules.clear();
  final result = <String, String>{};
  for (final module in program.modules) {
    // Skip base modules and empty stubs.
    final allBase = module.functions.every((f) => f.isBase);
    if (allBase && module.functions.isNotEmpty) continue;
    // A module with no local declarations is a stub UNLESS it carries
    // re-export directives in its metadata — those facades (e.g.
    // `matcher.dart`, `shelf.dart`) must still be emitted so downstream
    // imports resolve.
    final hasExports = _moduleHasExports(module);
    if (module.functions.isEmpty &&
        module.typeDefs.isEmpty &&
        module.typeAliases.isEmpty &&
        module.enums.isEmpty &&
        !hasExports &&
        module.name != program.entryModule) {
      continue;
    }
    try {
      result[module.name] = compileModule(module.name);
      // coverage:ignore-start
      // Defensive double-fallback: only fires if the dart_style formatter
      // throws (then retry raw), and the raw emit ALSO throws (then record
      // the failure). No deterministic input triggers either in the corpus.
    } catch (e) {
      // If formatting fails, try raw.
      try {
        result[module.name] = compileModuleRaw(module.name);
      } catch (rawError) {
        // Module failed both formatted and raw compilation. Record it so
        // callers can detect partial output rather than dropping it silently.
        failedModules[module.name] = rawError;
      }
    }
    // coverage:ignore-end
  }
  return result;
}