/usr/lib/nodejs/astw/index.js is in node-astw 2.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | var parse = require('acorn').parse;
module.exports = function (src, opts) {
if (!opts) opts = {}
var ast = src;
if (typeof src === 'string') {
try {
ast = parse(src, {
ecmaVersion: opts.ecmaVersion || 8,
allowReturnOutsideFunction: true
})
}
catch (err) { ast = parse('(' + src + ')') }
}
return function (cb) {
walk(ast, undefined, cb);
};
};
function walk (node, parent, cb) {
var keys = objectKeys(node);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key === 'parent') continue;
var child = node[key];
if (isArray(child)) {
for (var j = 0; j < child.length; j++) {
var c = child[j];
if (c && typeof c.type === 'string') {
c.parent = node;
walk(c, node, cb);
}
}
}
else if (child && typeof child.type === 'string') {
child.parent = node;
walk(child, node, cb);
}
}
cb(node);
}
var isArray = Array.isArray || function (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
};
|