Skip to content

Commit

Permalink
update to 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed May 4, 2015
1 parent bbce952 commit 8ccdc48
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 115 deletions.
59 changes: 30 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
var parser = require('./lib/parser');

// Parse dependencies
function parseDependencies(s, replace, async){
function parseDependencies(str, replace, async){
if (replace === true) {
async = true;
replace = null;
replace = undefined;
}

if (s.indexOf('require') === -1) {
return replace ? s : [];
if (str.indexOf('require') === -1) {
return replace ? str : [];
}

var REQUIRERE = async
Expand All @@ -23,7 +23,7 @@ function parseDependencies(s, replace, async){
var FLAGRE = /^require\s*(?:(?:\.\s*([a-zA-Z_$][\w$]*))|(?:\[\s*(['"])(.*?)\2\s*\]))/;
var CHAINRE = /^[\w$]+(?:\s*\.\s*[\w$]+)*/;

var index = 0, peek = '', length = s.length, isReg = 1, isReturn = 0, meta = [];
var index = 0, peek = '', length = str.length, isReg = 1, isReturn = 0, meta = [];
var parentheseState = 0, parentheseStack = [], braceState = 0, braceStack = [];
var mod = '', modStart = 0, modEnd = 0, modName = 0, modParenthese = [], flag = null;

Expand All @@ -45,14 +45,14 @@ function parseDependencies(s, replace, async){
readch();

if (peek === '/') {
index = s.indexOf('\n', index);
index = str.indexOf('\n', index);

if (index === -1) {
index = s.length;
index = str.length;
}
} else if (peek === '*') {
var i = s.indexOf('\n', index);
index = s.indexOf('*/', index);
var i = str.indexOf('\n', index);
index = str.indexOf('*/', index);

if (index === -1) {
index = length;
Expand Down Expand Up @@ -105,16 +105,16 @@ function parseDependencies(s, replace, async){
modName = 0;
modEnd = index;

mod = s.substring(modStart, modEnd);
mod = str.substring(modStart, modEnd);

if (replace) {
var replaced = parser(mod, replace, { flag: flag });

s = s.slice(0, modStart) + replaced + s.slice(modEnd);
str = str.slice(0, modStart) + replaced + str.slice(modEnd);

if (replaced.length !== mod.length) {
index = modStart + replaced.length;
length = s.length;
length = str.length;
}
} else {
meta = meta.concat(parser(mod, { flag: flag }));
Expand All @@ -136,7 +136,7 @@ function parseDependencies(s, replace, async){
isReg = !braceState;
isReturn = 0;
} else {
var next = s.charAt(index);
var next = str.charAt(index);

if (peek === ';') {
braceState = 0;
Expand All @@ -152,10 +152,10 @@ function parseDependencies(s, replace, async){
}
}

return replace ? s : meta;
return replace ? str : meta;

function readch(){
peek = s.charAt(index++);
peek = str.charAt(index++);
}

function isBlank(){
Expand All @@ -169,11 +169,11 @@ function parseDependencies(s, replace, async){
function dealQuote(){
var start = index;
var c = peek;
var end = s.indexOf(c, start);
var end = str.indexOf(c, start);

if (end === -1) {
index = length;
} else if (s.charAt(end - 1) !== '\\') {
} else if (str.charAt(end - 1) !== '\\') {
index = end + 1;
} else {
while (index < length) {
Expand Down Expand Up @@ -217,8 +217,8 @@ function parseDependencies(s, replace, async){
}

function dealWord(){
var s2 = s.slice(index - 1);
var r = /^[\w$]+/.exec(s2)[0];
var substr = str.slice(index - 1);
var r = /^[\w$]+/.exec(substr)[0];

parentheseState = {
'if': 1,
Expand Down Expand Up @@ -252,39 +252,40 @@ function parseDependencies(s, replace, async){
}.hasOwnProperty(r);

if (r === 'require') {
modName = REQUIRERE.test(s2);
modName = REQUIRERE.test(substr);
}

if (r === 'require' && modName) {
modStart = index - 1;
r = REQUIRERE.exec(s2)[0];
r = REQUIRERE.exec(substr)[0];
index += r.length - 3;
flag = FLAGRE.exec(s2);
flag = FLAGRE.exec(substr);
flag = flag ? flag[1] || flag[3] : null;
} else {
index += CHAINRE.exec(s2)[0].length - 1;
index += CHAINRE.exec(substr)[0].length - 1;
}
}

function isNumber(){
return /\d/.test(peek) || peek === '.' && /\d/.test(s.charAt(index));
return /\d/.test(peek) || peek === '.' && /\d/.test(str.charAt(index));
}

function dealNumber(){
var s2 = s.slice(index - 1);
var r;
var substr = str.slice(index - 1);

if (peek === '.') {
r = /^\.\d+(?:E[+-]?\d*)?\s*/i.exec(s2)[0];
} else if (/^0x[\da-f]*/i.test(s2)) {
r = /^0x[\da-f]*\s*/i.exec(s2)[0];
r = /^\.\d+(?:E[+-]?\d*)?\s*/i.exec(substr)[0];
} else if (/^0x[\da-f]*/i.test(substr)) {
r = /^0x[\da-f]*\s*/i.exec(substr)[0];
} else {
r = /^\d+\.?\d*(?:E[+-]?\d*)?\s*/i.exec(s2)[0];
r = /^\d+\.?\d*(?:E[+-]?\d*)?\s*/i.exec(substr)[0];
}

index += r.length - 1;
isReg = 0;
}
}

// Exports
module.exports = parseDependencies;
116 changes: 59 additions & 57 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,43 @@ function parse(src, opts){
if (!opts) opts = {};

return acorn.parse(src, {
ranges: defined(opts.ranges, opts.range),
tolerant: true,
ranges: defined(opts.ranges, false),
ecmaVersion: defined(opts.ecmaVersion, 6),
locations: defined(opts.locations, opts.loc),
locations: defined(opts.locations, null),
allowReturnOutsideFunction: defined(opts.allowReturnOutsideFunction, true),
strictSemicolons: defined(opts.strictSemicolons, false),
allowTrailingCommas: defined(opts.allowTrailingCommas, true),
forbidReserved: defined(opts.forbidReserved, false)
});
}

// The traverse function
function traverse(node, cb){
if (node) {
if (Array.isArray(node)) {
node.forEach(function (item){
if (item) {
traverse(item, cb);
}
});
} else if (node && typeof node === 'object') {
cb(node);
// Executes visitor on the object and its children (recursively).
function traverse(object, visitor){
var key, child;

Object.keys(node).forEach(function (key){
if (node[key]) {
traverse(node[key], cb);
}
});
if (visitor.call(null, object) === false) {
return;
}

for (key in object) {
if (object.hasOwnProperty(key)) {
child = object[key];

if (typeof child === 'object' && child !== null) {
traverse(child, visitor);
}
}
}
}

// The tree walk function
function walk(src, opts, cb){
var ast = parse(src, opts);
var syntax = parse(src, opts);

traverse(ast, cb);
traverse(syntax, cb);

return ast;
return syntax;
}

// The is require function
Expand All @@ -81,61 +80,64 @@ module.exports = function (src, replace, opts){
var offset = 0;
var modules = [];

if (typeof replace === 'object' && !Array.isArray(replace)) {
if (replace && typeof replace === 'object' && !Array.isArray(replace)) {
opts = replace;
replace = undefined;
}

opts = opts || {};
opts.parse = opts.parse || {};
opts.parse.tolerant = true;

if (typeof src !== 'string') src = src + '';
if (typeof opts.word !== 'string') opts.word = 'require';
if (src.indexOf(opts.word) === -1) return modules;
if (replace && typeof replace !== 'function') replace = noop;

walk(src, opts.parse, function (node){
if (isRequire(node, opts.word, opts.flag)) {
var args = node.arguments;
var flag = node.callee.property ? opts.flag : null;

var handle = function (node){
var update,
value = node.value;
try {
walk(src, opts.parse, function (node){
if (isRequire(node, opts.word, opts.flag)) {
var args = node.arguments;
var flag = node.callee.property ? opts.flag : null;

// Push modules in to array
modules.push({
flag: flag,
path: value
});
var handle = function (node){
var update,
value = node.value;

// Replace code
if (replace) {
update = replace(value, opts.flag);

if (typeof update === 'string') {
src = src.substring(0, node.start + offset + 1) + update + src.substring(node.end + offset - 1);
offset += update.length - value.length;
}
}
};
// Push modules in to array
modules.push({
flag: flag,
path: value
});

if (args.length) {
args = args[0];
// Replace code
if (replace) {
update = replace(value, opts.flag);

if (args.type === 'Literal') {
handle(args);
} else if (args.type === 'ArrayExpression') {
args.elements.forEach(function (args){
if (args.type === 'Literal') {
handle(args);
if (typeof update === 'string') {
src = src.substring(0, node.start + offset + 1) + update + src.substring(node.end + offset - 1);
offset += update.length - value.length;
}
});
}
};

if (args.length) {
args = args[0];

if (args.type === 'Literal') {
handle(args);
} else if (args.type === 'ArrayExpression') {
args.elements.forEach(function (args){
if (args.type === 'Literal') {
handle(args);
}
});
}
}
}
}
});
});
} catch (e) {
// Not a standard script code, do nothing
}

return replace ? src : modules;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cmd-deps",
"version": "0.0.2",
"version": "0.0.3",
"description": "Get commonjs dependences",
"author": {
"name": "nuintun",
Expand Down
Loading

0 comments on commit 8ccdc48

Please sign in to comment.