8000 categorize max-statements as Complexity, fix point calc · codeclimate/codeclimate-eslint@52dbe0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 52dbe0f

Browse files
author
Will Fleming
committed
categorize max-statements as Complexity, fix point calc
* max-statements rule should go in the Complexity Category * Calculating remediation points for rules in that category is now more complicated, because we have to use regexs to extract the values * Adjusted the overage points here to match RuboCop, since we're trying to standardize around that for these types of checks.
1 parent 61be7d5 commit 52dbe0f

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

lib/checks.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var checkCategoryMapping = {
1414
"guard-for-in": "Bug Risk",
1515
"handle-callback-err": "Bug Risk",
1616
"init-declarations": "Clarity",
17+
"max-statements": "Complexity",
1718
"no-alert": "Bug Risk",
1819
"no-caller": "Compatibility",
1920
"no-case-declarations": "Bug Risk",
@@ -113,21 +114,44 @@ var categories = function(checkName) {
113114
return [checkCategoryMapping[checkName] || "Style"];
114115
};
115116

117+
// Here Be Dragons: this function extracts the relevant value that triggered the issue for
118+
// checks in the Complexity category. Unfortunately, these values are not available in a
119+
// structured way, so we extract them from strings. That means that any check categorized
120+
// as Complexity MUST have a rule here to extract value.
121+
//
122+
// If this function fails to find a matching string segment, it will return `undefined`.
123+
var messageMetricValue = function(message) {
124+
var match = null;
125+
switch (message.ruleId) {
126+
case "complexity":
127+
match = message.message(/complexity of (\d+)/);
128+
break;
129+
case "max-statements":
130+
match = message.message("/too many statements \((\d+)\)/");
131+
break;
132+
}
133+
if (null !== match) {
134+
return parseInt(match[1], 10);
135+
}
136+
return null;
137+
};
138+
116139
var remediationPoints = function(checkName, message) {
117-
if (checkName === "complexity") {
140+
if (categories(checkName) === ["Complexity"]) {
118141
// (@base_cost + (overage * @cost_per))*1_000_000
119142
// cost_per: 0.1, base: 1
120-
var costPer = 100000
143+
var costPer = 70000
121144
, points = 1000000
122145
, threshold = 10 // TODO: Get this from the eslintrc
123146
, overage
124-
, complexity;
125-
126-
complexity = message.message.match(/complexity of (\d+)/)[1];
127-
overage = complexity - threshold;
147+
, metricValue;
128148

129-
if (overage > 0) {
130-
points += (costPer * overage);
149+
metricValue = messageMetricValue(message);
150+
if (null !== metricValue) {
151+
overage = metricValue - threshold;
152+
if (overage > 0) {
153+
points += (costPer * overage);
154+
}
131155
}
132156

133157
return points;

0 commit comments

Comments
 (0)
0