-
Notifications
You must be signed in to change notification settings - Fork 2
/
JSLint
executable file
·149 lines (131 loc) · 3.8 KB
/
JSLint
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
//v8.js
/*global JSLINT require, process console*/
/*jslint rhino: true, strict: false, onevar: false, white: false, browser: true,
laxbreak: true, undef: true, nomen: false, eqeqeq: true, plusplus: false, bitwise: true,regexp: true, newcap: true, immed: true
*/
var jslint_file,
argv = process.argv,
jslint_file_name = '.jslint_core',
sys = require('util'),
fs = require('fs'),
path = require('path'),
core_path = path.dirname(process.mainModule.filename),
jslint_file_path = path.join(core_path, jslint_file_name),
simple = false,
defaults = {
bitwise: true, // Allow bitwise operators
browser: true, // Assume a browser ( http://www.JSLint.com/lint.html#browser )
css: true, // Tolerate CSS workarounds ( http://www.JSLint.com/lint.html#css )
eqeqeq: true, // Require `===` && `!==`
immed: true, // Immediate invocations must be wrapped in parens.
laxbreak: true, // Tolerate "sloppy" line breaks
newcap: true, // Require initial caps for constructors ( http://www.JSLint.com/lint.html#new )
nomen: false, // Allow dangling `_` in identifiers
onevar: false, // Allow multiple `var` statements.
plusplus: true, // Allow `++` and `--`
regexp: true, // Disallow `.` and `[^...]` in regex
strict: false, // Don't require `use strict;`
undef: true, // Disallow undeclared variables ( http://www.JSLint.com/lint.html#undefined )
white: true, // Don't apply strict whitespace rules
sloppy: true,
forin: true,
vars: true,
'continue': true
};
function init()
{
var i;
argv.shift(); // drop "node"
argv.shift(); // drop this script's name
jslint_file = fs.readFileSync(jslint_file_path, 'utf8');
eval(jslint_file);
i = argv.indexOf('--list');
if(i !== -1)
{
simple = true;
argv.splice(i, 1);
}
if(!argv[0])
{
sys.puts("Usage: node JSLint [--list] file.js [file2.js file3.js ...]");
sys.puts("\x1B[33mUsage: ./JSLint [--list] file.js [file2.js file3.js ...]");
sys.puts("\x1B[33mUsage: ./JSLint [--list] *.js\033[0m\n");
process.exit(1);
}
for(i = 0; i < argv.length; i++)
{
parseFile(JSLINT, argv[i]);
}
sys.puts('');
}
function parseFile(JSLINT, fileToParse)
{
var e, i, data, numErrors;
if(!path.existsSync(fileToParse))
{
sys.puts("\x1B[31mJSLint: Couldn't open file '" + fileToParse + "'.\n");
process.exit(1);
}
data = fs.readFileSync(fileToParse);
if ( !JSLINT( data.toString(), defaults ) )
{
sys.puts('>'+fileToParse+' -\x1B[31m '+JSLINT.errors.length+' problem(s) found\x1B[0m');
if(!simple)
{
sys.puts('');
for ( i = 0, numErrors = JSLINT.errors.length; i < numErrors; i += 1 )
{
e = JSLINT.errors[ i ];
if ( e )
{
sys.puts(
' \x1B[33mLine ' + e.line + ', Column: ' +
e.character + ': ' + e.reason + '\x1B[0m'
);
sys.puts(
' '+( e.evidence || '' ).replace( /^\s+|\s+$/, "" )
);
}
}
}
}
else
{
sys.puts('>'+fileToParse+' -\x1B[32m No problems found\x1B[0m');
//process.exit( 0 );
}
}
sys.puts('');
if(path.existsSync(jslint_file_path))
{
init();
}
else
{
var https = require('https'),
url = require('url'),
options = url.parse('https://raw.github.com/douglascrockford/JSLint/master/jslint.js'),
req;
sys.puts('\x1B[31mJSLint base file\x1B[0m ('+jslint_file_name+') \x1B[31mnot exists\x1B[0m');
sys.puts('\x1B[31mDownloading JSLint from '+options.href+' ...\x1B[0m');
req = https.request(options, function(res)
{
//console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function(){
sys.puts('\x1B[32mDownload completed\x1B[0m\n');
fs.writeFileSync(jslint_file_path, data, 'utf8');
init();
});
});
req.on('error', function(e)
{
console.log('problem with request: ' + e.message);
});
req.end();
}