qjs
Version:
Use the await keyword with Q promises to tame your async code
82 lines (68 loc) • 2.17 kB
JavaScript
//Modified to work with next version of esprima (which gives the last index as an 'eclusive' number)
var parse = require('./esprima').parse;
module.exports = function (src, opts, fn) {
if (typeof opts === 'function') {
fn = opts;
opts = {};
}
if (typeof src === 'object') {
opts = src;
src = opts.source;
delete opts.source;
}
src = src || opts.source;
opts.range = true;
if (typeof src !== 'string') src = String(src);
var ast = parse(src, opts);
var result = {
chunks : src.split(''),
toString : function () { return result.chunks.join('') },
inspect : function () { return result.toString() }
};
var index = 0;
(function walk (node, parent) {
insertHelpers(node, parent, result.chunks);
Object.keys(node).forEach(function (key) {
if (key === 'parent') return;
var child = node[key];
if (Array.isArray(child)) {
child.forEach(function (c) {
if (c && typeof c.type === 'string') {
walk(c, node);
}
});
}
else if (child && typeof child.type === 'string') {
insertHelpers(child, node, result.chunks);
walk(child, node);
}
});
fn(node);
})(ast, undefined);
return result;
};
function insertHelpers (node, parent, chunks) {
if (!node.range) return;
node.parent = parent;
node.source = function () {
return chunks.slice(
node.range[0], node.range[1]
).join('');
};
if (node.update && typeof node.update === 'object') {
var prev = node.update;
Object.keys(prev).forEach(function (key) {
update[key] = prev[key];
});
node.update = update;
}
else {
node.update = update;
}
function update (s) {
chunks[node.range[0]] = s;
for (var i = node.range[0] + 1; i < node.range[1]; i++) {
chunks[i] = '';
}
};
}