Last active
August 29, 2015 14:25
-
-
Save sujayy1983/aa77d787b2242d7419ad to your computer and use it in GitHub Desktop.
This file contains 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
__author__ = 'Sujayyendhiren Srinivasamurthi' | |
__email__ = 'sujayy1983@gmail.com' | |
__description__ = '''Javascript unused variable checker. | |
Tested with a simple PAC file too.''' | |
import os | |
dupvariables = {} | |
def parserfunction( js_file ): | |
nonvardata = '' | |
js_fd = open( js_file , "r") | |
js_data = [ line.strip('\n') for line in js_fd.readlines()] | |
for line in js_data: | |
if ('var ' in line and '[' not in line): | |
variable = line.replace(' ', '').split('=')[0].split('var')[1] | |
if variable not in dupvariables.keys(): | |
dupvariables[variable] = 1 | |
else: | |
dupvariables[variable] += 1 | |
else : | |
if 'var ' in line and '[' in line: | |
variable = line.replace('var ','').replace(' ','').split('[')[0] | |
decvariable = line.replace('var ','').replace(' ','').split('[')[1].split(']')[0] | |
nonvardata+=decvariable | |
if variable not in dupvariables.keys(): | |
dupvariables[variable] = 1 | |
else: | |
dupvariables[variable] += 1 | |
else: | |
nonvardata+= line + '\n' | |
for var in dupvariables.keys(): | |
if var not in nonvardata: | |
print "Unused variable: " + var | |
js_fd.close() | |
if __name__ == '__main__': | |
parserfunction( "test.js" ) | |
#Test case1 - test.js file | |
#var abcd = 0; | |
#var pqrs = 1; | |
#var stuv = 2; | |
#var tv[pqrs] = 5; | |
#function test() { | |
# pqrs = abcd; | |
# return pqrs; | |
#} | |
#test() | |
## Similarly try different combinations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment