Skip to content

Commit

Permalink
Add some documentation about onParseFile and onAddModule options.
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed May 25, 2014
1 parent daf3b25 commit dfa9c2b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
14 changes: 7 additions & 7 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"sub" : true,
"strict" : false,
"white" : true,
"smarttabs": true,
"expr": true,
"maxparams": 5,
"maxdepth": 5,
"maxstatements": 25,
"maxcomplexity": 10,
"predef": [
"smarttabs" : true,
"expr" : true,
"maxparams" : 5,
"maxdepth" : 5,
"maxstatements" : 25,
"maxcomplexity" : 10,
"predef" : [
"define",
"describe",
"it"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Only required if you want to generate the visual graphs using [Graphviz](http://
- {Boolean} **breakOnError**. True if the parser should stop on parse errors and when modules are missing, false otherwise. Defaults to false.
- {Boolean} **optimized**. True if the parser should read modules from a optimized file (r.js). Defaults to false.
- {String} **requireConfig**. Path to RequireJS config used to find shim dependencies. Not used by default.
- {Function} **onParseFile**. Function to be called when parsing a file (argument will be an object with "filename" and "src" property set).
- {Function} **onAddModule** . Function to be called when adding a module to the module tree (argument will be an object with "id" and "dependencies" property set).

## dependency object (returned from madge)

Expand Down Expand Up @@ -223,6 +225,9 @@ minimize a global energy function, which is equivalent to statistical multi-dime

# Release Notes

## v0.3.0 (May 25, 2014)
Added support for onParseFile and onAddModule options (Thanks to Brandon Selway).

## v0.2.0 (April 17, 2014)
Added support for including shim dependencies found in RequiredJS config (specify with option -R).

Expand Down
8 changes: 5 additions & 3 deletions lib/parse/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ var fs = require('fs'),
* @constructor
* @param {Array} src
* @param {Object} opts
* @param {Object} parent
*/
var Base = module.exports = function (src, opts, parent) {
if(opts.onParseFile){
var Base = module.exports = function(src, opts, parent) {
if (opts.onParseFile) {
this.on('parseFile', opts.onParseFile.bind(parent));
}
if(opts.onAddModule){

if (opts.onAddModule) {
this.on('addModule', opts.onAddModule.bind(parent));
}

Expand Down
21 changes: 10 additions & 11 deletions test/pluggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe('Madge', function () {
var fileAdd = "";
var idAdd = "";
var opts = {};
opts.onParseFile = function(obj){
opts.onParseFile = function(obj) {
var arr = obj.filename.split(path.sep);
fileAdd += arr[arr.length-1];
};
opts.onAddModule = function(obj){
opts.onAddModule = function(obj) {
idAdd += obj.id;
};
madge([__dirname + '/files/cjs/normal'], opts);
(fileAdd+idAdd).should.eql( "a.jsd.jsb.jsc.js" + "adsub/bsub/c" );
(fileAdd + idAdd).should.eql( "a.jsd.jsb.jsc.js" + "adsub/bsub/c" );
});
});

Expand All @@ -25,28 +25,27 @@ describe('Madge', function () {
var fileAdd = "";
var idAdd = "";
var opts = {};
opts.onParseFile = function(obj){
opts.onParseFile = function(obj) {
var arr = obj.filename.split(path.sep);
fileAdd += arr[arr.length-1];
};
opts.onAddModule = function(obj){
opts.onAddModule = function(obj) {
idAdd += obj.id;
};
opts.format = 'amd';
madge([__dirname + '/files/amd/ok'], opts);
(idAdd+fileAdd).should.eql( "adesub/bsub/c" + "a.jsd.jse.jsb.jsc.js" );
(idAdd + fileAdd).should.eql( "adesub/bsub/c" + "a.jsd.jse.jsb.jsc.js" );
});
});

describe('pluggable - scope', function () {
it('should add idAdd property to the returned madger', function () {
var opts = {};
opts.onAddModule = function(obj){
if(this.idAdd){
opts.onAddModule = function(obj) {
if (this.idAdd) {
this.idAdd += obj.id;
}
else{
this.idAdd = ""+obj.id;
} else {
this.idAdd = "" + obj.id;
}
};
var madger = madge([__dirname + '/files/cjs/normal'], opts);
Expand Down

0 comments on commit dfa9c2b

Please sign in to comment.