complexity-report
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "complexity-report", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"author": "Phil Booth <pmbooth@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "A tool for reporting code complexity metrics in JavaScript projects.", |
@@ -20,2 +20,10 @@ /*globals exports, require */ | ||
LogicalExpression: processLogical, | ||
SwitchStatement: processSwitch, | ||
SwitchCase: processCase, | ||
ForStatement: processLoop, | ||
ForInStatement: processForIn, | ||
WhileStatement: processLoop, | ||
DoWhileStatement: processLoop, | ||
TryStatement: processTry, | ||
CatchClause: processCatch, | ||
FunctionDeclaration: processFunction, | ||
@@ -109,2 +117,40 @@ FunctionExpression: processFunction, | ||
function processSwitch (s, report, customReport) { | ||
processTree(s.cases, report, customReport); | ||
} | ||
function processCase (c, report, customReport) { | ||
if (c.test) { | ||
processCondition({ | ||
consequent: { | ||
type: 'BlockStatement', | ||
body: c.consequent | ||
} | ||
}, report, customReport); | ||
} else { | ||
processTree(c.consequent, report, customReport); | ||
} | ||
} | ||
function processLoop (loop, report, customReport) { | ||
if (loop.test) { | ||
processCondition({ | ||
consequent: loop.body | ||
}, report, customReport); | ||
} | ||
} | ||
function processForIn (forIn, report, customReport) { | ||
processNode(forIn.body, report, customReport); | ||
} | ||
function processTry (t, report, customReport) { | ||
processNode(t.block, report, customReport); | ||
processTree(t.handlers, report, customReport); | ||
} | ||
function processCatch (c, report, customReport) { | ||
processNode(c.body, report, customReport); | ||
} | ||
function processFunction (fn, report, currentReport) { | ||
@@ -133,9 +179,11 @@ var name; | ||
function processVariable (variable, report, currentReport) { | ||
if ( | ||
variable.init.type === 'FunctionExpression' && | ||
check.isObject(variable.init.id) === false | ||
) { | ||
processFunctionBody(variable.id.name, variable.init.body, report); | ||
} else { | ||
processNode(variable.init, report, currentReport); | ||
if (variable.init) { | ||
if ( | ||
variable.init.type === 'FunctionExpression' && | ||
check.isObject(variable.init.id) === false | ||
) { | ||
processFunctionBody(variable.id.name, variable.init.body, report); | ||
} else { | ||
processNode(variable.init, report, currentReport); | ||
} | ||
} | ||
@@ -142,0 +190,0 @@ } |
@@ -180,2 +180,278 @@ /*global require, suite, setup, teardown, test */ | ||
suite('run against switch statement:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('switch (Date.now()) { case 1: "foo"; break; case 2: "bar"; break; default: "baz"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against switch statement with fall-through case:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('switch (Date.now()) { case 1: case 2: "foo"; break; default: "bar"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against switch statement containing condition:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('switch (Date.now()) { case 1: "foo"; break; case 2: "bar"; break; default: if (true) { "baz"; } }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 4); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against switch statement:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('switch (Date.now()) { case 1: "foo"; break; case 2: "bar"; break; default: "baz"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against for loop:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('var i; for (i = 0; i < 10; i += 1) { "foo"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against for loop containing condition:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('var i; for (i = 0; i < 10; i += 1) { if (true) { "foo"; } }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
}); | ||
suite('run against for...in loop:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('var property; for (property in { foo: "bar", baz: "qux" }) { "wibble"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 1); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against for...in loop containing condition:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('var property, object = { foo: "bar", baz: "qux" }; for (property in object) { if (object.hasOwnProperty(property)) { "wibble"; } }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
}); | ||
suite('run against while loop:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('while (true) { "foo"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against while loop containing condition:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('while (true) { if (true) { "foo"; } }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
}); | ||
suite('run against do...while loop:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('do { "foo"; } while (true)'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against do...while loop containing condition:', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('do { if (true) { "foo"; } } while (true)'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 3); | ||
}); | ||
}); | ||
suite('run against try...catch', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('try { "foo"; } catch (e) { e.message; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 1); | ||
}); | ||
test('functions is empty', function () { | ||
assert.lengthOf(report.functions, 0); | ||
}); | ||
}); | ||
suite('run against try containing condition', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('try { if (true) { "foo"; } } catch (e) { "bar"; }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
}); | ||
suite('run against catch containing condition', function () { | ||
var report; | ||
setup(function () { | ||
report = cr.run('try { "foo"; } catch (e) { if (true) { "bar"; } }'); | ||
}); | ||
teardown(function () { | ||
report = undefined; | ||
}); | ||
test('aggregate has correct cyclomatic complexity', function () { | ||
assert.strictEqual(report.aggregate.complexity.cyclomatic, 2); | ||
}); | ||
}); | ||
suite('run against function declaration:', function () { | ||
@@ -237,3 +513,3 @@ var report; | ||
suite('run against function declaration containing conditional:', function () { | ||
suite('run against function declaration containing condition:', function () { | ||
var report; | ||
@@ -278,3 +554,3 @@ | ||
suite('run against ternary condtional expression assigned to variable', function () { | ||
suite('run against ternary condtional expression assigned to variable:', function () { | ||
var report; | ||
@@ -295,3 +571,3 @@ | ||
suite('run against logical or expression assigned to variable', function () { | ||
suite('run against logical or expression assigned to variable:', function () { | ||
var report; | ||
@@ -348,3 +624,3 @@ | ||
suite('run against ternary condtional expression returned from function', function () { | ||
suite('run against ternary condtional expression returned from function:', function () { | ||
var report; | ||
@@ -369,3 +645,3 @@ | ||
suite('run against logical or expression returned from function', function () { | ||
suite('run against logical or expression returned from function:', function () { | ||
var report; | ||
@@ -430,3 +706,3 @@ | ||
suite('run against ternary condtional expression passed as argument', function () { | ||
suite('run against ternary condtional expression passed as argument:', function () { | ||
var report; | ||
@@ -447,3 +723,3 @@ | ||
suite('run against logical or expression passed as argument', function () { | ||
suite('run against logical or expression passed as argument:', function () { | ||
var report; | ||
@@ -450,0 +726,0 @@ |
38459
858