[Pkg-javascript-commits] [node-lexical-scope] 01/83: detecting locals seemingly properly

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:45:45 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-lexical-scope.

commit aaaf2ba0a50ef045839335213f6cddec11ed60a1
Author: James Halliday <mail at substack.net>
Date:   Sun Feb 17 14:11:15 2013 +1000

    detecting locals seemingly properly
---
 example/detect.js |  7 +++++
 example/src.js    | 15 +++++++++
 index.js          | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)

diff --git a/example/detect.js b/example/detect.js
new file mode 100644
index 0000000..e04db0e
--- /dev/null
+++ b/example/detect.js
@@ -0,0 +1,7 @@
+var detect = require('../');
+var fs = require('fs');
+var src = fs.readFileSync(__dirname + '/src.js');
+
+var scope = detect(src);
+console.dir(scope.globals);
+console.dir(scope.locals);
diff --git a/example/src.js b/example/src.js
new file mode 100644
index 0000000..03e13c2
--- /dev/null
+++ b/example/src.js
@@ -0,0 +1,15 @@
+var x = 5;
+var y = 3, z = 2;
+w = 2;
+
+foo(function () {
+    var BAR = 3;
+    process.nextTick(function (ZZZZZZZZZZZZ) {
+        console.log('beep boop');
+        var xyz = 4;
+        x += 10;
+        x.zzzzzz;
+    });
+});
+
+console.log(xyz);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..2394598
--- /dev/null
+++ b/index.js
@@ -0,0 +1,91 @@
+var falafel = require('falafel');
+
+module.exports = function (src) {
+    var locals = {};
+    var globals = {};
+    
+    falafel(String(src), function (node) {
+        if (node.type === 'VariableDeclaration') {
+            // take off the leading `var `
+            var id = getScope(node);
+            node.declarations.forEach(function (d) {
+                locals[id][d.id.name] = d;
+            });
+        }
+    });
+    
+    return { locals: locals, globals: globals };
+    
+    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(node);
+        if (node.type === 'VariableDeclaration') {
+            // the var gets stripped off so the id needs updated
+            id = id.replace(/\.init$/, '.right');
+        }
+        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) {
+        if (!isFunction(n)) continue;
+        var key = keyOf(n).join('.');
+        id.unshift(key);
+    }
+    return id.join('.');
+}
+
+function keyOf (node) {
+    var p = node.parent;
+    var kv = Object.keys(p)
+        .reduce(function (acc, key) {
+            acc.keys.push(key);
+            acc.values.push(p[key]);
+            acc.top.push(undefined);
+            
+            if (Array.isArray(p[key])) {
+                var keys = Object.keys(p[key]);
+                acc.keys.push.apply(acc.keys, keys);
+                acc.values.push.apply(acc.values, p[key]);
+                acc.top.push.apply(
+                    acc.top,
+                    keys.map(function () { return key })
+                );
+            }
+            
+            return acc;
+        }, { keys : [], values : [], top : [] })
+    ;
+    var ix = kv.values.indexOf(node);
+    var res = [ kv.top[ix], kv.keys[ix] ].filter(Boolean);
+    if (node.parent.type === 'CallExpression') {
+        res.unshift.apply(res, keyOf(node.parent.parent));
+    }
+    return res;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-lexical-scope.git



More information about the Pkg-javascript-commits mailing list