commonjs-walker
Advanced tools
Comparing version 7.0.0 to 7.0.1
@@ -1,9 +0,9 @@ | ||
'use strict'; | ||
'use strict' | ||
var parser = exports; | ||
var esprima = require('esprima'); | ||
var node_path = require('path'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var unique = require('array-unique'); | ||
var parser = exports | ||
var esprima = require('esprima') | ||
var node_path = require('path') | ||
var fs = require('fs') | ||
var util = require('util') | ||
var unique = require('array-unique') | ||
var tools = require('./tools') | ||
@@ -27,3 +27,3 @@ | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -35,6 +35,6 @@ | ||
async: [] | ||
}; | ||
} | ||
try { | ||
parser._parse_dependencies(ast, dependencies, options); | ||
parser._parse_dependencies(ast, dependencies, options) | ||
} catch(e) { | ||
@@ -48,7 +48,7 @@ return callback({ | ||
} | ||
}); | ||
}) | ||
} | ||
if (options.comment_require) { | ||
parser._parse_comments(ast, dependencies, options); | ||
parser._parse_comments(ast, dependencies, options) | ||
} | ||
@@ -62,5 +62,5 @@ | ||
async: unique(dependencies.async) | ||
}); | ||
}); | ||
}; | ||
}) | ||
}) | ||
} | ||
@@ -70,3 +70,5 @@ | ||
parser._lex_js = function (content, callback) { | ||
var ast; | ||
content = parser._silly_wrap(content) | ||
var ast | ||
try { | ||
@@ -76,11 +78,18 @@ ast = esprima.parse(content, { | ||
comment: true | ||
}); | ||
}) | ||
} catch(e) { | ||
return callback(e); | ||
return callback(e) | ||
} | ||
callback(null, ast); | ||
}; | ||
callback(null, ast) | ||
} | ||
parser._silly_wrap = function (content) { | ||
return '(function(){\n' // '\n' to prevent '(function(){//a})' | ||
+ content | ||
+ '\n})()' | ||
} | ||
// Parses AST and returns the dependencies | ||
@@ -90,3 +99,3 @@ parser._parse_dependencies = function (node, dependencies, options) { | ||
if (!node || Object(node) !== node) { | ||
return; | ||
return | ||
} | ||
@@ -97,3 +106,4 @@ | ||
&& node.callee.type === 'Identifier' | ||
&& node.callee.name === 'require'; | ||
&& node.callee.name === 'require' | ||
}, dependencies.normal, options, true) | ||
@@ -105,3 +115,4 @@ | ||
&& node.callee.object.name === 'require' | ||
&& node.callee.property.name === 'resolve'; | ||
&& node.callee.property.name === 'resolve' | ||
}, dependencies.resolve, options, true) | ||
@@ -113,17 +124,17 @@ | ||
&& node.callee.object.name === 'require' | ||
&& node.callee.property.name === 'async'; | ||
}, dependencies.async, options, false); | ||
&& node.callee.property.name === 'async' | ||
}, dependencies.async, options, false) | ||
if (util.isArray(node)) { | ||
node.forEach(function (sub) { | ||
parser._parse_dependencies(sub, dependencies, options); | ||
}); | ||
parser._parse_dependencies(sub, dependencies, options) | ||
}) | ||
} else { | ||
var key; | ||
var key | ||
for (key in node) { | ||
parser._parse_dependencies(node[key], dependencies, options); | ||
parser._parse_dependencies(node[key], dependencies, options) | ||
} | ||
} | ||
}; | ||
} | ||
@@ -133,62 +144,62 @@ | ||
if (!condition(node)) { | ||
return; | ||
return | ||
} | ||
var args = node.arguments; | ||
var loc = node.callee.loc.start; | ||
var loc_text = generate_loc_text(loc); | ||
var check_length = options.check_require_length; | ||
var args = node.arguments | ||
var loc = node.callee.loc.start | ||
var loc_text = generate_loc_text(loc) | ||
var check_length = options.check_require_length | ||
if (args.length === 0) { | ||
tools.throw(check_length, loc_text + 'Method `require` accepts one and only one parameter.'); | ||
tools.throw(check_length, loc_text + 'Method `require` accepts one and only one parameter.') | ||
} | ||
if (check_if_length_exceeded && args.length > 1) { | ||
tools.throw(check_length, loc_text + 'Method `require` should not contains more than one parameters'); | ||
tools.throw(check_length, loc_text + 'Method `require` should not contains more than one parameters') | ||
} | ||
var arg1 = args[0]; | ||
var arg1 = args[0] | ||
if (!arg1) { | ||
return; | ||
return | ||
} | ||
if (arg1.type !== 'Literal') { | ||
tools.throw(!options.allow_non_literal_require, generate_loc_text(arg1.loc.start) + 'Method `require` only accepts a string literal.' ); | ||
tools.throw(!options.allow_non_literal_require, generate_loc_text(arg1.loc.start) + 'Method `require` only accepts a string literal.' ) | ||
} else { | ||
deps_array.push(arg1.value); | ||
deps_array.push(arg1.value) | ||
} | ||
}; | ||
} | ||
var REGEX_LEFT_PARENTHESIS_STRING = '\\s*\\(\\s*([\'"])([A-Za-z0-9_\\/\\-\\.]+)\\1\\s*'; | ||
var REGEX_PARENTHESIS_STRING = REGEX_LEFT_PARENTHESIS_STRING + '\\)'; | ||
var REGEX_LEFT_PARENTHESIS_STRING = '\\s*\\(\\s*([\'"])([A-Za-z0-9_\\/\\-\\.]+)\\1\\s*' | ||
var REGEX_PARENTHESIS_STRING = REGEX_LEFT_PARENTHESIS_STRING + '\\)' | ||
var REGEX_REQUIRE = | ||
new RegExp('@require' + REGEX_PARENTHESIS_STRING, 'g'); | ||
new RegExp('@require' + REGEX_PARENTHESIS_STRING, 'g') | ||
var REGEX_REQUIRE_RESOLVE = | ||
new RegExp('@require\\.resolve' + REGEX_PARENTHESIS_STRING, 'g'); | ||
new RegExp('@require\\.resolve' + REGEX_PARENTHESIS_STRING, 'g') | ||
var REGEX_REQUIRE_ASYNC = | ||
new RegExp('@require\\.async' + REGEX_LEFT_PARENTHESIS_STRING, 'g'); | ||
new RegExp('@require\\.async' + REGEX_LEFT_PARENTHESIS_STRING, 'g') | ||
// Parses `@require`, `@require.resolve`, `@require.async` in comments | ||
parser._parse_comments = function (ast, dependencies, options) { | ||
var comments = ast.comments; | ||
var comments = ast.comments | ||
if (!comments) { | ||
return; | ||
return | ||
} | ||
comments.forEach(function (comment) { | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE, dependencies.normal); | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE, dependencies.normal) | ||
if (options.require_resolve) { | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE_RESOLVE, dependencies.resolve); | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE_RESOLVE, dependencies.resolve) | ||
} | ||
if (options.require_async) { | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE_ASYNC, dependencies.async); | ||
parser._parse_by_regex(comment.value, REGEX_REQUIRE_ASYNC, dependencies.async) | ||
} | ||
}); | ||
}; | ||
}) | ||
} | ||
@@ -200,11 +211,11 @@ | ||
parser._parse_by_regex = function (content, regex, matches) { | ||
var match; | ||
var match | ||
while(match = regex.exec(content)){ | ||
matches.push(match[2]); | ||
matches.push(match[2]) | ||
} | ||
}; | ||
} | ||
function generate_loc_text (loc) { | ||
return 'Line ' + loc.line + ': Column ' + loc.column + ': '; | ||
return 'Line ' + loc.line + ': Column ' + loc.column + ': ' | ||
} |
{ | ||
"name": "commonjs-walker", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "Analyzer and tree walker for commonjs.", | ||
@@ -38,3 +38,3 @@ "main": "index.js", | ||
"async": "^1.4.0", | ||
"esprima": "^2.4.1", | ||
"esprima": "^2.7.1", | ||
"make-array": "^0.1.2", | ||
@@ -41,0 +41,0 @@ "mix2": "^1.0.1", |
@@ -7,5 +7,17 @@ 'use strict'; | ||
var util = require('util'); | ||
var make_array = require('make-array') | ||
var root = node_path.join(__dirname, 'fixtures', 'walker'); | ||
function dir_slash (err, path, nodes, entry) { | ||
expect(err).to.equal(null); | ||
var dep = './cases/dir/'; | ||
var real = node_path.join( node_path.dirname(path), dep ) + 'index.js'; | ||
expect(entry.require[dep]).to.equal(real); | ||
} | ||
function multiple_requires (err, path, nodes, entry) { | ||
expect(err).to.equal(null); | ||
} | ||
var cases = [ | ||
@@ -186,8 +198,3 @@ { | ||
file: 'fallback/dir-slash.js', | ||
expect: function (err, path, nodes, entry) { | ||
expect(err).to.equal(null); | ||
var dep = './cases/dir/'; | ||
var real = node_path.join( node_path.dirname(path), dep ) + 'index.js'; | ||
expect(entry.require[dep]).to.equal(real); | ||
} | ||
expect: dir_slash | ||
}, | ||
@@ -199,7 +206,12 @@ { | ||
file: 'multi-require/index.js', | ||
expect: function (err, path, nodes, entry) { | ||
expect(err).to.equal(null); | ||
} | ||
expect: multiple_requires | ||
}, | ||
{ | ||
desc: '#25: multi-walker', | ||
options: {}, | ||
file: ['fallback/dir-slash.js', 'multi-require/index.js'], | ||
expect: [dir_slash, multiple_requires], | ||
multi: true | ||
}, | ||
{ | ||
desc: '#14: parsing a json file will not fail', | ||
@@ -331,4 +343,7 @@ file: 'json/index.js', | ||
i(desc, function(done){ | ||
var file = node_path.join(root, c.file); | ||
var file = make_array(c.file).map(function(f){ | ||
return node_path.join(root, f); | ||
}); | ||
var warnings = []; | ||
var tests = make_array(c.expect); | ||
@@ -338,6 +353,9 @@ var callback = function (err, nodes) { | ||
var entry; | ||
if (!err && nodes) { | ||
entry = nodes[file] | ||
} | ||
c.expect(err, file, nodes, entry, warnings); | ||
file.forEach(function(f, i){ | ||
if (!err && nodes) { | ||
entry = nodes[f] | ||
} | ||
tests[i](err, f, nodes, entry, warnings); | ||
}); | ||
}; | ||
@@ -353,3 +371,6 @@ | ||
w.walk(file).done(callback); | ||
var f = c.multi | ||
? file | ||
: file[0] | ||
w.walk(f).done(callback); | ||
}); | ||
@@ -356,0 +377,0 @@ } |
@@ -53,2 +53,8 @@ 'use strict'; | ||
error: true | ||
}, | ||
{ | ||
desc: '#26: allow return statement', | ||
file: 'return.js', | ||
options: {}, | ||
deps: ['..'] | ||
} | ||
@@ -59,3 +65,7 @@ ]; | ||
cases.forEach(function (c) { | ||
it(c.desc, function(done) { | ||
var _it = c.only | ||
? it.only | ||
: it | ||
_it(c.desc, function(done) { | ||
var file = node_path.join(__dirname, 'fixtures', 'parser', c.file); | ||
@@ -62,0 +72,0 @@ parser.parse(file, fs.readFileSync(file).toString(), c.options || {}, function (err, result) { |
41529
69
1178
Updatedesprima@^2.7.1