1 /*jshint evil: true, shadow: true, wsh: true */
2 /*global JSHINT: false */
5 function readFile(path
, charset
) {
7 var stream
= WScript
.CreateObject("ADODB.Stream");
9 stream
.Charset
= charset
;
11 stream
.LoadFromFile(path
);
13 var result
= stream
.ReadText();
23 errors
: function(errors
, lines
) {
24 for (var i
= 0; i
< errors
.length
; i
++) {
25 var error
= errors
[i
];
29 if (i
) lines
.push("");
31 lines
.push("Line " + error
.line
+ " character " + error
.character
+ ": " + error
.reason
);
33 if (error
.evidence
) lines
.push(" " + error
.evidence
.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, "$1"));
37 implieds
: function(implieds
, lines
) {
38 lines
.push("Implied globals:");
42 for (var i
= 0; i
< implieds
.length
; i
++) {
43 var item
= implieds
[i
];
45 if (!(item
.name
in globals
)) globals
[item
.name
] = [];
47 globals
[item
.name
].push(item
.line
);
50 for (var name
in globals
) {
51 lines
.push(" " + name
+ ": " + globals
[name
].join(", "));
55 unused
: function(unused
, lines
) {
56 lines
.push("Unused variables:");
60 for (var i
= 0; i
< unused
.length
; i
++) {
63 func
= item
["function"];
65 if (!(func
in names
)) names
[func
] = [];
67 names
[func
].push(item
.name
+ " (" + item
.line
+ ")");
71 lines
.push(" " + func
+ ": " + names
[func
].join(", "));
76 var scriptName
= WScript
.ScriptName
;
77 var scriptPath
= WScript
.ScriptFullName
;
79 scriptPath
= scriptPath
.substr(0, scriptPath
.length
- scriptName
.length
);
81 // load JSHint if the two scripts have not been concatenated
82 if (typeof JSHINT
=== "undefined") {
83 eval(readFile(scriptPath
+ "..\\jshint.js", 'utf-8'));
85 if (typeof JSHINT
=== "undefined") {
86 WScript
.StdOut
.WriteLine("ERROR: Could not find 'jshint.js'.");
94 var named
= WScript
.Arguments
.Named
;
95 var unnamed
= WScript
.Arguments
.Unnamed
;
97 if (unnamed
.length
!== 1) {
98 WScript
.StdOut
.WriteLine(" usage: cscript " + scriptName
+ " [options] <script>");
99 WScript
.StdOut
.WriteLine("");
100 WScript
.StdOut
.WriteLine("Scans the specified script with JSHint and reports any errors encountered. If");
101 WScript
.StdOut
.WriteLine("the script name is \"-\", it will be read from standard input instead.");
102 WScript
.StdOut
.WriteLine("");
103 WScript
.StdOut
.WriteLine("JSHint configuration options can be passed in via optional, Windows-style");
104 WScript
.StdOut
.WriteLine("arguments. For example:");
105 WScript
.StdOut
.WriteLine(" cscript " + scriptName
+ " /jquery:true myscript.js");
106 WScript
.StdOut
.WriteLine(" cscript " + scriptName
+ " /global:QUnit:false,_:false,foo:true foo.js");
107 WScript
.StdOut
.WriteLine("");
108 WScript
.StdOut
.WriteLine("By default, we assume that your file is encoded if UTF-8. You can change that");
109 WScript
.StdOut
.WriteLine("by providing a custom charset option:");
110 WScript
.StdOut
.WriteLine(" cscript " + scriptName
+ " /charset:ascii myscript.js");
115 var script
= unnamed(0);
117 if (script
=== "-") {
119 script
= WScript
.StdIn
.ReadAll();
124 script
= readFile(script
, named('charset') || 'utf-8');
127 if (script
=== null) {
128 WScript
.StdOut
.WriteLine("ERROR: Could not read target script.");
133 for (var etor
= new Enumerator(named
); !etor
.atEnd(); etor
.moveNext()) {
134 var option
= etor
.item();
135 var value
= named(option
);
137 if (option
=== "global") {
138 value
= value
.split(",");
140 for (var i
= 0; i
< value
.length
; i
++) {
141 var name
= value
[i
].split(":");
143 if (name
.length
=== 1 || name
[1] === "false") {
144 globals
[name
[0]] = false;
145 } else if (name
[1] === "true") {
146 globals
[name
[0]] = true;
148 WScript
.StdOut
.WriteLine("Unrecognized value for global: " + name
[0]);
149 WScript
.StdOut
.WriteLine("Must be \"true\", \"false\", or omitted.");
155 options
[option
] = value
=== "true" ? true : value
=== "false" ? false : value
;
159 JSHINT(script
, options
, globals
);
161 var data
= JSHINT
.data();
164 for (var formatter
in formatters
) {
165 if (data
[formatter
]) {
166 if (lines
.length
) lines
.push("");
168 formatters
[formatter
](data
[formatter
], lines
);
173 for (var i
= 0; i
< lines
.length
; i
++) {
174 WScript
.StdOut
.WriteLine(lines
[i
]);