|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/dart/ast/syntactic_entity.dart'; |
| 3 | +import 'package:analyzer/dart/ast/token.dart'; |
| 4 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 5 | +import 'package:analyzer/dart/element/type.dart'; |
| 6 | + |
| 7 | +import '../../../../../utils/node_utils.dart'; |
| 8 | +import '../../../models/context_message.dart'; |
| 9 | +import '../../../models/entity_type.dart'; |
| 10 | +import '../../../models/internal_resolved_unit_result.dart'; |
| 11 | +import '../../../models/scoped_class_declaration.dart'; |
| 12 | +import '../../../models/scoped_function_declaration.dart'; |
| 13 | +import '../../metric_utils.dart'; |
| 14 | +import '../../models/file_metric.dart'; |
| 15 | +import '../../models/metric_computation_result.dart'; |
| 16 | +import '../../models/metric_documentation.dart'; |
| 17 | +import '../../models/metric_value.dart'; |
| 18 | + |
| 19 | +part 'visitor.dart'; |
| 20 | + |
| 21 | +const _documentation = MetricDocumentation( |
| 22 | + name: 'Technical Debt', |
| 23 | + shortName: 'TECHDEBT', |
| 24 | + measuredType: EntityType.fileEntity, |
| 25 | + recomendedThreshold: 0, |
| 26 | +); |
| 27 | + |
| 28 | +/// Technical Debt (TECHDEBT) |
| 29 | +/// |
| 30 | +/// Technical debt is a concept in software development that reflects the |
| 31 | +/// implied cost of additional rework caused by choosing an easy solution now |
| 32 | +/// instead of using a better approach that would take longer. |
| 33 | +class TechnicalDebtMetric extends FileMetric<double> { |
| 34 | + static const String metricId = 'technical-debt'; |
| 35 | + |
| 36 | + final double _todoCommentCost; |
| 37 | + final double _ignoreCommentCost; |
| 38 | + final double _ignoreForFileCommentCost; |
| 39 | + final double _asDynamicExpressionCost; |
| 40 | + final double _deprecatedAnnotationsCost; |
| 41 | + final double _fileNullSafetyMigrationCost; |
| 42 | + final String? _unitType; |
| 43 | + |
| 44 | + TechnicalDebtMetric({Map<String, Object> config = const {}}) |
| 45 | + : _todoCommentCost = |
| 46 | + readConfigValue<double>(config, metricId, 'todo-cost') ?? 0.0, |
| 47 | + _ignoreCommentCost = |
| 48 | + readConfigValue<double>(config, metricId, 'ignore-cost') ?? 0.0, |
| 49 | + _ignoreForFileCommentCost = |
| 50 | + readConfigValue<double>(config, metricId, 'ignore-for-file-cost') ?? |
| 51 | + 0.0, |
| 52 | + _asDynamicExpressionCost = |
| 53 | + readConfigValue<double>(config, metricId, 'as-dynamic-cost') ?? 0.0, |
| 54 | + _deprecatedAnnotationsCost = readConfigValue<double>( |
| 55 | + config, |
| 56 | + metricId, |
| 57 | + 'deprecated-annotations', |
| 58 | + ) ?? |
| 59 | + 0.0, |
| 60 | + _fileNullSafetyMigrationCost = readConfigValue<double>( |
| 61 | + config, |
| 62 | + metricId, |
| 63 | + 'file-nullsafety-migration', |
| 64 | + ) ?? |
| 65 | + 0.0, |
| 66 | + _unitType = readConfigValue<String>(config, metricId, 'unit-type'), |
| 67 | + super( |
| 68 | + id: metricId, |
| 69 | + documentation: _documentation, |
| 70 | + threshold: readNullableThreshold<double>(config, metricId), |
| 71 | + levelComputer: valueLevel, |
| 72 | + ); |
| 73 | + |
| 74 | + @override |
| 75 | + MetricComputationResult<double> computeImplementation( |
| 76 | + AstNode node, |
| 77 | + Iterable<ScopedClassDeclaration> classDeclarations, |
| 78 | + Iterable<ScopedFunctionDeclaration> functionDeclarations, |
| 79 | + InternalResolvedUnitResult source, |
| 80 | + Iterable<MetricValue<num>> otherMetricsValues, |
| 81 | + ) { |
| 82 | + var debt = 0.0; |
| 83 | + |
| 84 | + final visitor = _Visitor()..visitComments(node); |
| 85 | + node.visitChildren(visitor); |
| 86 | + |
| 87 | + debt += visitor.todos.length * _todoCommentCost; |
| 88 | + debt += visitor.ignore.length * _ignoreCommentCost; |
| 89 | + debt += visitor.ignoreForFile.length * _ignoreForFileCommentCost; |
| 90 | + debt += visitor.asDynamicExpressions.length * _asDynamicExpressionCost; |
| 91 | + debt += visitor.deprecatedAnnotations.length * _deprecatedAnnotationsCost; |
| 92 | + debt += visitor.nonNullSafetyLanguageComment.length * |
| 93 | + _fileNullSafetyMigrationCost; |
| 94 | + |
| 95 | + return MetricComputationResult( |
| 96 | + value: debt, |
| 97 | + context: [ |
| 98 | + if (_todoCommentCost > 0.0) |
| 99 | + ..._context(visitor.todos, source, _todoCommentCost), |
| 100 | + if (_ignoreCommentCost > 0.0) |
| 101 | + ..._context(visitor.ignore, source, _ignoreCommentCost), |
| 102 | + if (_ignoreForFileCommentCost > 0.0) |
| 103 | + ..._context(visitor.ignoreForFile, source, _ignoreForFileCommentCost), |
| 104 | + if (_asDynamicExpressionCost > 0.0) |
| 105 | + ..._context( |
| 106 | + visitor.asDynamicExpressions, |
| 107 | + source, |
| 108 | + _asDynamicExpressionCost, |
| 109 | + ), |
| 110 | + if (_deprecatedAnnotationsCost > 0.0) |
| 111 | + ..._context( |
| 112 | + visitor.deprecatedAnnotations, |
| 113 | + source, |
| 114 | + _deprecatedAnnotationsCost, |
| 115 | + ), |
| 116 | + if (_fileNullSafetyMigrationCost > 0.0) |
| 117 | + ..._context( |
| 118 | + visitor.nonNullSafetyLanguageComment, |
| 119 | + source, |
| 120 | + _fileNullSafetyMigrationCost, |
| 121 | + ), |
| 122 | + ], |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @override |
| 127 | + String commentMessage(String nodeType, double value, double? threshold) { |
| 128 | + final exceeds = threshold != null && value > threshold |
| 129 | + ? ', exceeds the maximum of $threshold allowed' |
| 130 | + : ''; |
| 131 | + final debt = '$value swe ${value == 1 ? 'hour' : 'hours'} of debt'; |
| 132 | + |
| 133 | + return 'This $nodeType has $debt$exceeds.'; |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + String? unitType(double value) => _unitType; |
| 138 | + |
| 139 | + Iterable<ContextMessage> _context( |
| 140 | + Iterable<SyntacticEntity> complexityEntities, |
| 141 | + InternalResolvedUnitResult source, |
| 142 | + double cost, |
| 143 | + ) => |
| 144 | + complexityEntities |
| 145 | + .map((entity) => ContextMessage( |
| 146 | + message: '+$cost ${_unitType ?? ''}'.trim(), |
| 147 | + location: nodeLocation(node: entity, source: source), |
| 148 | + )) |
| 149 | + .toList() |
| 150 | + ..sort((a, b) => a.location.start.compareTo(b.location.start)); |
| 151 | +} |
0 commit comments