Add JSHint and make dygraphs pass its checks.
[dygraphs.git] / jshint / env / rhino.js
diff --git a/jshint/env/rhino.js b/jshint/env/rhino.js
new file mode 100644 (file)
index 0000000..f0529cd
--- /dev/null
@@ -0,0 +1,75 @@
+/*jshint boss: true, rhino: true */
+/*globals JSHINT*/
+
+(function (args) {
+    var filenames = [],
+        optstr, // arg1=val1,arg2=val2,...
+        predef, // global1=override,global2,global3,...
+        opts   = { rhino: true },
+        retval = 0;
+
+    args.forEach(function (arg) {
+        if (arg.indexOf("=") > -1) {
+            //first time it's the options
+            if (!optstr) {
+                optstr = arg;
+            } else if (!predef) {
+                predef = arg;
+            }
+        } else {
+            filenames.push(arg);
+        }
+    });
+
+    if (filenames.length === 0) {
+        print('Usage: jshint.js file.js');
+        quit(1);
+    }
+
+    if (optstr) {
+        optstr.split(',').forEach(function (arg) {
+            var o = arg.split('=');
+            opts[o[0]] = (function (ov) {
+                switch (ov) {
+                case 'true':
+                    return true;
+                case 'false':
+                    return false;
+                default:
+                    return ov;
+                }
+            }(o[1]));
+        });
+    }
+
+    if (predef) {
+        opts.predef = {};
+        predef.split(',').forEach(function (arg) {
+            var global = arg.split('=');
+            opts.predef[global[0]] = (function (override) {
+                return (override === 'false') ? false : true;
+            }(global[1]));
+        });
+    }
+
+    filenames.forEach(function (name) {
+
+        var input = readFile(name);
+
+        if (!input) {
+            print('jshint: Couldn\'t open file ' + name);
+            quit(1);
+        }
+
+        if (!JSHINT(input, opts)) {
+            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 = 1;
+        }
+    });
+
+    quit(retval);
+}(arguments));