Skip to content

Commit

Permalink
Cleanup after PL.
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Apr 2, 2015
1 parent d5b0b60 commit a0d18ff
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"maxparams" : 5,
"maxdepth" : 5,
"maxstatements" : 25,
"maxcomplexity" : 11,
"maxcomplexity" : 10,
"predef" : [
"define",
"describe",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ minimize a global energy function, which is equivalent to statistical multi-dime

# Release Notes

## v0.5.0 (April 2, 2015)
Added support for ES6 modules (Thanks to Marc Laval).
Added support for setting custom file extension name (Thanks to Marc Laval).

## v0.4.1 (December 19, 2014)
Fixed issues with absolute paths for modules IDs in Windows (all tests should now pass on Windows too).

Expand Down
2 changes: 2 additions & 0 deletions bin/madge
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ program
.option('-M, --main-require-module', 'name of the primary RequireJS module, if it\'s included with `require()`', '')
.option('-j --json', 'output dependency tree in json')
.option('-p --paths <directory>', 'additional comma separated paths to search for dependencies (CJS only)', '')
.option('-e --extensions <list>', 'comma separated string of valid file extensions', 'js,coffee')
.parse(process.argv);

if (!program.args.length && !program.read && !program.requireConfig) {
Expand Down Expand Up @@ -79,6 +80,7 @@ function run() {
requireConfig: program.requireConfig,
mainRequireModule: program.mainRequireModule,
paths: program.paths ? program.paths.split(',') : undefined,
extensions: program.extensions.split(',').map(function (str) { return '.' + str; }),
findNestedDependencies: program.findNestedDependencies
});

Expand Down
27 changes: 18 additions & 9 deletions lib/madge.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,7 @@ function Madge(src, opts) {
}

if (src && src.length) {
if (this.opts.format === 'cjs') {
tree = new CJS(src, this.opts, this).tree;
} else if (this.opts.format === 'amd') {
tree = new AMD(src, this.opts, this).tree;
} else if (this.opts.format === 'es6') {
tree = new ES6(src, this.opts, this).tree;
} else {
throw new Error('invalid module format "' + this.opts.format + '"');
}
tree = this.parse(src);
}

if (this.opts.requireConfig) {
Expand All @@ -146,6 +138,23 @@ function Madge(src, opts) {
this.tree = tree;
}

/**
* Parse the given source folder(s).
* @param {Array|Object} src
* @return {Object}
*/
Madge.prototype.parse = function(src) {
if (this.opts.format === 'cjs') {
return new CJS(src, this.opts, this).tree;
} else if (this.opts.format === 'amd') {
return new AMD(src, this.opts, this).tree;
} else if (this.opts.format === 'es6') {
return new ES6(src, this.opts, this).tree;
} else {
throw new Error('invalid module format "' + this.opts.format + '"');
}
};

/**
* Return the module dependency graph as an object.
* @api public
Expand Down
4 changes: 2 additions & 2 deletions lib/parse/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ AMD.prototype.normalize = function (filename) {
AMD.prototype.parseFile = function (filename) {
try {
var dependencies = [],
src = this.getFileSource(filename);
src = this.getFileSource(filename),
fileData = {filename: filename, src: src};

var fileData = {filename: filename, src: src};
this.emit('parseFile', fileData);

if (/define|require\s*\(/m.test(fileData.src)) {
Expand Down
8 changes: 6 additions & 2 deletions lib/parse/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ var Base = module.exports = function(src, opts, parent) {
}

this.opts = opts;

if (typeof this.opts.extensions === "undefined") {
this.opts.extensions = ['.js'];
}

this.tree = {};
this.extRegEx = new RegExp("\.(coffee|jsx|" + this.opts.extensions.map(function(el) {return el.substring(1);}).join("|") + ")$","g");
this.extRegEx = new RegExp('\\.(coffee|jsx|' + this.opts.extensions.map(function(str) {
return str.substring(1);
}).join('|') + ')$', 'g');
this.coffeeExtRegEx = /\.coffee$/;
this.jsxExtRegEx = /\.jsx$/;
src = this.resolveTargets(src);
Expand Down Expand Up @@ -156,7 +160,7 @@ Base.prototype.getFileSource = function (filename) {
bare: true
});
} else if (filename.match(this.jsxExtRegEx)) {
src = jsx.transform( src );
src = jsx.transform(src);
}

return src;
Expand Down
6 changes: 3 additions & 3 deletions lib/parse/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ CJS.prototype.parseFile = function (filename) {
try {
if (fs.existsSync(filename)) {
var dependencies = [],
src = this.getFileSource(filename);
src = this.getFileSource(filename),
fileData = {filename: filename, src: src};

var fileData = {filename: filename, src: src};
this.emit('parseFile', fileData);
this.emit('parseFile', fileData);

if (/require\s*\(/m.test(fileData.src)) {
detective(fileData.src).map(function (id) {
Expand Down
68 changes: 34 additions & 34 deletions lib/parse/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* Module dependencies.
*/
var fs = require('fs'),
path = require('path'),
util = require('util'),
detective = require('detective-es6'),
colors = require('colors'),
Base = require('./base');
path = require('path'),
util = require('util'),
detective = require('detective-es6'),
colors = require('colors'),
Base = require('./base');

/**
* This class will parse the ES6 module format.
* @see http://nodejs.org/api/modules.html
* @constructor
*/
var ES6 = module.exports = function () {
Base.apply(this, arguments);
Base.apply(this, arguments);
};

/**
Expand All @@ -30,35 +30,35 @@ util.inherits(ES6, Base);
* @return {Array}
*/
ES6.prototype.parseFile = function (filename) {
try {
if (fs.existsSync(filename)) {
var dependencies = [],
src = this.getFileSource(filename);
try {
if (fs.existsSync(filename)) {
var dependencies = [],
src = this.getFileSource(filename),
fileData = {filename: filename, src: src};

var fileData = {filename: filename, src: src};
this.emit('parseFile', fileData);

if (/import.*from/m.test(fileData.src)) {
detective(fileData.src).map(function (id) {
var depFilename = this.resolve(path.dirname(fileData.filename), id);
if (depFilename) {
return this.normalize(depFilename);
}
}, this).filter(function (id) {
if (!this.isExcluded(id) && dependencies.indexOf(id) < 0) {
dependencies.push(id);
}
}, this);
this.emit('parseFile', fileData);

return dependencies;
}
}
} catch (e) {
if (this.opts.breakOnError) {
console.log(String('\nError while parsing file: ' + filename).red);
throw e;
}
}
if (/import.*from/m.test(fileData.src)) {
detective(fileData.src).map(function (id) {
var depFilename = this.resolve(path.dirname(fileData.filename), id);
if (depFilename) {
return this.normalize(depFilename);
}
}, this).filter(function (id) {
if (!this.isExcluded(id) && dependencies.indexOf(id) < 0) {
dependencies.push(id);
}
}, this);

return [];
return dependencies;
}
}
} catch (e) {
if (this.opts.breakOnError) {
console.log(String('\nError while parsing file: ' + filename).red);
throw e;
}
}

return [];
};

0 comments on commit a0d18ff

Please sign in to comment.