|
| 1 | +// ignore_for_file: public_member_api_docs |
| 2 | + |
| 3 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 4 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 5 | + |
| 6 | +import '../../../../../utils/node_utils.dart'; |
| 7 | +import '../../../lint_utils.dart'; |
| 8 | +import '../../../models/internal_resolved_unit_result.dart'; |
| 9 | +import '../../../models/issue.dart'; |
| 10 | +import '../../../models/severity.dart'; |
| 11 | +import '../../models/common_rule.dart'; |
| 12 | +import '../../rule_utils.dart'; |
| 13 | + |
| 14 | +part 'config_parser.dart'; |
| 15 | +part 'visitor.dart'; |
| 16 | + |
| 17 | +class PreferCorrectTestFileNameRule extends CommonRule { |
| 18 | + static const String ruleId = 'prefer-correct-test-file-name'; |
| 19 | + |
| 20 | + static const _warningMessage = 'Test file name should end with '; |
| 21 | + |
| 22 | + final String _fileNamePattern; |
| 23 | + |
| 24 | + PreferCorrectTestFileNameRule([Map<String, Object> config = const {}]) |
| 25 | + : _fileNamePattern = _ConfigParser.parseNamePattern(config), |
| 26 | + super( |
| 27 | + id: ruleId, |
| 28 | + severity: readSeverity(config, Severity.warning), |
| 29 | + excludes: |
| 30 | + hasExcludes(config) ? readExcludes(config) : ['/**', '!test/**'], |
| 31 | + ); |
| 32 | + |
| 33 | + @override |
| 34 | + Iterable<Issue> check(InternalResolvedUnitResult source) { |
| 35 | + final visitor = _Visitor(source.path, _fileNamePattern); |
| 36 | + |
| 37 | + source.unit.visitChildren(visitor); |
| 38 | + |
| 39 | + return visitor.declaration |
| 40 | + .map((declaration) => createIssue( |
| 41 | + rule: this, |
| 42 | + location: nodeLocation(node: declaration, source: source), |
| 43 | + message: '$_warningMessage$_fileNamePattern', |
| 44 | + )) |
| 45 | + .toList(growable: false); |
| 46 | + } |
| 47 | +} |
0 commit comments