Skip to content

Commit 2ebd03a

Browse files
committed
[refactoring] Simplify and rename modules
1 parent e72e24f commit 2ebd03a

File tree

5 files changed

+96
-111
lines changed

5 files changed

+96
-111
lines changed

src/lib/analyse.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*jshint -W079 */
2+
var Promise = require('bluebird'),
3+
cr = require('escomplex'),
4+
treeWalker = require('escomplex-ast-moz'),
5+
esprima = require('esprima');
6+
7+
/**
8+
* Builds final complexity report of
9+
* a given file from its reference and content
10+
*
11+
* @param {String} 'fileRef' The file path
12+
* @param {String} 'fileData' The file content stringified
13+
* @returns {Promise} The fulfilled promise returns the report {Object}
14+
*/
15+
16+
module.exports = function analyse (fileRef){
17+
18+
return function(fileData){
19+
20+
return new Promise(function(resolve, reject){
21+
22+
var report;
23+
24+
try {
25+
report = cr.analyse( esprima.parse(fileData, {loc : true}), treeWalker);
26+
} catch(e){
27+
reject('parsing error');
28+
}
29+
30+
resolve({
31+
path : fileRef,
32+
escapedPath : fileRef.replace(/\\/g, '\\'), // windows use
33+
complexity : report.aggregate.cyclomatic,
34+
lineNumber : report.aggregate.sloc.logical,
35+
maintainability : report.aggregate.halstead.effort,
36+
halstead : {
37+
length : report.aggregate.halstead.length,
38+
vocabulary : Math.round(report.aggregate.halstead.vocabulary),
39+
difficulty : Math.round(report.aggregate.halstead.difficulty),
40+
bugs : Math.round(report.aggregate.halstead.bugs)
41+
}
42+
});
43+
44+
});
45+
46+
};
47+
48+
};

src/lib/analyser.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/lib/createFileReport.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/lib/report.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*jshint -W079 */
2+
var Promise = require('bluebird'),
3+
_ = require('lodash'),
4+
fs = require('fs'),
5+
readFileAsync = Promise.promisify(fs.readFile, fs);
6+
7+
var analyse = require('./analyse');
8+
9+
/**
10+
* Promise of a report on a single file
11+
*
12+
* @param {String} 'fileRef' The file's path
13+
* @param {Object} 'fileStats' The informations about current file
14+
* @returns {Promise} The promise shall always be fulfilled
15+
*/
16+
17+
module.exports = function report (fileRef, isVerbose) {
18+
19+
return readFileAsync(fileRef, 'utf8')
20+
21+
.then(analyse(fileRef))
22+
23+
.then(function(result){
24+
25+
if (this.isVerbose) {
26+
console.log('%s | %s', result.complexity, fileRef);
27+
}
28+
29+
return {
30+
success : true,
31+
result : result
32+
};
33+
34+
}.bind(this))
35+
36+
.caught(function(err){
37+
38+
return {
39+
success : false,
40+
error : err
41+
};
42+
43+
});
44+
45+
};

src/scan.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var Promise = require('bluebird'),
66
path = require('path')
77
;
88

9-
var Analyser = require('./lib/analyser');
9+
var reportAsync = require('./lib/report');
1010

1111
/**
1212
* Scan a file tree seeking for js files and generates a complexity report
@@ -34,11 +34,10 @@ module.exports = function (pattern, globOptions, isVerbose){
3434

3535
.then(function (files) {
3636

37-
var analyser = new Analyser(isVerbose ? isVerbose : false),
38-
filesReports = [];
37+
var filesReports = [];
3938

4039
_.each(files, function(file){
41-
filesReports.push(analyser.analyse(file));
40+
filesReports.push(reportAsync(file, isVerbose ? isVerbose : false));
4241
});
4342

4443
Promise.all(filesReports)

0 commit comments

Comments
 (0)