check method

  1. @override
void check(
  1. DcqRegistry registry
)

Implementation

@override
void check(DcqRegistry registry) {
  registry.addCompilationUnit((node) {
    final unit = registry.context.currentUnit;
    if (unit == null) return;

    final filePath = unit.file.path;

    // Only check files inside lib/src/ (not barrel files in lib/).
    final libSrc = '${p.separator}lib${p.separator}src${p.separator}';
    if (!filePath.contains(libSrc)) return;

    // Skip generated files.
    if (filePath.endsWith('.g.dart') || filePath.endsWith('.freezed.dart')) {
      return;
    }

    // Build expected test path.
    final libIndex = filePath.lastIndexOf('${p.separator}lib${p.separator}');
    if (libIndex == -1) return;

    final projectRoot = filePath.substring(0, libIndex);
    final relativePath = filePath.substring(
      libIndex + '${p.separator}lib${p.separator}'.length,
    );

    final baseName = p.basenameWithoutExtension(relativePath);
    final dirName = p.dirname(relativePath);
    final testPath = p.join(
      projectRoot,
      'test',
      dirName,
      '${baseName}_test.dart',
    );

    // Note: uses dart:io directly; may not work in all analyzer host environments.
    if (!File(testPath).existsSync()) {
      // Report on the library directive or first declaration.
      final target = node.directives.isNotEmpty
          ? node.directives.first
          : node.declarations.isNotEmpty
          ? node.declarations.first
          : null;
      if (target != null) {
        reportAtNode(target);
      }
    }
  });
}