Created
April 11, 2012 16:54
-
-
Save jbclements/2360500 to your computer and use it in GitHub Desktop.
Rabaino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* @dependency jslint.js | |
*/ | |
/** | |
* Uses JSLINT parse tree to find variable references. | |
* A variable reference is labeled as an identifier, and has no children, | |
* (identifiers such as 'var' would have children). | |
* @param source (or string (array string)) | |
* @return (array string) | |
*/ | |
function findVariables(source) | |
{ | |
var variables = new Array(); | |
// parse source | |
JSLINT(source); | |
// find variables | |
jslintSearch(JSLINT.tree, variables); | |
return variables; | |
} | |
/** | |
* Searches for variable | |
* @param node jslint parse tree node | |
* @param vars (array string) | |
*/ | |
function jslintSearch(node, vars) | |
{ | |
// nothing to work with | |
if(node == undefined) return; | |
// is a global variable name | |
if(node.identifier && node.first==undefined) vars.push(node.string); | |
// is an array | |
else if (node instanceof Array) | |
{ | |
// search each element | |
for(a in node) | |
{ | |
jslintSearch(node[a], vars); | |
} | |
} | |
// is an object | |
else if(node instanceof Object) | |
{ | |
// is a function, don't add function name | |
if(node.funct) | |
{ | |
for(v in node.funct) | |
if(node.funct[v] == "var" || node.funct[v] == "param") | |
vars.push(v); | |
} | |
// search the first part of everything else, if it exists | |
else if(node.first) | |
jslintSearch(node.first, vars); | |
// search the second part, if it exists | |
if(node.second) | |
jslintSearch(node.second, vars); | |
} | |
// unknown | |
else | |
{ | |
vars.push("?"+typeof node); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment