grunt-complexity
Advanced tools
Comparing version 1.0.0-rc to 1.0.0-rc2
@@ -7,3 +7,3 @@ /*global module:false*/ | ||
files: ['Gruntfile.js', 'tasks/**/*.js', 'test/*.js'], | ||
files: ['Gruntfile.js', 'tasks/**/*.js', 'test/**/*.js'], | ||
@@ -17,3 +17,6 @@ watch: { | ||
jshint: { | ||
all: '<%= files %>' | ||
all: '<%= files %>', | ||
options: { | ||
esnext: true | ||
} | ||
}, | ||
@@ -20,0 +23,0 @@ |
{ | ||
"name": "grunt-complexity", | ||
"description": "Evaluates code maintainability using Halstead and Cyclomatic metrics.", | ||
"version": "1.0.0-rc", | ||
"version": "1.0.0-rc2", | ||
"homepage": "https://github.com/vigetlabs/grunt-complexity.git", | ||
@@ -27,2 +27,3 @@ "author": { | ||
"grunt": ">=0.4.x", | ||
"lodash": "^4.15.0", | ||
"typhonjs-escomplex": "0.0.12" | ||
@@ -29,0 +30,0 @@ }, |
/*global module:false*/ | ||
var escomplex = require('typhonjs-escomplex'); | ||
var _ = require('lodash'); | ||
@@ -109,50 +110,55 @@ module.exports = function(grunt) { | ||
reportComplexity: function(reporter, analysis, filepath, options) { | ||
var complicatedFunctions = []; | ||
var classFunctionsReports = this.getFunctionsFromClasses(analysis.classes); | ||
var methodFunctionsReports = analysis.methods; | ||
var complicatedFunctions = []; | ||
if (options.hideComplexFunctions !== true) { | ||
complicatedFunctions = analysis.methods.filter(function(data) { | ||
return this.isComplicated(data, options); | ||
}, this).map(function(data) { | ||
return this.assignSeverity(data, options); | ||
}, this); | ||
var allFunctionReports = methodFunctionsReports.concat(classFunctionsReports); | ||
complicatedFunctions = this.getComplicatedFunctions(allFunctionReports, options); | ||
} | ||
grunt.fail.errorcount += complicatedFunctions.length; | ||
reporter.complexity(filepath, complicatedFunctions); | ||
}, | ||
getFunctionsFromClasses: function(classes) { | ||
return _.flatMap(classes, 'methods'); | ||
}, | ||
getComplicatedFunctions: function(methodList, options) { | ||
return methodList.filter((data) => this.isComplicated(data, options)).map((data) => this.assignSeverity(data, options)); | ||
}, | ||
reportMaintainability: function(reporter, analysis, filepath, options) { | ||
var valid = this.isMaintainable(analysis, options); | ||
if (!options.errorsOnly || !valid) { | ||
reporter.maintainability(filepath, valid, analysis); | ||
} | ||
if (!valid) { | ||
grunt.fail.errorcount++; | ||
} | ||
}, | ||
analyze: function(reporter, files, options) { | ||
reporter.start(); | ||
files.map(function(filepath) { | ||
getProjectInfos: function(files) { | ||
return files.map((filepath) => { | ||
var content = grunt.file.read(filepath); | ||
if (!content.length) { | ||
throw new Error('Empty source file: \'' + filepath + '\'.'); | ||
} else { | ||
return { srcPath: filepath, code: content }; | ||
} | ||
}); | ||
}, | ||
if (!content.length) { | ||
throw new Error('Empty source file: \'' + filepath + '\'.'); | ||
} | ||
return { | ||
filepath: filepath, | ||
analysis: escomplex.analyzeModule(content, options) | ||
}; | ||
}).sort(function (info1, info2) { | ||
return info1.analysis.maintainability - info2.analysis.maintainability; | ||
}).forEach(function (info) { | ||
displayReports: function(reporter, fileReports, options) { | ||
fileReports = _.orderBy(fileReports,'analysis.maintainability', 'asc'); | ||
grunt.fail.errorcount = _.countBy(fileReports, (report) => this.isMaintainable(report, options)); | ||
fileReports.forEach((info) => { | ||
this.reportMaintainability(reporter, info.analysis, info.filepath, options); | ||
this.reportComplexity(reporter, info.analysis, info.filepath, options); | ||
}, this); | ||
}); | ||
}, | ||
analyze: function(reporter, files, options) { | ||
reporter.start(); | ||
var analyzedProject = escomplex.analyzeProject(this.getProjectInfos(files), options); | ||
var fileReports = analyzedProject.modules.map((moduleReport) => ({ filepath: moduleReport.srcPath, analysis: moduleReport })); | ||
this.displayReports(reporter, fileReports, options); | ||
reporter.finish(); | ||
@@ -159,0 +165,0 @@ } |
@@ -29,2 +29,34 @@ describe("Complexity task", function() { | ||
it ('parses es6', function () { | ||
var reporter = { | ||
start: function () {}, | ||
maintainability: function () {}, | ||
complexity: function () {}, | ||
finish: function () {} | ||
}; | ||
var options = cut.normalizeOptions({ | ||
cyclomatic: 3, | ||
halstead: 8 | ||
}); | ||
cut.analyze(reporter, [__dirname + '/fixtures/es6.js'], options); | ||
}); | ||
it ('parses jsx', function () { | ||
var reporter = { | ||
start: function () {}, | ||
maintainability: function () {}, | ||
complexity: function () {}, | ||
finish: function () {} | ||
}; | ||
var options = cut.normalizeOptions({ | ||
cyclomatic: 3, | ||
halstead: 8 | ||
}); | ||
cut.analyze(reporter, [__dirname + '/fixtures/react.jsx'], options); | ||
}); | ||
describe("isComplicated", function() { | ||
@@ -31,0 +63,0 @@ var data = { |
@@ -10,14 +10,14 @@ describe('Event Reporter', function() { | ||
var targetFile = __dirname + '/fixtures/sample.js'; | ||
var reporter = Complexity.buildReporter([targetFile], { broadcast: true }) | ||
var reporter = Complexity.buildReporter([targetFile], { broadcast: true }); | ||
grunt.event.on('grunt-complexity.maintainability', function(report) { | ||
expect(report.filepath).to.equal(targetFile) | ||
expect(report.valid).to.equal(true) | ||
done() | ||
}) | ||
expect(report.filepath).to.equal(targetFile); | ||
expect(report.valid).to.equal(true); | ||
done(); | ||
}); | ||
Complexity.analyze(reporter, [targetFile], Complexity.normalizeOptions({ | ||
maintainability: 0 | ||
})) | ||
})); | ||
}); | ||
}); |
@@ -1,7 +0,33 @@ | ||
import {EventEmitter} from 'events' | ||
import {EventEmitter} from 'events'; | ||
class MyEmitter extends EventEmitter {} | ||
const myEmitter = new MyEmitter | ||
const myEmitter = new MyEmitter(); | ||
export default myEmitter | ||
export default myEmitter; | ||
export class Sample { | ||
constructor() { | ||
} | ||
isFunctionComplex() { | ||
if (true) { | ||
if (true) { | ||
if (true) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
function isFunctionComplex() { | ||
if (true) { | ||
if (true) { | ||
if (true) { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
@@ -5,6 +5,6 @@ module.exports = function() { | ||
if (true) { | ||
return false | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
178520
39
636
4
+ Addedlodash@^4.15.0