Skip to content

Commit 73b3712

Browse files
committed
[api change] Expose the 'analyse' function
1 parent e95c82d commit 73b3712

File tree

8 files changed

+92
-89
lines changed

8 files changed

+92
-89
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Command-line tool and module to generate a complexity report on file tree Javasc
1616
var jscomplexity = require('jscomplexity');
1717

1818
// jscomplexity() returns a promise (using bluebird)
19-
jscomplexity('/path/to/js/dir').then(console.log);
19+
jscomplexity('/glob/pattern/to/js/*' [, globOptions]).then(console.log);
2020

2121
// you can also use CPS style
22-
jscomplexity('/path/to/js/dir', function(err, result){
22+
jscomplexity('/glob/pattern/to/js/*', {}, function(err, result){
2323
if(err) {
2424
return console.log(err);
2525
}

index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
module.exports = function(srcDir, skippedDirectories, isVerbose, callback){
1+
var scan = require('./src/scan');
2+
var analyse = require('./src/analyse');
23

3-
var scanner = require('./src/scan');
4-
return scanner(srcDir, skippedDirectories, isVerbose).nodeify(callback);
5-
6-
}
4+
module.exports = scan;
5+
module.exports.analyse = analyse;

sample.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ jscomplexity('./src/**').then(function(data){
1010
}).caught(console.log);
1111

1212

13-
/**
14-
* Node-style version :
15-
*
16-
* jscomplexity('./src/**', function(err, data){
17-
*
18-
* if(err){
19-
* return console.log(err);
20-
* }
21-
*
22-
* console.log(JSON.stringify(data.report, null, 6));
23-
*
24-
* });
25-
*/
13+
// Node-style version
14+
15+
// jscomplexity('./src/**', {}, function(err, data){
16+
17+
// if(err){
18+
// return console.log(err);
19+
// }
20+
21+
// console.log(JSON.stringify(data.report, null, 6));
22+
23+
// });

src/analyse.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*jshint -W079 */
2+
var Promise = require('bluebird'),
3+
escomplex = require('escomplex'),
4+
treeWalker = require('escomplex-ast-moz'),
5+
esprima = require('esprima'),
6+
fs = require('fs'),
7+
readFileAsync = Promise.promisify(fs.readFile, fs);
8+
9+
/**
10+
* Promise of a complexity analysis of a single file
11+
*
12+
* @param {String} 'fileRef' The file path
13+
* @returns {Promise} The fulfilled promise returns the report {Object}
14+
*/
15+
16+
module.exports = function analyse (fileRef){
17+
18+
return readFileAsync(fileRef, 'utf8')
19+
20+
.then(function(fileData){
21+
22+
return escomplex.analyse(
23+
esprima.parse(fileData, {loc : true}),
24+
treeWalker
25+
);
26+
27+
})
28+
29+
.then(function(report){
30+
31+
return {
32+
path : fileRef,
33+
escapedPath : fileRef.replace(/\\/g, '\\'), // windows use
34+
complexity : report.aggregate.cyclomatic,
35+
lineNumber : report.aggregate.sloc.logical,
36+
maintainability : report.aggregate.halstead.effort,
37+
halstead : {
38+
length : report.aggregate.halstead.length,
39+
vocabulary : Math.round(report.aggregate.halstead.vocabulary),
40+
difficulty : Math.round(report.aggregate.halstead.difficulty),
41+
bugs : Math.round(report.aggregate.halstead.bugs)
42+
}
43+
};
44+
45+
})
46+
47+
;
48+
49+
};

src/lib/analyse.js

-48
This file was deleted.

src/lib/report.js renamed to src/report.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
/*jshint -W079 */
22
var Promise = require('bluebird'),
3-
_ = require('lodash'),
4-
fs = require('fs'),
5-
readFileAsync = Promise.promisify(fs.readFile, fs);
3+
_ = require('lodash');
64

75
var analyse = require('./analyse');
86

97
/**
108
* Promise of a report on a single file
119
*
1210
* @param {String} 'fileRef' The file's path
13-
* @param {Object} 'fileStats' The informations about current file
1411
* @returns {Promise} The promise shall always be fulfilled
1512
*/
1613

1714
module.exports = function report (fileRef) {
1815

19-
return readFileAsync(fileRef, 'utf8')
16+
return Promise.resolve(fileRef)
2017

21-
.then(analyse(fileRef))
18+
.then(analyse)
2219

2320
.then(function(result){
2421

src/scan.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ var Promise = require('bluebird'),
66
path = require('path')
77
;
88

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

1111
/**
1212
* Scan a file tree seeking for js files and generates a complexity report
1313
*
1414
* @public
15-
* @param {String} 'pattern' The glob pattern
16-
* @param {Array} 'globOptions' The glob options
17-
* @returns {Promise} The fulfilled promise returns the final report
15+
* @param {String} 'pattern' The glob pattern
16+
* @param {Array} 'globOptions' The glob options
17+
* @param {Function} 'callback' cps callback function (optional)
18+
* @returns {Promise} The fulfilled promise returns the final report
1819
*/
1920

20-
module.exports = function (pattern, globOptions){
21+
module.exports = function scan(pattern, globOptions, callback){
2122

2223
return new Promise(function (resolve, reject) {
2324

@@ -69,6 +70,6 @@ module.exports = function (pattern, globOptions){
6970

7071
;
7172

72-
});
73+
}).nodeify(callback);
7374

7475
};

test/index.coffee

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ chai = require('chai')
55
chaiAsPromised = require('chai-as-promised')
66
should = chai.should()
77
Promise = require('bluebird')
8-
scanner = require('../index')
8+
9+
jscomplexity = require('../index')
10+
analyse = require('../index').analyse
911

1012
chai.use chaiAsPromised
1113

@@ -18,23 +20,23 @@ EXPECTED_NO_JS_FILE_REPORT =
1820
fails: []
1921

2022

21-
describe 'the complexity scanner', ->
23+
describe 'the required jscomplexity module', ->
2224

2325
it 'should return a Promise', ->
2426

25-
scanner('/*js').should.be.an.instanceof Promise
27+
jscomplexity('/*js').should.be.an.instanceof Promise
2628

2729
it 'should be rejected if (glob\'s) arguments <String:pattern> and <Object:options> are not valid', ->
2830

29-
scanner().should.be.rejected
30-
scanner([]).should.be.rejected
31-
scanner('').should.be.rejected
32-
scanner('/*.js', []).should.be.rejected
31+
jscomplexity().should.be.rejected
32+
jscomplexity([]).should.be.rejected
33+
jscomplexity('').should.be.rejected
34+
jscomplexity('/*.js', []).should.be.rejected
3335

3436

3537
it 'should return an empty array when targeted folder doesn\'t contain .js files', (done) ->
3638

37-
scanner(TREE_WITHOUT_JS_FILE)
39+
jscomplexity(TREE_WITHOUT_JS_FILE)
3840
.should.be.fulfilled
3941
.and.eventually.deep.equal(EXPECTED_NO_JS_FILE_REPORT)
4042
.and.notify done
@@ -43,11 +45,16 @@ describe 'the complexity scanner', ->
4345
it 'should return the expected complex result', (done) ->
4446

4547
@timeout 10000
46-
scanner(TREE_WITH_COMPLEX_FILES)
48+
jscomplexity(TREE_WITH_COMPLEX_FILES)
4749
.should.be.fulfilled
4850
.and.eventually.deep.equal(EXPECTED_COMPLEX_FILES_REPORT)
4951
.and.notify done
5052

53+
describe 'the analyse function', ->
54+
55+
it 'should be exposed by the module', ->
56+
57+
analyse.should.be.a 'function'
5158

5259

5360

0 commit comments

Comments
 (0)