-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyjslint.py
129 lines (107 loc) · 3.56 KB
/
pyjslint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# Copyright (C) 2011 Alejandro Blanco <[email protected]>
"""
wrapper for JSLint
"""
import os
import sys
import subprocess
from optparse import OptionParser
from tempfile import NamedTemporaryFile
try:
from urllib2 import urlopen # Python 2
except ImportError:
from urllib.request import urlopen # Python 3
default_jslint_options = r"""
maxerr: 100
"""
node_script = r"""
var JSLINT = require("%s").JSLINT,
print = require("sys").print,
readFileSync = require("fs").readFileSync,
error = null, i = 0, j = 0, src = null;
print("Analyzing file " + process.argv[2] + "\n");
src = readFileSync(process.argv[2], "utf8");
JSLINT(src, {%s});
for (j = 0; j < JSLINT.errors.length; j++ ) {
error = JSLINT.errors[j];
if (error !== null) {
if (typeof error.evidence !== "undefined") {
print("\n" + error.evidence + "\n");
} else {
print("\n");
}
print("Lint at line " + error.line + " character " +
error.character + ": " + error.reason);
}
}
if (JSLINT.errors.length > 0) {
print("\n" + JSLINT.errors.length + " Error(s) found.\n");
process.exit(1);
} else {
print("\nNo errors found.\n");
process.exit(0);
}
"""
usage = "Usage: %prog [options] jsfile"
parser = OptionParser(usage)
parser.add_option('-u', '--upgrade', dest='force', help='Upgrade JSLint',
action='store_true', default=False)
parser.add_option('-j', '--jslint', dest='jslint', help='JSLint location',
default=os.path.join('~', '.jslint', 'jslint.js'))
parser.add_option('-o', '--options', dest='jsoptions',
help='JSLint options', default=default_jslint_options)
parser.add_option('-n', '--node', dest='node',
help='Node location', default='node')
def execute_command(proc):
p = subprocess.Popen(proc, stdout=subprocess.PIPE)
out, err = p.communicate()
return out, p.returncode
def get_lint(options):
jslint = os.path.expanduser(options.jslint)
if not os.path.exists(jslint) or options.force:
# download jslint from github
response = urlopen('https://raw.github.com/douglascrockford/'
'JSLint/master/jslint.js')
if not os.path.exists(os.path.dirname(jslint)):
os.makedirs(os.path.dirname(jslint))
f = open(jslint, 'w')
f.write(response.read())
response.close()
f.write('\n\nexports.JSLINT = JSLINT;') # add node support
f.close()
# write node script
lint = NamedTemporaryFile("w+")
lint.write(node_script % (jslint[:-3], options.jsoptions))
lint.file.flush()
return lint
def process(jsfile, options):
lint = get_lint(options)
command = [options.node, lint.name, jsfile.name]
output, valid = execute_command(command)
jsfile.close()
lint.close()
output = output.decode("utf-8")
return [line for line in output.split("\n") if line], valid == 0
# Hooks entry point
def check_JSLint(code_string):
tmpfile = NamedTemporaryFile("w+")
tmpfile.write(code_string)
tmpfile.file.flush()
output, valid = process(tmpfile, parser.get_default_values())
if valid:
return []
else:
return output
def main():
(options, args) = parser.parse_args()
if len(args) < 1:
sys.stderr.write('One JavaScript file must be specified\n')
parser.print_usage()
sys.exit(False)
filename = args[0]
output, valid = process(open(filename, "r"), options)
print("\n".join(output))
sys.exit(valid)
if __name__ == "__main__":
main()