rhino.js
2.61 KB
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
/*jshint boss: true, rhino: true, unused: true, undef: true, quotmark: double */
/*global JSHINT, readFully */
(function(args) {
"use strict";
var filenames = [];
var flags = {};
var opts = {};
var globals = {};
var retval = 0;
var readf = (typeof readFully === "function" ? readFully : readFile);
var optstr; // arg1=val1,arg2=val2,...
var predef; // global1=true,global2,global3,...
args.forEach(function(arg) {
if (arg.indexOf("--") === 0) {
// Configuration Flags might be boolean or will be split into name and value
if (arg.indexOf("=") > -1) {
var o = arg.split("=");
flags[o[0].slice(2)] = o[1];
} else {
flags[arg.slice(2)] = true;
}
return;
} else if (arg.indexOf("=") > -1) {
// usual rhino configuration, like "boss=true,browser=true"
if (!optstr) {
// First time it's the options.
optstr = arg;
} else {
predef = arg;
}
return;
}
if (optstr) {
predef = arg;
return;
}
filenames.push(arg);
});
if (filenames.length === 0) {
print("Usage: jshint.js file.js");
quit(1);
}
// If a config flag has been provided, try and load that
if ("config" in flags) {
var cfgFileContent;
try {
cfgFileContent = readf(flags.config);
} catch (e) {
print("Could not read config file " + flags.config);
quit(1);
}
opts = JSON.parse(cfgFileContent);
}
if (optstr) {
optstr.split(",").forEach(function(arg) {
var o = arg.split("=");
if (o[0] === "indent") {
opts[o[0]] = parseInt(o[1], 10);
} else {
opts[o[0]] = (function(ov) {
switch (ov) {
case "true":
return true;
case "false":
return false;
default:
return ov;
}
}(o[1]));
}
});
}
globals = opts.globals || {};
delete(opts.globals);
if (predef) {
predef.split(",").forEach(function(arg) {
var global = arg.split("=");
globals[global[0]] = global[1] === "true" ? true : false;
});
}
filenames.forEach(function(name) {
var input = readf(name);
if (!input) {
print("jshint: Couldn't open file " + name);
quit(1);
}
if (!JSHINT(input, opts, globals)) {
for (var i = 0, err; err = JSHINT.errors[i]; i += 1) {
print(err.reason + " (" + name + ":" + err.line + ":" + err.character + ")");
print("> " + (err.evidence || "").replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
print("");
}
retval = 2;
}
});
quit(retval);
}(arguments));