Created
April 9, 2011 09:41
-
-
Save jed/911277 to your computer and use it in GitHub Desktop.
a function that takes code and returns a list of variable names needing declaration
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
var getVars = function( dummy, err, re ) { | |
try { eval( dummy ) } catch( e ) { err += e } | |
re = new RegExp( "^" + err.replace( dummy, "(\\w+)" ) + "$" ) | |
return function( code ) { | |
var args = [] | |
, name | |
while ( 1 ) { | |
try { Function.apply( Function, args.concat( code ) )() } | |
catch( e ) { | |
name = ( re.exec( e ) || [] )[ 1 ] | |
if ( !name ) throw e | |
args.push( name ) | |
continue | |
} | |
return args | |
} | |
} | |
}( "_" + Date.now(), "" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment