commonjs-walker
Advanced tools
Comparing version 7.0.1 to 7.0.2
@@ -1,59 +0,59 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = Walker; | ||
module.exports = Walker | ||
var parser = require('./parser'); | ||
var circular = require('./circular'); | ||
var tools = require('./tools'); | ||
var parser = require('./parser') | ||
var circular = require('./circular') | ||
var tools = require('./tools') | ||
var node_path = require('path'); | ||
var async = require('async'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var resolve = require('resolve'); | ||
var EE = require('events').EventEmitter; | ||
var mix = require('mix2'); | ||
var make_array = require('make-array'); | ||
var node_path = require('path') | ||
var async = require('async') | ||
var fs = require('fs') | ||
var util = require('util') | ||
var resolve = require('resolve') | ||
var EE = require('events').EventEmitter | ||
var mix = require('mix2') | ||
var make_array = require('make-array') | ||
function Walker (options) { | ||
this.options = options; | ||
this.nodes = {}; | ||
this.compilers = []; | ||
this.options = options | ||
this.nodes = {} | ||
this.compilers = [] | ||
if (!this._check_extensions()) { | ||
throw new Error('Invalid value of `options.extensions`'); | ||
throw new Error('Invalid value of `options.extensions`') | ||
} | ||
this._init_queue(); | ||
this._init_queue() | ||
} | ||
util.inherits(Walker, EE); | ||
util.inherits(Walker, EE) | ||
Walker.prototype._init_queue = function() { | ||
var self = this; | ||
var self = this | ||
this.queue = async.queue(function (task, done) { | ||
// `path` will always be an absolute path. | ||
var path = task.path; | ||
var path = task.path | ||
self._parse_file(path, function(err){ | ||
if (!err) { | ||
return done(); | ||
return done() | ||
} | ||
self.processing = false; | ||
self.queue.kill(); | ||
self.error = err; | ||
self.emit('done'); | ||
}); | ||
self.processing = false | ||
self.queue.kill() | ||
self.error = err | ||
self.emit('done') | ||
}) | ||
}, self.options.concurrency); | ||
}, self.options.concurrency) | ||
this.queue.drain = function () { | ||
self.processing = false; | ||
self.emit('done'); | ||
self.processing = false | ||
self.emit('done') | ||
} | ||
}; | ||
} | ||
@@ -63,61 +63,61 @@ | ||
if (this.doned) { | ||
throw new Error('walker: you should not call .walk() after .done()'); | ||
throw new Error('walker: you should not call .walk() after .done()') | ||
} | ||
make_array(entry).forEach(function(entry){ | ||
entry = node_path.resolve(entry); | ||
entry = node_path.resolve(entry) | ||
if (this._has_node(entry)) { | ||
return; | ||
return | ||
} | ||
this._walk(entry); | ||
}.bind(this)); | ||
this._walk(entry) | ||
}.bind(this)) | ||
return this; | ||
}; | ||
return this | ||
} | ||
Walker.prototype._walk = function(entry) { | ||
var node = this._create_node(entry); | ||
var node = this._create_node(entry) | ||
if (node.foreign) { | ||
return; | ||
return | ||
} | ||
this.processing = true; | ||
this.processing = true | ||
this.queue.push({ | ||
path: entry | ||
}); | ||
}; | ||
}) | ||
} | ||
Walker.prototype.done = function(callback) { | ||
this.doned = true; | ||
this.doned = true | ||
function cb () { | ||
callback(this.error || null, this.nodes); | ||
callback(this.error || null, this.nodes) | ||
} | ||
if (!this.processing) { | ||
return cb(); | ||
}; | ||
return cb() | ||
} | ||
this.once('done', cb) | ||
}; | ||
} | ||
// [ref](http://nodejs.org/api/modules.html#modules_file_modules) | ||
var EXTS_NODE = Walker.EXTS_NODE = ['.js', '.json', '.node']; | ||
var EXTS_NODE = Walker.EXTS_NODE = ['.js', '.json', '.node'] | ||
// Checks if the `options.extensions` is valid | ||
Walker.prototype._check_extensions = function() { | ||
var exts = this.options.extensions; | ||
var exts = this.options.extensions | ||
if (!util.isArray(exts)) { | ||
return false; | ||
return false | ||
} | ||
return exts.every(function (ext, i) { | ||
return ext === EXTS_NODE[i]; | ||
}); | ||
}; | ||
return ext === EXTS_NODE[i] | ||
}) | ||
} | ||
@@ -130,33 +130,33 @@ | ||
Walker.prototype.register = function(new_compilers) { | ||
new_compilers = make_array(new_compilers); | ||
new_compilers = make_array(new_compilers) | ||
var compilers = this.compilers; | ||
var compilers = this.compilers | ||
new_compilers.forEach(function (c) { | ||
c.test = util.isRegExp(c.test) | ||
? c.test | ||
: new RegExp(c.test); | ||
: new RegExp(c.test) | ||
compilers.push(c); | ||
}); | ||
compilers.push(c) | ||
}) | ||
return this; | ||
}; | ||
return this | ||
} | ||
Walker.prototype._parse_file = function(path, callback) { | ||
var self = this; | ||
var self = this | ||
this._get_compiled_content(path, function (err, compiled) { | ||
if (err) { | ||
return callback(err); | ||
return callback(err) | ||
} | ||
var node = self._get_node(path); | ||
node.require = {}; | ||
node.resolve = {}; | ||
node.async = {}; | ||
node.code = compiled.content; | ||
var node = self._get_node(path) | ||
node.require = {} | ||
node.resolve = {} | ||
node.async = {} | ||
node.code = compiled.content | ||
if (!compiled.js) { | ||
return callback(null); | ||
return callback(null) | ||
} | ||
@@ -166,23 +166,23 @@ | ||
if (err) { | ||
return callback(err); | ||
return callback(err) | ||
} | ||
async.each(['require', 'resolve', 'async'], function (type, done) { | ||
self._parse_dependencies_by_type(path, data[type], type, done); | ||
}, callback); | ||
}); | ||
}); | ||
}; | ||
self._parse_dependencies_by_type(path, data[type], type, done) | ||
}, callback) | ||
}) | ||
}) | ||
} | ||
Walker.prototype._get_compiled_content = function(filename, callback) { | ||
var self = this; | ||
var self = this | ||
this._read(filename, function (err, content) { | ||
if (err) { | ||
return callback(err); | ||
return callback(err) | ||
} | ||
self._compile(filename, content, callback); | ||
}); | ||
}; | ||
self._compile(filename, content, callback) | ||
}) | ||
} | ||
@@ -200,8 +200,8 @@ | ||
} | ||
}); | ||
}) | ||
} | ||
callback(null, content.toString()); | ||
}); | ||
}; | ||
callback(null, content.toString()) | ||
}) | ||
} | ||
@@ -212,3 +212,3 @@ | ||
var tasks = this.compilers.filter(function (c) { | ||
return c.test.test(filename); | ||
return c.test.test(filename) | ||
@@ -221,9 +221,9 @@ }).reduce(function (prev, c) { | ||
}, c.options, false); | ||
c.compiler(compiled.content, options, done); | ||
}; | ||
prev.push(task); | ||
return prev; | ||
}, c.options, false) | ||
c.compiler(compiled.content, options, done) | ||
} | ||
prev.push(task) | ||
return prev | ||
}, [init]); | ||
}, [init]) | ||
@@ -235,15 +235,15 @@ // If no registered compilers, just return | ||
js: tools.match_ext(filename, 'js') | ||
}); | ||
}) | ||
} | ||
async.waterfall(tasks, callback); | ||
}; | ||
async.waterfall(tasks, callback) | ||
} | ||
Walker.prototype._parse_dependencies_by_type = function(path, paths, type, callback) { | ||
var self = this; | ||
var options = this.options; | ||
var node = this._get_node(path); | ||
var self = this | ||
var options = this.options | ||
var node = this._get_node(path) | ||
async.each(paths, function (dep, done) { | ||
var origin = dep; | ||
var origin = dep | ||
@@ -258,8 +258,8 @@ if (dep.indexOf('/') === 0) { | ||
} | ||
}; | ||
} | ||
if (!options.allow_absolute_path) { | ||
return done(); | ||
return done() | ||
} else { | ||
self.emit('warn', message); | ||
self.emit('warn', message) | ||
} | ||
@@ -270,3 +270,3 @@ } | ||
// we only map top level id for now | ||
dep = self._solve_aliased_dependency(options['as'][dep], path) || dep; | ||
dep = self._solve_aliased_dependency(options['as'][dep], path) || dep | ||
} | ||
@@ -276,3 +276,3 @@ | ||
if (!self._is_relative_path(dep)) { | ||
return self._deal_dependency(origin, dep, node, type, done); | ||
return self._deal_dependency(origin, dep, node, type, done) | ||
} | ||
@@ -292,9 +292,9 @@ | ||
} | ||
}); | ||
}) | ||
} | ||
self._deal_dependency(origin, real, node, type, done); | ||
}); | ||
}, callback); | ||
}; | ||
self._deal_dependency(origin, real, node, type, done) | ||
}) | ||
}, callback) | ||
} | ||
@@ -312,33 +312,33 @@ | ||
Walker.prototype._solve_aliased_dependency = function(dep, env_path) { | ||
var cwd = this.options.cwd; | ||
var cwd = this.options.cwd | ||
if (!dep || !cwd || !this._is_relative_path(dep)) { | ||
return dep; | ||
return dep | ||
} | ||
dep = node_path.join(cwd, dep); | ||
dep = node_path.join(cwd, dep) | ||
dep = node_path.relative(node_path.dirname(env_path), dep) | ||
// After join and relative, dep will contains `node_path.sep` which varies from operating system, | ||
// so normalize it | ||
.replace(/\\/g, '/'); | ||
.replace(/\\/g, '/') | ||
if (!~dep.indexOf('..')) { | ||
// 'abc.js' -> './abc.js' | ||
dep = './' + dep; | ||
dep = './' + dep | ||
} | ||
return dep; | ||
}; | ||
return dep | ||
} | ||
Walker.prototype._deal_dependency = function(dep, real, node, type, callback) { | ||
node[type][dep] = real; | ||
node[type][dep] = real | ||
if (!this._has_node(real)) { | ||
// Only walk a module file if the node not exists. | ||
this._walk(real); | ||
return callback(null); | ||
this._walk(real) | ||
return callback(null) | ||
} | ||
var sub_node = this._get_node(real); | ||
var sub_node = this._get_node(real) | ||
@@ -349,3 +349,3 @@ // We only check the node if it meets the conditions below: | ||
// If one of the ancestor dependents of `node` is `current`, it forms a circle. | ||
var circular_trace; | ||
var circular_trace | ||
// node -> sub_node | ||
@@ -360,22 +360,22 @@ if (circular_trace = circular.trace(sub_node, node, this.nodes)) { | ||
} | ||
}; | ||
} | ||
if (!this.options.allow_cyclic) { | ||
return callback(message); | ||
return callback(message) | ||
} else { | ||
this.emit('warn', message); | ||
this.emit('warn', message) | ||
} | ||
} | ||
callback(null); | ||
}; | ||
callback(null) | ||
} | ||
Walker.prototype._has_node = function (path) { | ||
return path in this.nodes; | ||
}; | ||
return path in this.nodes | ||
} | ||
Walker.prototype._get_node = function(path) { | ||
return this.nodes[path]; | ||
}; | ||
return this.nodes[path] | ||
} | ||
@@ -392,14 +392,14 @@ | ||
foreign: this._is_foreign(id) | ||
}; | ||
}; | ||
} | ||
} | ||
Walker.prototype._is_foreign = function(path) { | ||
return !this._is_absolute_path(path); | ||
}; | ||
return !this._is_absolute_path(path) | ||
} | ||
Walker.prototype._is_absolute_path = function(path) { | ||
return node_path.resolve(path) === path.replace(/[\/\\]+$/, ''); | ||
}; | ||
return node_path.resolve(path) === path.replace(/[\/\\]+$/, '') | ||
} | ||
@@ -412,4 +412,7 @@ | ||
// so we should not use `'.' + node_path.sep` to test these paths | ||
return path.indexOf('./') === 0 || path.indexOf('../') === 0; | ||
}; | ||
return path === '.' | ||
|| path === '..' | ||
|| path.indexOf('./') === 0 | ||
|| path.indexOf('../') === 0 | ||
} | ||
@@ -422,14 +425,14 @@ | ||
var list = trace.map(function (node, index) { | ||
return index + 1 + ': ' + node.id; | ||
}); | ||
list.pop(); | ||
return index + 1 + ': ' + node.id | ||
}) | ||
list.pop() | ||
var flow = trace.map(function (node, index) { | ||
++ index; | ||
++ index | ||
return index === 1 || index === trace.length | ||
? '[1]' | ||
: index; | ||
}); | ||
: index | ||
}) | ||
return list.join('\n') + '\n\n' + flow.join(' -> '); | ||
}; | ||
return list.join('\n') + '\n\n' + flow.join(' -> ') | ||
} |
{ | ||
"name": "commonjs-walker", | ||
"version": "7.0.1", | ||
"version": "7.0.2", | ||
"description": "Analyzer and tree walker for commonjs.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -317,2 +317,10 @@ 'use strict'; | ||
} | ||
}, { | ||
desc: '#21: require(..)', | ||
options: {}, | ||
file: 'require-dot/lib/require.js', | ||
expect: function (err, path, nodes, entry) { | ||
expect(entry.require['.']).to.equal(node_path.join(node_path.dirname(path), './index.js')); | ||
expect(entry.require['..']).to.equal(node_path.join(node_path.dirname(path), '../index.js')); | ||
} | ||
} | ||
@@ -319,0 +327,0 @@ ]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41811
72
1190