Permalink
Browse files

detect some variables

  • Loading branch information...
0 parents commit cadb2b87aafe671f762a7dd031bf0e5673ad3627 @substack committed Oct 23, 2012
Showing with 41 additions and 0 deletions.
  1. +32 −0 detect.js
  2. +9 −0 source.js
@@ -0,0 +1,32 @@
+var falafel = require('falafel');
+var fs = require('fs');
+
+var src = fs.readFileSync(process.argv[2], 'utf8');
+var out = falafel(src, function (node) {
+ if (node.type === 'VariableDeclarator') {
+ var context = parents(node).filter(function (x) {
+ return x.type === 'FunctionDeclaration'
+ || x.type === 'FunctionExpression'
+ ;
+ });
+
+ var scope = context.map(function (c) {
+ return c.id.name
+ });
+ console.dir({
+ id : node.id.name,
+ scope : scope
+ });
+ node.update(node.source().toUpperCase());
+ }
+});
+console.log(out);
+
+function parents (node) {
+ var p = node;
+ var ps = [];
+ while (p = p.parent) {
+ ps.push(p);
+ }
+ return ps;
+}
@@ -0,0 +1,9 @@
+var x = 5;
+
+function foo () {
+ var y = 100;
+
+ (function bar () {
+ var z = 6;
+ })();
+}

0 comments on commit cadb2b8

Please sign in to comment.