|
| 1 | +import 'package:analyzer/analysis_rule/rule_context.dart'; |
| 2 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 3 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 4 | +import 'package:analyzer/dart/element/element.dart'; |
| 5 | +import 'package:path/path.dart' as p; |
| 6 | +import 'package:solid_lints/src/lints/prefer_mirror_test_structure/prefer_mirror_test_structure_rule.dart'; |
| 7 | + |
| 8 | +/// The AST visitor that checks if test files mirror the folder structure of |
| 9 | +/// their corresponding implementation files. |
| 10 | +class PreferMirrorTestStructureVisitor extends SimpleAstVisitor<void> { |
| 11 | + static const _testSuffix = '_test.dart'; |
| 12 | + static const _dartSuffix = '.dart'; |
| 13 | + static const _testDirName = 'test'; |
| 14 | + static const _libDirName = 'lib'; |
| 15 | + |
| 16 | + /// The rule that instantiated this visitor. |
| 17 | + final PreferMirrorTestStructureRule rule; |
| 18 | + |
| 19 | + /// The context of the current analysis rule. |
| 20 | + final RuleContext context; |
| 21 | + |
| 22 | + /// Creates a new instance of [PreferMirrorTestStructureVisitor]. |
| 23 | + PreferMirrorTestStructureVisitor({ |
| 24 | + required this.rule, |
| 25 | + required this.context, |
| 26 | + }); |
| 27 | + |
| 28 | + @override |
| 29 | + void visitCompilationUnit(CompilationUnit node) { |
| 30 | + final currentUnit = context.currentUnit; |
| 31 | + final packageRoot = context.package?.root.path; |
| 32 | + if (currentUnit == null || |
| 33 | + packageRoot == null || |
| 34 | + !context.isInTestDirectory) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + final file = currentUnit.file; |
| 39 | + final fileName = file.shortName; |
| 40 | + if (!fileName.endsWith(_testSuffix)) return; |
| 41 | + |
| 42 | + final pathContext = file.provider.pathContext; |
| 43 | + final testDir = pathContext.relative( |
| 44 | + file.parent.path, |
| 45 | + from: pathContext.join(packageRoot, _testDirName), |
| 46 | + ); |
| 47 | + |
| 48 | + final libRoot = pathContext.join(packageRoot, _libDirName); |
| 49 | + final targetFileName = fileName.replaceFirst(_testSuffix, _dartSuffix); |
| 50 | + |
| 51 | + for (final directive in node.directives.whereType<ImportDirective>()) { |
| 52 | + if (directive.libraryImport?.uri case DirectiveUriWithSource( |
| 53 | + :final source, |
| 54 | + )) { |
| 55 | + final importedPath = source.fullName; |
| 56 | + if (pathContext.basename(importedPath) != targetFileName || |
| 57 | + !pathContext.isWithin(libRoot, importedPath)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + final libDir = pathContext.relative( |
| 62 | + pathContext.dirname(importedPath), |
| 63 | + from: libRoot, |
| 64 | + ); |
| 65 | + if (libDir == testDir) return; |
| 66 | + |
| 67 | + final nativePath = pathContext.normalize( |
| 68 | + pathContext.join(_testDirName, libDir, fileName), |
| 69 | + ); |
| 70 | + currentUnit.diagnosticReporter.atNode( |
| 71 | + directive.uri, |
| 72 | + rule.diagnosticCode, |
| 73 | + arguments: [p.posix.joinAll(pathContext.split(nativePath))], |
| 74 | + ); |
| 75 | + |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments