This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
code_climate_issue.dart
125 lines (100 loc) · 4.17 KB
/
code_climate_issue.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:dart_code_metrics/src/models/code_issue.dart';
import 'package:dart_code_metrics/src/models/code_issue_severity.dart';
import 'package:meta/meta.dart';
import '../../models/design_issue.dart';
@immutable
class CodeClimateLocationLines {
final int begin;
final int end;
const CodeClimateLocationLines(this.begin, this.end);
Map<String, int> toJson() => {
'begin': begin,
'end': end,
};
}
@immutable
class CodeClimateLocation {
final String path;
final CodeClimateLocationLines lines;
const CodeClimateLocation(this.path, this.lines);
Map<String, Object> toJson() => {
'path': path,
'lines': lines.toJson(),
};
}
@immutable
class CodeClimateIssue {
static const String type = 'issue';
static const int remediationPoints = 50000;
final String checkName;
final String description;
final Iterable<String> categories;
final CodeClimateLocation location;
final String fingerprint;
const CodeClimateIssue._(this.checkName, this.description, this.categories,
this.location, this.fingerprint);
factory CodeClimateIssue._create(
String name, String desc, int startLine, int endLine, String fileName,
{Iterable<String> categories = const ['Complexity']}) {
final locationLines = CodeClimateLocationLines(startLine, endLine);
final location = CodeClimateLocation(fileName, locationLines);
final fingerprint = md5
.convert(utf8.encode('$name $desc $startLine $endLine $fileName'))
.toString();
return CodeClimateIssue._(name, desc, categories, location, fingerprint);
}
factory CodeClimateIssue.cyclomaticComplexity(int startLine, int endLine,
int value, String fileName, String functionName, int threshold) {
final desc =
'Function `$functionName` has a Cyclomatic Complexity of $value (exceeds $threshold allowed). Consider refactoring.';
return CodeClimateIssue._create(
'cyclomaticComplexity', desc, startLine, endLine, fileName);
}
factory CodeClimateIssue.maintainabilityIndex(int startLine, int endLine,
int value, String fileName, String functionName) {
final desc =
'Function `$functionName` has a Maintainability Index of $value (min 40 allowed). Consider refactoring.';
return CodeClimateIssue._create(
'maintainabilityIndex', desc, startLine, endLine, fileName);
}
factory CodeClimateIssue.numberOfArguments(int startLine, int endLine,
int value, String fileName, String functionName, int threshold) {
final desc =
'Function `$functionName` has $value number of arguments (exceeds $threshold allowed). Consider refactoring.';
return CodeClimateIssue._create(
'numberOfArguments', desc, startLine, endLine, fileName);
}
factory CodeClimateIssue.numberOfMethods(int startLine, int endLine,
int value, String fileName, String componentName, int threshold) {
final desc =
'Component `$componentName` has $value number of methods (exceeds $threshold allowed). Consider refactoring.';
return CodeClimateIssue._create(
'numberOfMethods', desc, startLine, endLine, fileName);
}
factory CodeClimateIssue.fromCodeIssue(CodeIssue issue, String fileName) {
const severityHumanReadable = {
CodeIssueSeverity.style: ['Style'],
CodeIssueSeverity.warning: ['Clarity'],
CodeIssueSeverity.error: ['Bug Risk'],
};
return CodeClimateIssue._create(issue.ruleId, issue.message,
issue.sourceSpan.start.line, issue.sourceSpan.start.line, fileName,
categories: severityHumanReadable[issue.severity]);
}
factory CodeClimateIssue.fromDesignIssue(
DesignIssue issue, String fileName) =>
CodeClimateIssue._create(issue.patternId, issue.message,
issue.sourceSpan.start.line, issue.sourceSpan.start.line, fileName,
categories: const ['Complexity']);
Map<String, Object> toJson() => {
'type': type,
'check_name': checkName,
'description': description,
'categories': categories,
'location': location.toJson(),
'remediation_points': remediationPoints,
'fingerprint': fingerprint,
};
}