duplicateSpecTypes function

Map<String, List<File>> duplicateSpecTypes(
  1. Directory libDir
)

Bridge types (@HybridStruct, @HybridRecord, @HybridEnum, @NitroVariant, @NitroCustomType) declared in more than one .native.dart under libDir, with the files declaring each. Both specs compile into one Dart library export and one Swift module, so the build fails later with an "ambiguous" error far from the cause.

Implementation

Map<String, List<File>> duplicateSpecTypes(Directory libDir) {
  if (!libDir.existsSync()) return {};
  // The nearest class/enum after the annotation, whatever the annotations in
  // between carry (strings with parentheses, nested calls).
  final decl = RegExp(
    r'^\s*@(?:HybridStruct|HybridRecord|HybridEnum|NitroVariant|NitroCustomType)\b[\s\S]*?\b(?:class|enum)\s+(\w+)',
    multiLine: true,
  );
  // Comments are no declarations: `/* … */`, then `//` and `///` lines.
  final comments = RegExp(r'/\*[\s\S]*?\*/|//[^\n]*');
  final byName = <String, List<File>>{};
  final specs = libDir.listSync(recursive: true).whereType<File>().where((f) => f.path.endsWith('.native.dart')).toList()
    ..sort((a, b) => a.path.compareTo(b.path));
  for (final f in specs) {
    for (final m in decl.allMatches(f.readAsStringSync().replaceAll(comments, ''))) {
      byName.putIfAbsent(m.group(1)!, () => []).add(f);
    }
  }
  return {for (final e in byName.entries) if (e.value.length > 1) e.key: e.value};
}