Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

refactor: prepare for complex metric values #698

Merged
merged 4 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"setstate",
"SLOC",
"todos",
"unawaited"
"unawaited",
"writeln"
]
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
## Unreleased

* feat: add static code diagnostics `avoid-border-all`.
* chore: activate new lint rules.
* feat: improve `avoid-returning-widgets` builder functions handling.
* fix: correctly handle const maps in `no-magic-number`.
* fix: correctly handle excluded files for `check-unused-code`.
* chore: activate new lint rules.
* refactor: prepare for complex metric values.

## 4.11.0-dev.1

Expand Down
6 changes: 3 additions & 3 deletions lib/src/analyzers/lint_analyzer/lint_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class LintAnalyzer {
final classRecords = <ScopedClassDeclaration, Report>{};

for (final classDeclaration in visitor.classes) {
final metrics = <MetricValue<num>>[];
final metrics = <MetricValue>[];

for (final metric in config.classesMetrics) {
if (metric.supports(
Expand Down Expand Up @@ -336,7 +336,7 @@ class LintAnalyzer {
InternalResolvedUnitResult source,
LintAnalysisConfig config,
) {
final metrics = <MetricValue<num>>[];
final metrics = <MetricValue>[];

for (final metric in config.fileMetrics) {
if (metric.supports(
Expand Down Expand Up @@ -371,7 +371,7 @@ class LintAnalyzer {
final functionRecords = <ScopedFunctionDeclaration, Report>{};

for (final function in visitor.functions) {
final metrics = <MetricValue<num>>[];
final metrics = <MetricValue>[];

for (final metric in config.methodsMetrics) {
if (metric.supports(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const _documentation = MetricDocumentation(
name: 'Cyclomatic Complexity',
shortName: 'CYCLO',
measuredType: EntityType.methodEntity,
recomendedThreshold: 20,
recommendedThreshold: 20,
);

/// Cyclomatic Complexity (CYCLO)
Expand All @@ -45,7 +45,7 @@ class CyclomaticComplexityMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final visitor = CyclomaticComplexityFlowVisitor();
node.visitChildren(visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _documentation = MetricDocumentation(
name: 'Halstead Volume',
shortName: 'HALVOL',
measuredType: EntityType.methodEntity,
recomendedThreshold: 150,
recommendedThreshold: 150,
);

/// Halstead Volume (HALVOL)
Expand Down Expand Up @@ -45,7 +45,7 @@ class HalsteadVolumeMetric extends FunctionMetric<double> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final visitor = HalsteadVolumeAstVisitor();
node.visitChildren(visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const _documentation = MetricDocumentation(
name: 'Lines of Code',
shortName: 'LOC',
measuredType: EntityType.methodEntity,
recomendedThreshold: 100,
recommendedThreshold: 100,
);

/// Lines of Code (LOC)
Expand All @@ -39,7 +39,7 @@ class LinesOfCodeMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) =>
MetricComputationResult(
value: 1 +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const _documentation = MetricDocumentation(
name: 'Maintainability Index',
shortName: 'MI',
measuredType: EntityType.methodEntity,
recomendedThreshold: 50,
recommendedThreshold: 50,
);

/// Maintainability Index (MI)
Expand All @@ -43,7 +43,7 @@ class MaintainabilityIndexMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<Object>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) =>
super.supports(
node,
Expand All @@ -65,7 +65,7 @@ class MaintainabilityIndexMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final halVol = otherMetricsValues.firstWhere(
(value) => value.metricsId == HalsteadVolumeMetric.metricId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const _documentation = MetricDocumentation(
name: 'Maximum Nesting Level',
shortName: 'MAXNESTING',
measuredType: EntityType.methodEntity,
recomendedThreshold: 5,
recommendedThreshold: 5,
);

/// Maximum Nesting Level (MAXNESTING)
Expand All @@ -44,7 +44,7 @@ class MaximumNestingLevelMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final visitor = NestingLevelVisitor(node);
node.visitChildren(visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _documentation = MetricDocumentation(
name: 'Number of Methods',
shortName: 'NOM',
measuredType: EntityType.classEntity,
recomendedThreshold: 10,
recommendedThreshold: 10,
);

/// Number of Methods (NOM)
Expand All @@ -41,7 +41,7 @@ class NumberOfMethodsMetric extends ClassMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final methods = classMethods(node, functionDeclarations);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const _documentation = MetricDocumentation(
name: 'Number of Parameters',
shortName: 'NOP',
measuredType: EntityType.methodEntity,
recomendedThreshold: 4,
recommendedThreshold: 4,
);

/// Number of Parameters (NOP)
Expand All @@ -39,7 +39,7 @@ class NumberOfParametersMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<Object>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
if (node is FunctionDeclaration) {
return true;
Expand All @@ -64,7 +64,7 @@ class NumberOfParametersMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
int? parametersCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _documentation = MetricDocumentation(
name: 'Source lines of Code',
shortName: 'SLOC',
measuredType: EntityType.methodEntity,
recomendedThreshold: 50,
recommendedThreshold: 50,
);

/// Source lines of Code (SLOC)
Expand All @@ -44,7 +44,7 @@ class SourceLinesOfCodeMetric extends FunctionMetric<int> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final visitor = SourceCodeVisitor(source.lineInfo);
node.visitChildren(visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const _documentation = MetricDocumentation(
name: 'Technical Debt',
shortName: 'TECHDEBT',
measuredType: EntityType.fileEntity,
recomendedThreshold: 0,
recommendedThreshold: 0,
);

/// Technical Debt (TECHDEBT)
Expand Down Expand Up @@ -77,7 +77,7 @@ class TechnicalDebtMetric extends FileMetric<double> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
var debt = 0.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const _documentation = MetricDocumentation(
name: 'Weight Of a Class',
shortName: 'WOC',
measuredType: EntityType.classEntity,
recomendedThreshold: 0.33,
recommendedThreshold: 0.33,
);

/// Weight Of a Class (WOC)
Expand All @@ -42,7 +42,7 @@ class WeightOfClassMetric extends ClassMetric<double> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<Object>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) =>
super.supports(
node,
Expand All @@ -61,7 +61,7 @@ class WeightOfClassMetric extends ClassMetric<double> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final totalPublicMethods = classMethods(node, functionDeclarations)
.where(_isPublicMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class FileMetric<T extends num> extends Metric<T> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<Object>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) =>
node is CompilationUnit;

Expand Down
6 changes: 3 additions & 3 deletions lib/src/analyzers/lint_analyzer/metrics/models/metric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class Metric<T extends num> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<Object>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) =>
true;

Expand All @@ -49,7 +49,7 @@ abstract class Metric<T extends num> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
) {
final result = computeImplementation(
node,
Expand Down Expand Up @@ -80,7 +80,7 @@ abstract class Metric<T extends num> {
Iterable<ScopedClassDeclaration> classDeclarations,
Iterable<ScopedFunctionDeclaration> functionDeclarations,
InternalResolvedUnitResult source,
Iterable<MetricValue<num>> otherMetricsValues,
Iterable<MetricValue> otherMetricsValues,
);

/// Returns the message for the user containing information about the computed value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class MetricDocumentation {
/// The type of entities which will be measured by the metric.
final EntityType measuredType;

/// The recomended threshold value for this metric
final num recomendedThreshold;
/// The recommended threshold value for this metric
final num recommendedThreshold;

/// Initialize a newly created [MetricDocumentation].
///
Expand All @@ -21,6 +21,6 @@ class MetricDocumentation {
required this.name,
required this.shortName,
required this.measuredType,
required this.recomendedThreshold,
required this.recommendedThreshold,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'metric_documentation.dart';
import 'metric_value_level.dart';

/// Represents a value computed by the metric.
class MetricValue<T> {
class MetricValue<T extends num> {
/// The id of the computed metric.
final String metricsId;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/analyzers/lint_analyzer/models/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Report {
final AstNode declaration;

/// Target entity metrics.
final Iterable<MetricValue<num>> metrics;
final Iterable<MetricValue> metrics;

/// Returns a certain target metric.
MetricValue<num>? metric(String id) =>
MetricValue? metric(String id) =>
metrics.firstWhereOrNull((metric) => metric.metricsId == id);

/// The highest reported level of a metric.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'summary_lint_report_record_status.dart';

/// Represents a summary for a lint report.
class SummaryLintReportRecord<T> {
class SummaryLintReportRecord<T extends Object> {
final SummaryLintReportRecordStatus status;

final String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ class LintConsoleReporter extends ConsoleReporter<LintFileReport,
return [];
}

bool _isNeedToReport(MetricValue<Object> metric) =>
bool _isNeedToReport(MetricValue metric) =>
metric.level > MetricValueLevel.none;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../../../models/issue.dart';
import '../../../models/severity.dart';

final _alarmPen = AnsiPen()..rgb(r: 0.88, g: 0.32, b: 0.36);
final _warnigPen = AnsiPen()..rgb(r: 0.98, g: 0.68, b: 0.4);
final _warningPen = AnsiPen()..rgb(r: 0.98, g: 0.68, b: 0.4);
final _bluePen = AnsiPen()..rgb(r: 0.08, g: 0.11, b: 0.81);
final _whitePen = AnsiPen()..white();

Expand All @@ -16,14 +16,14 @@ final _linkPen = AnsiPen()..rgb(r: 0.0, g: 0.78, b: 1.0);
class LintConsoleReporterHelper {
static final _colorPens = {
MetricValueLevel.alarm: _alarmPen,
MetricValueLevel.warning: _warnigPen,
MetricValueLevel.warning: _warningPen,
MetricValueLevel.noted: _bluePen,
MetricValueLevel.none: _whitePen,
};

final _severityPens = {
Severity.error: _alarmPen,
Severity.warning: _warnigPen,
Severity.warning: _warningPen,
Severity.performance: _bluePen,
Severity.style: _bluePen,
Severity.none: _whitePen,
Expand Down Expand Up @@ -75,7 +75,7 @@ class LintConsoleReporterHelper {
}

/// Converts a [metric] to the metric message string.
String getMetricReport(MetricValue<num> metric) {
String getMetricReport(MetricValue metric) {
final color = _colorPens[metric.level];

if (color != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Element renderDetailsTooltip(Report entityReport, String entityType) {
return tooltip;
}

Element renderDetailsTooltipMetric(MetricValue<num> metric) {
Element renderDetailsTooltipMetric(MetricValue metric) {
final metricName = metric.documentation.name.toLowerCase();
final violationLevel = metric.level.toString();

Expand Down
Loading