Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactored wsh.js output formatting into a series of property formatters

  • Loading branch information...
commit b0ba5991358976e40b9c24ad523ed0eab33eb118 1 parent 41d9552
Ben Blank authored

Showing 1 changed file with 54 additions and 45 deletions. Show diff stats Hide diff stats

  1. 99  env/wsh.js
99  env/wsh.js
@@ -2,77 +2,86 @@
2 2
 /*global ActiveXObject: false, JSHINT: false, WScript: false */
3 3
 
4 4
 (function() {
5  
-	// load JSHint if the two scripts have not been concatenated
6  
-	if (typeof JSHINT === "undefined") eval(new ActiveXObject("Scripting.FileSystemObject").OpenTextFile("..\\jshint.js", 1).ReadAll());
  5
+	var formatters = {
  6
+		errors: function(errors, lines) {
  7
+			for (var i = 0; i < errors.length; i++) {
  8
+				var error = errors[i];
7 9
 
8  
-	var source = WScript.StdIn.ReadAll();
  10
+				if (!error) continue;
9 11
 
10  
-	// trim junk character; not sure where it comes from
11  
-	JSHINT(source.substr(0, source.length - 1), { passfail: false });
  12
+				if (i) lines.push("");
12 13
 
13  
-	var data = JSHINT.data();
14  
-	var exit = 0;
  14
+				lines.push("Line " + error.line + " character " + error.character + ": " + error.reason);
15 15
 
16  
-	if (data.errors && data.errors.length) {
17  
-		exit = 1;
  16
+				if (error.evidence) lines.push("    " + error.evidence.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, "$1"));
  17
+			}
  18
+		},
18 19
 
19  
-		for (var i = 0; i < data.errors.length; i++) {
20  
-			var error = data.errors[i];
  20
+		implieds: function(implieds, lines) {
  21
+			lines.push("Implied globals:");
21 22
 
22  
-			if (!error) continue;
  23
+			var globals = {};
23 24
 
24  
-			if (i) WScript.StdOut.WriteLine("");
  25
+			for (var i = 0; i < implieds.length; i++) {
  26
+				var item = implieds[i];
25 27
 
26  
-			WScript.StdOut.WriteLine("Lint at line " + error.line + " character " + error.character + ": " + error.reason);
  28
+				if (!(item.name in globals)) globals[item.name] = [];
27 29
 
28  
-			if (error.evidence) WScript.StdOut.WriteLine("    " + error.evidence.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, "$1"));
29  
-		}
  30
+				globals[item.name].push(item.line);
  31
+			}
30 32
 
31  
-		if ((data.unused && data.unused.length) || (data.implieds && data.implieds.length)) WScript.StdOut.WriteLine("");
32  
-	}
  33
+			for (var name in globals) {
  34
+				lines.push("    " + name + ": " + globals[name].join(", "));
  35
+			}
  36
+		},
33 37
 
34  
-	if (data.unused && data.unused.length) {
35  
-		exit = 1;
  38
+		unused: function(unused, lines) {
  39
+			lines.push("Unused variables:");
36 40
 
37  
-		WScript.StdOut.WriteLine("Unused variables:");
  41
+			var func, names = {};
38 42
 
39  
-		var unused = {};
  43
+			for (var i = 0; i < unused.length; i++) {
  44
+				var item = unused[i];
40 45
 
41  
-		for (var i = 0; i < data.unused.length; i++) {
42  
-			var item = data.unused[i];
43  
-			var func = item["function"];
  46
+				func = item["function"];
44 47
 
45  
-			if (!(func in unused)) unused[func] = [];
  48
+				if (!(func in names)) names[func] = [];
46 49
 
47  
-			unused[func].push(item.name + " (" + item.line + ")");
48  
-		}
  50
+				names[func].push(item.name + " (" + item.line + ")");
  51
+			}
49 52
 
50  
-		for (var func in unused) {
51  
-			WScript.StdOut.WriteLine("    " + func + ": " + unused[func].join(", "));
  53
+			for (func in names) {
  54
+				lines.push("    " + func + ": " + names[func].join(", "));
  55
+			}
52 56
 		}
  57
+	};
53 58
 
54  
-		if (data.implieds && data.implieds.length) WScript.StdOut.WriteLine("");
55  
-	}
56  
-
57  
-	if (data.implieds && data.implieds.length) {
58  
-		exit = 1;
  59
+	// load JSHint if the two scripts have not been concatenated
  60
+	if (typeof JSHINT === "undefined") eval(new ActiveXObject("Scripting.FileSystemObject").OpenTextFile("..\\jshint.js", 1).ReadAll());
59 61
 
60  
-		WScript.StdOut.WriteLine("Implied globals:");
  62
+	var source = WScript.StdIn.ReadAll();
61 63
 
62  
-		var implieds = {};
  64
+	// trim junk character; not sure where it comes from
  65
+	JSHINT(source.substr(0, source.length - 1), { passfail: false });
63 66
 
64  
-		for (var i = 0; i < data.implieds.length; i++) {
65  
-			var item = data.implieds[i];
  67
+	var data = JSHINT.data();
  68
+	var lines = [];
66 69
 
67  
-			if (!(item.name in implieds)) implieds[item.name] = [];
  70
+	for (var formatter in formatters) {
  71
+		if (data[formatter]) {
  72
+			if (lines.length) lines.push("");
68 73
 
69  
-			implieds[item.name].push(item.line);
  74
+			formatters[formatter](data[formatter], lines);
70 75
 		}
  76
+	}
71 77
 
72  
-		for (var name in implieds) {
73  
-			WScript.StdOut.WriteLine("    " + name + ": " + implieds[name].join(", "));
  78
+	if (lines.length) {
  79
+		for (var i = 0; i < lines.length; i++) {
  80
+			WScript.StdOut.WriteLine(lines[i]);
74 81
 		}
75  
-	}
76 82
 
77  
-	WScript.Quit(exit);
  83
+		WScript.Quit(1);
  84
+	} else {
  85
+		WScript.Quit(0);
  86
+	}
78 87
 }());

0 notes on commit b0ba599

Please sign in to comment.
Something went wrong with that request. Please try again.