Skip to content
Browse files

JSHint config and cleanup

  • Loading branch information...
1 parent 36b4a68 commit c98c790a8cd6ec436a0d03d6085133f3d4f71aa9 @briancavalier briancavalier committed
Showing with 128 additions and 25 deletions.
  1. +73 −0 .jshintrc
  2. +4 −2 apply.js
  3. +25 −12 debug.js
  4. +2 −0 delay.js
  5. +7 −5 timeout.js
  6. +17 −6 when.js
View
73 .jshintrc
@@ -0,0 +1,73 @@
+{
+ // Settings
+ "passfail" : false, // Stop on first error.
+ "maxerr" : 20, // Maximum error before stopping.
+
+
+ // Predefined globals whom JSHint will ignore.
+ "browser" : true, // Standard browser globals e.g. `window`, `document`.
+
+ "node" : true,
+ "rhino" : false,
+ "couch" : false,
+ "wsh" : false, // Windows Scripting Host.
+
+ "jquery" : false,
+ "prototypejs" : false,
+ "mootools" : false,
+ "dojo" : false,
+
+ "predef" : [ // Custom globals.
+ "define",
+ "module"
+ ],
+
+
+ // Development.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "devel" : false, // Allow developments statements e.g. `console.log();`.
+
+
+ // ECMAScript 5.
+ "es5" : false, // Allow ECMAScript 5 syntax.
+ "strict" : false, // Require `use strict` pragma in every file.
+ "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
+
+
+ // The Good Parts.
+ "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.).
+ "boss" : true, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "curly" : true, // Require {} for every new block or scope.
+ "eqeqeq" : false, // Require triple equals i.e. `===`.
+ "eqnull" : true, // Tolerate use of `== null`.
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : true, // Tolerate `ExpressionStatement` as Programs.
+ "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : false, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : false, // Prohibit variable use before definition.
+ "loopfunc" : false, // Allow functions to be defined within loops.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "nonstandard" : false, // Defines non-standard but widely adopted globals such as escape and unescape.
+ "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions.
+ "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
+ "scripturl" : true, // Tolerate script-targeted URLs.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+
+
+ // Personal styling preferences.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noempty" : false, // Prohibit use of empty blocks.
+ "nonew" : true, // Prohibit use of constructors for side-effects.
+ "nomen" : false, // Prohibit use of initial or trailing underbars in names.
+ "onevar" : false, // Allow only one `var` statement per function.
+ "plusplus" : false, // Prohibit use of `++` & `--`.
+ "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "trailing" : true, // Prohibit trailing whitespaces.
+ "white" : false, // Check against strict whitespace and indentation rules.
+ "indent" : 4, // Specify indentation spacing
+ "smarttabs" : true // Suppresses warnings about mixed tabs and spaces when the latter are used for alignmnent only
+}
View
6 apply.js
@@ -49,10 +49,12 @@ define(function() {
*/
return function(array) {
// It better be an array
- if(toString.call(array) != '[object Array]') throw new Error('apply called with non-array arg');
+ if(toString.call(array) != '[object Array]') {
+ throw new Error('apply called with non-array arg');
+ }
return f.apply(null, array);
- }
+ };
};
});
View
37 debug.js
@@ -1,5 +1,8 @@
/** @license MIT License (c) copyright B Cavalier & J Hann */
+/*jshint devel: true*/
+/*global console:true, setTimeout:true*/
+
/**
* This is a drop-in replacement for the when module that sets up automatic
* debug output for promises created or consumed by when.js. Use this
@@ -78,7 +81,9 @@ define(['./when'], function(when) {
}
function wrapCallback(cb) {
- if(typeof cb != 'function') return cb;
+ if(typeof cb != 'function') {
+ return cb;
+ }
return function(v) {
try {
@@ -96,16 +101,16 @@ define(['./when'], function(when) {
throw err;
}
- }
+ };
}
- function wrapCallbacks(arguments) {
+ function wrapCallbacks(callbacks) {
var cb, args, len, i;
args = [];
- for(i = 0, len = arguments.length; i < len; i++) {
- args[i] = typeof (cb = arguments[i]) == 'function'
+ for(i = 0, len = callbacks.length; i < len; i++) {
+ args[i] = typeof (cb = callbacks[i]) == 'function'
? wrapCallback(cb)
: cb;
}
@@ -123,7 +128,11 @@ define(['./when'], function(when) {
*/
function toString(name, id, status, value) {
var s = '[object ' + name + ' ' + id + '] ' + status;
- if(value !== pending) s += ': ' + value;
+
+ if(value !== pending) {
+ s += ': ' + value;
+ }
+
return s;
}
@@ -163,7 +172,9 @@ define(['./when'], function(when) {
// if no id provided, generate one. Not sure if this is
// useful or not.
id = arguments[arguments.length - 1];
- if(id === undef) id = ++promiseId;
+ if(id === undef) {
+ id = ++promiseId;
+ }
// Promise and resolver are frozen, so have to delegate
// in order to setup toString() on promise, resolver,
@@ -243,15 +254,17 @@ define(['./when'], function(when) {
whenDebug.defer = deferDebug;
whenDebug.isPromise = when.isPromise;
+ function makeDebug(name, func) {
+ whenDebug[name] = function() {
+ return debugPromise(func.apply(when, arguments));
+ };
+ }
+
// For each method we haven't already replaced, replace it with
// one that sets up debug logging on the returned promise
for(var p in when) {
if(when.hasOwnProperty(p) && !(p in whenDebug)) {
- (function(p, orig) {
- whenDebug[p] = function() {
- return debugPromise(orig.apply(when, arguments));
- };
- })(p, when[p]);
+ makeDebug(p, when[p]);
}
}
View
2 delay.js
@@ -1,5 +1,7 @@
/** @license MIT License (c) copyright B Cavalier & J Hann */
+/*global setTimeout:true*/
+
/**
* delay.js
*
View
12 timeout.js
@@ -1,5 +1,7 @@
/** @license MIT License (c) copyright B Cavalier & J Hann */
+/*global setTimeout:true, clearTimeout:true*/
+
/**
* timeout.js
*
@@ -33,17 +35,17 @@ define(['./when'], function(when) {
* @returns {Promise}
*/
return function timeout(promise, msec) {
- var deferred, timeout;
+ var deferred, timeoutRef;
deferred = when.defer();
- timeout = setTimeout(function onTimeout() {
- timeout && deferred.reject(new Error('timed out'));
+ timeoutRef = setTimeout(function onTimeout() {
+ timeoutRef && deferred.reject(new Error('timed out'));
}, msec);
function cancelTimeout() {
- clearTimeout(timeout);
- timeout = undef;
+ clearTimeout(timeoutRef);
+ timeoutRef = undef;
}
when(promise,
View
23 when.js
@@ -325,7 +325,10 @@ define(function() { "use strict";
*/
_progress = function(update) {
var progress, i = 0;
- while (progress = progressHandlers[i++]) progress(update);
+
+ while (progress = progressHandlers[i++]) {
+ progress(update);
+ }
};
/**
@@ -555,8 +558,9 @@ define(function() { "use strict";
// Since mapFunc may be async, get all invocations of it into flight
// asap, and then use reduce() to collect all the results
for(i = 0; i < len; i++) {
- if(i in promisesOrValues)
+ if(i in promisesOrValues) {
results[i] = when(promisesOrValues[i], mapFunc);
+ }
}
// Could use all() here, but that would result in another array
@@ -617,7 +621,9 @@ define(function() { "use strict";
}
];
- if (arguments.length > 2) args.push(initialValue);
+ if (arguments.length > 2) {
+ args.push(initialValue);
+ }
return reduceArray.apply(promisesOrValues, args);
}
@@ -659,9 +665,13 @@ define(function() { "use strict";
*/
function checkCallbacks(start, arrayOfCallbacks) {
var arg, i = arrayOfCallbacks.length;
+
while(i > start) {
arg = arrayOfCallbacks[--i];
- if (arg != null && typeof arg != 'function') throw new Error('arg '+i+' must be a function');
+
+ if (arg != null && typeof arg != 'function') {
+ throw new Error('arg '+i+' must be a function');
+ }
}
}
@@ -686,7 +696,7 @@ define(function() { "use strict";
var arr, args, reduced, len, i;
i = 0;
- arr = Object(this);
+ arr = new Object(this);
len = arr.length >>> 0;
args = arguments;
@@ -714,8 +724,9 @@ define(function() { "use strict";
// Do the actual reduce
for(;i < len; ++i) {
// Skip holes
- if(i in arr)
+ if(i in arr) {
reduced = reduceFunc(reduced, arr[i], i, arr);
+ }
}
return reduced;

0 comments on commit c98c790

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