/usr/lib/nodejs/lexical-scope/index.js is in node-lexical-scope 1.2.0+dfsg1-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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | var astw = require('astw');
module.exports = function (src) {
var locals = {};
var implicit = {};
var exported = {};
var implicitProps = {};
if (typeof src === 'string') {
src = String(src).replace(/^#![^\n]*\n/, '');
}
if (src && typeof src === 'object'
&& typeof src.copy === 'function' && typeof src.toString === 'function') {
src = src.toString('utf8');
}
var walk = astw(src);
walk(function (node) {
if (node.type === 'VariableDeclaration') {
// take off the leading `var `
var id = getScope(node);
for (var i = 0; i < node.declarations.length; i++) {
var d = node.declarations[i];
locals[id][d.id.name] = d;
}
}
else if (node.type === 'CatchClause') {
var id = getScope(node);
locals[id][node.param.name] = node.param
}
else if (isFunction(node)) {
var id = getScope(node.parent);
if (node.id) locals[id][node.id.name] = node;
var nid = node.params.length && getScope(node);
if (nid && !locals[nid]) locals[nid] = {};
for (var i = 0; i < node.params.length; i++) {
var p = node.params[i];
locals[nid][p.name] = p;
}
}
});
walk(function (node) {
if (node.type === 'Identifier'
&& lookup(node) === undefined) {
if (node.parent.type === 'Property'
&& node.parent.key === node) return;
if (node.parent.type === 'MemberExpression'
&& node.parent.property === node) return;
if (isFunction(node.parent)) return;
if (node.parent.type === 'LabeledStatement') return;
if (node.parent.type === 'ContinueStatement') return;
if (node.parent.type === 'BreakStatement') return;
if (node.parent.type === 'AssignmentExpression') {
var isLeft0 = node.parent.left.type === 'MemberExpression'
&& node.parent.left.object === node.name
;
var isLeft1 = node.parent.left.type === 'Identifier'
&& node.parent.left.name === node.name
;
if (isLeft0 || isLeft1) {
exported[node.name] = keyOf(node).length;
}
}
if (!exported[node.name]
|| exported[node.name] < keyOf(node).length) {
implicit[node.name] = keyOf(node).length;
if (!implicitProps[node.name]) implicitProps[node.name] = {};
if (node.parent && node.parent.type === 'MemberExpression'
&& node.parent.property.type === 'Identifier') {
implicitProps[node.name][node.parent.property.name] = true;
}
else if (node.parent && node.parent.type === 'CallExpression'
&& node.parent.callee === node) {
implicitProps[node.name]['()'] = true;
}
else {
implicitProps[node.name]['*'] = true;
}
}
}
});
var localScopes = {};
var lks = objectKeys(locals);
for (var i = 0; i < lks.length; i++) {
var key = lks[i];
localScopes[key] = objectKeys(locals[key]);
}
var props = {};
var pkeys = objectKeys(implicitProps);
for (var i = 0; i < pkeys.length; i++) {
props[pkeys[i]] = objectKeys(implicitProps[pkeys[i]]);
}
return {
locals: localScopes,
globals: {
implicit: objectKeys(implicit),
implicitProperties: props,
exported: objectKeys(exported)
}
};
function lookup (node) {
for (var p = node; p; p = p.parent) {
if (isFunction(p) || p.type === 'Program') {
var id = getScope(p);
if (locals[id][node.name]) {
return id;
}
}
}
return undefined;
}
function getScope (node) {
for (
var p = node;
!isFunction(p) && p.type !== 'Program';
p = p.parent
);
var id = idOf(p);
if (!locals[id]) locals[id] = {};
return id;
}
};
function isFunction (x) {
return x.type === 'FunctionDeclaration'
|| x.type === 'FunctionExpression'
;
}
function idOf (node) {
var id = [];
for (var n = node; n.type !== 'Program'; n = n.parent) {
var key = keyOf(n).join('.');
id.unshift(key);
}
return id.join('.');
}
function keyOf (node) {
if (node.lexicalScopeKey) return node.lexicalScopeKey;
var p = node.parent;
var ks = objectKeys(p);
var kv = { keys : [], values : [], top : [] };
for (var i = 0; i < ks.length; i++) {
var key = ks[i];
kv.keys.push(key);
kv.values.push(p[key]);
kv.top.push(undefined);
if (isArray(p[key])) {
var keys = objectKeys(p[key]);
kv.keys.push.apply(kv.keys, keys);
kv.values.push.apply(kv.values, p[key]);
var nkeys = [];
for (var j = 0; j < keys.length; j++) nkeys.push(key);
kv.top.push.apply(kv.top, nkeys);
}
}
var ix = indexOf(kv.values, node);
var res = [];
if (kv.top[ix]) res.push(kv.top[ix]);
if (kv.keys[ix]) res.push(kv.keys[ix]);
if (node.parent.type === 'CallExpression') {
res.unshift.apply(res, keyOf(node.parent.parent));
}
return node.lexicalScopeKey = res;
}
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;
};
function indexOf (xs, x) {
if (xs.indexOf) return xs.indexOf(x);
for (var i = 0; i < xs.length; i++) {
if (x === xs[i]) return i;
}
return -1;
}
|