-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
77 lines (62 loc) · 1.95 KB
/
index.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
/* jshint node:true */
var cr = require('complexity-report'),
through = require('through2'),
gutil = require('gulp-util'),
extend = require('util-extend'),
reporter = require('./reporter'),
PluginError = gutil.PluginError;
function complexity(options){
options = extend({
cyclomatic: [3, 7, 12],
halstead: [8, 13, 20],
maintainability: 100,
breakOnErrors: true,
verbose: true
}, options);
// always making sure threasholds are arrays
if(!Array.isArray(options.cyclomatic)){
options.cyclomatic = [options.cyclomatic];
}
if(!Array.isArray(options.halstead)){
options.halstead = [options.halstead];
}
var files = [];
var errorCount = 0;
return through.obj(function(file, enc, cb){
if(file.isNull()){
return cb(null, file);
}
if(file.isStream()){
return cb(new PluginError('gulp-complexity', 'Streaming not supported'));
}
files.push(file);
cb(null, file);
}, function(cb){
var path = require('path'),
helpers = require('./reporter-helpers');
var maxLength = helpers.longestString(files.map(function(file){
return path.relative(file.cwd, file.path);
}));
files.filter(function(file){
return file.contents.toString().length > 0;
}).forEach(function(file){
var base = path.relative(file.cwd, file.path);
var report = cr.run(file.contents.toString(), options);
var initialErrorCount = errorCount;
errorCount += report.functions.filter(function(data){
return (data.complexity.cyclomatic > options.cyclomatic[0]) || (data.complexity.halstead.difficulty > options.halstead[0]);
}).length;
if (report.maintainability < options.maintainability) {
errorCount++;
}
if (errorCount !== initialErrorCount || options.verbose) {
reporter.log(file, report, options, helpers.fitWhitespace(maxLength, base));
}
});
if(options.breakOnErrors && errorCount > 0) {
this.emit('error', new PluginError('gulp-complexity', 'Complexity too high'));
}
cb();
});
}
module.exports = complexity;