|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:collection/collection.dart'; |
| 4 | +import 'package:path/path.dart'; |
| 5 | +import 'package:source_span/source_span.dart'; |
| 6 | +import 'package:yaml/yaml.dart'; |
| 7 | + |
| 8 | +import 'lint_analysis_config.dart'; |
| 9 | +import 'models/issue.dart'; |
| 10 | +import 'models/lint_file_report.dart'; |
| 11 | +import 'models/severity.dart'; |
| 12 | + |
| 13 | +class LintAnalysisOptionsValidator { |
| 14 | + static LintFileReport? validateOptions( |
| 15 | + LintAnalysisConfig config, |
| 16 | + String rootFolder, |
| 17 | + ) { |
| 18 | + final path = config.analysisOptionsPath; |
| 19 | + final file = path != null && File(path).existsSync() ? File(path) : null; |
| 20 | + if (file == null) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + final fileContent = file.readAsStringSync(); |
| 25 | + final node = loadYamlNode(fileContent); |
| 26 | + final rulesList = _getRulesList(node); |
| 27 | + if (rulesList == null) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + final parsedRuleIds = config.codeRules.map((rule) => rule.id).toList(); |
| 32 | + final issues = <Issue>[]; |
| 33 | + |
| 34 | + for (final rule in rulesList) { |
| 35 | + if (!parsedRuleIds.contains(rule.ruleName)) { |
| 36 | + issues.add( |
| 37 | + Issue( |
| 38 | + ruleId: 'unknown-config', |
| 39 | + severity: Severity.warning, |
| 40 | + message: |
| 41 | + "'${rule.ruleName}' is not recognized as a valid rule name.", |
| 42 | + documentation: Uri.parse('https://dartcodemetrics.dev/docs/rules'), |
| 43 | + location: _copySpanWithOffset(rule.span), |
| 44 | + ), |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (issues.isNotEmpty) { |
| 50 | + final filePath = file.path; |
| 51 | + final relativePath = relative(filePath, from: rootFolder); |
| 52 | + |
| 53 | + return LintFileReport.onlyIssues( |
| 54 | + path: file.path, |
| 55 | + relativePath: relativePath, |
| 56 | + issues: issues, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + static List<_RuleWithSpan>? _getRulesList(YamlNode node) { |
| 64 | + if (node is YamlMap) { |
| 65 | + final rules = |
| 66 | + (node['dart_code_metrics'] as YamlMap?)?['rules'] as YamlNode?; |
| 67 | + if (rules is YamlList) { |
| 68 | + return rules.nodes |
| 69 | + // ignore: avoid_types_on_closure_parameters |
| 70 | + .map((Object? rule) { |
| 71 | + if (rule is YamlMap) { |
| 72 | + final key = rule.nodes.keys.first as Object?; |
| 73 | + if (key is YamlScalar && key.value is String) { |
| 74 | + return _RuleWithSpan(key.value as String, key.span); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (rule is YamlScalar && rule.value is String) { |
| 79 | + return _RuleWithSpan(rule.value as String, rule.span); |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + }) |
| 84 | + .whereNotNull() |
| 85 | + .toList(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + static SourceSpan _copySpanWithOffset(SourceSpan span) => SourceSpan( |
| 93 | + SourceLocation( |
| 94 | + span.start.offset, |
| 95 | + sourceUrl: span.start.sourceUrl, |
| 96 | + line: span.start.line + 1, |
| 97 | + column: span.start.column + 1, |
| 98 | + ), |
| 99 | + SourceLocation( |
| 100 | + span.end.offset, |
| 101 | + sourceUrl: span.end.sourceUrl, |
| 102 | + line: span.end.line + 1, |
| 103 | + column: span.end.column + 1, |
| 104 | + ), |
| 105 | + span.text, |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +class _RuleWithSpan { |
| 110 | + final String ruleName; |
| 111 | + final SourceSpan span; |
| 112 | + |
| 113 | + const _RuleWithSpan(this.ruleName, this.span); |
| 114 | +} |
0 commit comments