# Generated File Handling - Implementation Summary

## What Was Improved

Enhanced the Dart analyzer to skip a comprehensive list of generated files automatically.

## Changes Made

### 1. Updated analyzer_service.dart

**File:** `dart_analyzer/lib/analyzer_service.dart`

**What changed:** Expanded `_generatedFilePatterns` from 7 patterns to 29 comprehensive patterns covering all major Dart/Flutter code generation tools.

**New patterns added:**
- `.gen.dart` - flutter_gen (assets, localization)
- `.graphql.dart` - GraphQL (alternative)
- `.pb.dart`, `.pbenum.dart`, `.pbserver.dart`, `.pbjson.dart` - Protocol buffers
- `.config.dart`, `.iconfig.dart` - Injectable dependency injection
- `.mapper.dart` - dart_mappable
- `.drift.dart` - Drift database ORM
- `.chopper.dart` - Chopper HTTP client
- `.reflectable.dart` - Reflectable code generation
- `pigeon.dart` - Pigeon platform channels
- `l10n.dart`, `_l10n.dart` - Flutter internationalization
- `/generated/` directory pattern
- `.generated.` filename pattern

### 2. Updated Documentation

**File:** `docs/skills/CREATE_NEW_DART_RULE.md`

**What changed:** Added new section "8. Generated File Handling" with:
- Complete list of all 29 skipped file patterns
- Explanation that the check is centralized and automatic
- Guidance on when NOT to add file checks in individual rules
- Example of rule-specific file path checks (for test files, config files, etc.)
- Updated all subsequent section numbers (9-15)
- Added changelog entry for v1.2.0

## How It Works

### Centralized Skip Logic

The `analyzeFile` method in `analyzer_service.dart` performs the check:

```dart
if (_isGeneratedFile(absolutePath)) {
  _log('Skipping generated file: $absolutePath');
  return [];
}
```

This happens **before** any rule analysis, so:
- ✅ All Dart rules (D001-D025) automatically skip generated files
- ✅ All Common rules (C-series) automatically skip generated files in Dart
- ✅ All Security rules (S-series) automatically skip generated files in Dart
- ✅ Consistent behavior across all rules
- ✅ Single point of maintenance

### Pattern Matching

The method checks both:
1. **Full file path** (lowercase): `lib/models/user.g.dart`
2. **File name only**: `user.g.dart`

This ensures patterns like `__generated__` (directory) and `.g.dart` (extension) both work correctly.

## Testing

Created `test_generated_files.dart` to verify all patterns work correctly.

**Test results:** ✅ All 27 test cases passed
- 23 generated files correctly identified for skipping
- 4 regular files correctly identified for analysis

## Benefits

1. **No Code Changes Needed in Rules**: All existing rules (D001-D025) automatically benefit from this improvement
2. **Better Performance**: Skip expensive AST parsing for files we don't care about
3. **Reduced Noise**: No false positives from generated code
4. **Easy Maintenance**: Add new patterns in one place when new generators emerge
5. **Clear Documentation**: Developers know what files are skipped without checking code

## Common Generated Files in Flutter Projects

| Generator | Pattern | Example |
|-----------|---------|---------|
| json_serializable | `.g.dart` | `user.g.dart` |
| built_value | `.g.dart` | `serializers.g.dart` |
| flutter_gen | `.gen.dart` | `assets.gen.dart` |
| freezed | `.freezed.dart` | `state.freezed.dart` |
| injectable | `.config.dart` | `injection.config.dart` |
| dart_mappable | `.mapper.dart` | `user.mapper.dart` |
| drift | `.drift.dart` | `database.drift.dart` |
| GraphQL | `.gql.dart` | `schema.gql.dart` |
| gRPC | `.gr.dart` | `service.gr.dart` |
| Protocol Buffers | `.pb.dart` | `message.pb.dart` |
| Mockito | `.mocks.dart` | `service.mocks.dart` |
| Pigeon | `pigeon.dart` | `pigeon.dart` |
| Intl | `l10n.dart` | `app_localizations.dart` |

## When to Add File Checks in Rules

**DON'T** add generated file checks in individual rules - it's already handled centrally.

**DO** add file-specific checks for:
- Test files (`_test.dart`) - if rule behavior differs for tests
- Config files - if hardcoded values are acceptable in configs
- Example/fixture files - if rule should skip demonstration code
- Platform-specific code - if rule only applies to certain platforms

**Example:**

```dart
// Good: Rule-specific business logic
if (filePath.contains('_test.dart')) {
  return []; // Allow print() in test files
}

// Bad: Don't duplicate generated file check
if (filePath.endsWith('.g.dart')) {  // ❌ Already handled centrally
  return [];
}
```

## Next Steps

1. ✅ Patterns updated in analyzer_service.dart
2. ✅ Documentation updated with new section
3. ✅ Test script created and verified
4. ✅ Dart binary rebuilt: `cd dart_analyzer && dart compile exe bin/sunlint_dart_analyzer.dart -o bin/sunlint-dart-macos`
5. ✅ Binary is in `dart_analyzer/bin/` where the adapter looks for it (no need to copy elsewhere)
6. ✅ No changes needed in existing rules - they automatically benefit

## Migration Guide

**For Existing Rules:**
No migration needed! All rules automatically use the centralized check.

**For New Rules:**
Follow the updated CREATE_NEW_DART_RULE.md guide which now documents the automatic skip behavior.

**For Rule Reviewers:**
If you see generated file checks in rule implementations, they can be removed as redundant.

## Notes

- The original implementation already had 7 patterns - this extends it to 29
- The check uses regex for flexibility (e.g., case-insensitive matching)
- The logging statement helps debug which files are being skipped
- Pattern matching checks both full path and filename for maximum coverage
