/
complexity.js
139 lines (125 loc) · 7.81 KB
/
complexity.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @fileoverview Tests for complexity rule.
* @author Patrick Brosset
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/complexity"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Generates a code string with the amount of complexity specified in the parameter
* @param {int} complexity The level of complexity
* @returns {string} Code with the amount of complexity specified in the parameter
* @private
*/
function createComplexity(complexity) {
let funcString = "function test (a) { if (a === 1) {";
for (let i = 2; i < complexity; i++) {
funcString += `} else if (a === ${i}) {`;
}
funcString += "} };";
return funcString;
}
/**
* Create an expected error object
* @param {string} name The name of the symbol being tested
* @param {number} complexity The cyclomatic complexity value of the symbol
* @param {number} max The maximum cyclomatic complexity value of the symbol
* @returns {Object} The error object
*/
function makeError(name, complexity, max) {
return {
messageId: "complex",
data: { name, complexity, max }
};
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2021 } });
ruleTester.run("complexity", rule, {
valid: [
"function a(x) {}",
{ code: "function b(x) {}", options: [1] },
{ code: "function a(x) {if (true) {return x;}}", options: [2] },
{ code: "function a(x) {if (true) {return x;} else {return x+1;}}", options: [2] },
{ code: "function a(x) {if (true) {return x;} else if (false) {return x+1;} else {return 4;}}", options: [3] },
{ code: "function a(x) {for(var i = 0; i < 5; i ++) {x ++;} return x;}", options: [2] },
{ code: "function a(obj) {for(var i in obj) {obj[i] = 3;}}", options: [2] },
{ code: "function a(x) {for(var i = 0; i < 5; i ++) {if(i % 2 === 0) {x ++;}} return x;}", options: [3] },
{ code: "function a(obj) {if(obj){ for(var x in obj) {try {x.getThis();} catch (e) {x.getThat();}}} else {return false;}}", options: [4] },
{ code: "function a(x) {try {x.getThis();} catch (e) {x.getThat();}}", options: [2] },
{ code: "function a(x) {return x === 4 ? 3 : 5;}", options: [2] },
{ code: "function a(x) {return x === 4 ? 3 : (x === 3 ? 2 : 1);}", options: [3] },
{ code: "function a(x) {return x || 4;}", options: [2] },
{ code: "function a(x) {x && 4;}", options: [2] },
{ code: "function a(x) {x ?? 4;}", options: [2] },
{ code: "function a(x) {x ||= 4;}", options: [2] },
{ code: "function a(x) {x &&= 4;}", options: [2] },
{ code: "function a(x) {x ??= 4;}", options: [2] },
{ code: "function a(x) {x = 4;}", options: [1] },
{ code: "function a(x) {x |= 4;}", options: [1] },
{ code: "function a(x) {x &= 4;}", options: [1] },
{ code: "function a(x) {x += 4;}", options: [1] },
{ code: "function a(x) {x >>= 4;}", options: [1] },
{ code: "function a(x) {x >>>= 4;}", options: [1] },
{ code: "function a(x) {x == 4;}", options: [1] },
{ code: "function a(x) {x === 4;}", options: [1] },
{ code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: 3;}}", options: [3] },
{ code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: if(x == 'foo') {5;};}}", options: [4] },
{ code: "function a(x) {while(true) {'foo';}}", options: [2] },
{ code: "function a(x) {do {'foo';} while (true)}", options: [2] },
{ code: "if (foo) { bar(); }", options: [3] },
{ code: "var a = (x) => {do {'foo';} while (true)}", options: [2], parserOptions: { ecmaVersion: 6 } },
// object property options
{ code: "function b(x) {}", options: [{ max: 1 }] }
],
invalid: [
{ code: "function a(x) {}", options: [0], errors: [makeError("Function 'a'", 1, 0)] },
{ code: "var func = function () {}", options: [0], errors: [makeError("Function", 1, 0)] },
{ code: "var obj = { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1, 0)] },
{ code: "class Test { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1, 0)] },
{ code: "var a = (x) => {if (true) {return x;}}", options: [1], parserOptions: { ecmaVersion: 6 }, errors: 1 },
{ code: "function a(x) {if (true) {return x;}}", options: [1], errors: 1 },
{ code: "function a(x) {if (true) {return x;} else {return x+1;}}", options: [1], errors: 1 },
{ code: "function a(x) {if (true) {return x;} else if (false) {return x+1;} else {return 4;}}", options: [2], errors: 1 },
{ code: "function a(x) {for(var i = 0; i < 5; i ++) {x ++;} return x;}", options: [1], errors: 1 },
{ code: "function a(obj) {for(var i in obj) {obj[i] = 3;}}", options: [1], errors: 1 },
{ code: "function a(obj) {for(var i of obj) {obj[i] = 3;}}", options: [1], parserOptions: { ecmaVersion: 6 }, errors: 1 },
{ code: "function a(x) {for(var i = 0; i < 5; i ++) {if(i % 2 === 0) {x ++;}} return x;}", options: [2], errors: 1 },
{ code: "function a(obj) {if(obj){ for(var x in obj) {try {x.getThis();} catch (e) {x.getThat();}}} else {return false;}}", options: [3], errors: 1 },
{ code: "function a(x) {try {x.getThis();} catch (e) {x.getThat();}}", options: [1], errors: 1 },
{ code: "function a(x) {return x === 4 ? 3 : 5;}", options: [1], errors: 1 },
{ code: "function a(x) {return x === 4 ? 3 : (x === 3 ? 2 : 1);}", options: [2], errors: 1 },
{ code: "function a(x) {return x || 4;}", options: [1], errors: 1 },
{ code: "function a(x) {x && 4;}", options: [1], errors: 1 },
{ code: "function a(x) {x ?? 4;}", options: [1], errors: 1 },
{ code: "function a(x) {x ||= 4;}", options: [1], errors: 1 },
{ code: "function a(x) {x &&= 4;}", options: [1], errors: 1 },
{ code: "function a(x) {x ??= 4;}", options: [1], errors: 1 },
{ code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: 3;}}", options: [2], errors: 1 },
{ code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: if(x == 'foo') {5;};}}", options: [3], errors: 1 },
{ code: "function a(x) {while(true) {'foo';}}", options: [1], errors: 1 },
{ code: "function a(x) {do {'foo';} while (true)}", options: [1], errors: 1 },
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {while(true){'bar';}})();}", options: [1], errors: 2 },
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {'bar';})();}", options: [1], errors: 1 },
{ code: "var obj = { a(x) { return x ? 0 : 1; } };", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 2, 1)] },
{ code: "var obj = { a: function b(x) { return x ? 0 : 1; } };", options: [1], errors: [makeError("Method 'a'", 2, 1)] },
{
code: createComplexity(21),
errors: [makeError("Function 'test'", 21, 20)]
},
{
code: createComplexity(21),
options: [{}],
errors: [makeError("Function 'test'", 21, 20)]
},
// object property options
{ code: "function a(x) {}", options: [{ max: 0 }], errors: [makeError("Function 'a'", 1, 0)] }
]
});