Contents |
jQuery core passes JSHint (not JSLint) with a few stipulations. To run JSHint against jQuery you can download jQuery from Git and run 'make hint'.
We ignore the following warnings from JSHint:
parent.selectedIndex; (or similar) this is due to the fact that WebKit mis-reports the default selected property of an option, accessing the parent's selectedIndex property fixes it.
Array.prototype.slice.call( document.documentElement.childNodes, 0 )[0].node... - This check simply attempts to determine if, after a slice on a NodeList, the resulting collection still holds DOM nodes. This is a feature detection and is wrapped in a try/catch (if the property doesn't exist it'll throw an exception.
:only-child selector checks, reducing code and improving performance.
Additionally we enable the following two JSLint options by default:
Object.prototype as it's considered harmful.
Use tabs to indent your code.
Be sure to use liberal spacing in your code.
// No:
if(blah==="foo"){
foo("bar","baz",{zoo:1});
}
// Yes:
if ( blah === "foo" ) {
foo( "bar", "baz", {zoo: 1} );
}
Opening braces are always prefixed by a space.
Don't use tabs inside a line.
var a = true,
// yayy
c = false,
// grrr
b = false;
Lines with nothing on them should have no whitespace.
There should be no whitespace at the end of a line.
Long comments should use /* ... */.
Single line comments should always be on their own line and be above the line they reference. Additionally there should be an extra endline above it. For example:
var some = "stuff";
// We're going to loop here
for ( var i = 0; i < 10; i++ ) {}
Inline comments are allowed as an exception when used to annotate special arguments in formal parameter lists:
function foo( types, selector, data, fn, /*INTERNAL*/ one ) {
// do stuff.
}
Strict equality checks (===) should be used in favor of == wherever possible.
if/else/for/while/try always have braces and always go on multiple lines.
Braces should always be used on blocks.
// NO:
if ( true )
blah();
// YES:
if ( true ) {
blah();
}
Don't put statements on the same line as a conditional.
// NO:
if ( true ) return;
if ( true ) blah();
// YES:
if ( true ) {
return;
}
if ( true ) {
blah();
}
else/else if/catch go on the same line as the brace.
if ( blah ) {
baz();
} else {
baz2();
}
Exception: else if is allowed, a else { } is not required
} else if ( test ) {
blah();
}
// Not needed:
} else {
if ( test ) {
blah();
}
}
Don't use ternary operators instead of if/else statements.
Don't use object && object.method() instead of if/else statements (except in conditionals).
Always include extra spaces around the arguments.
foo( true );
Exception: foo(true) (no spaces) is allowed if you're already inside of another function call.
foo( bar(true) );
Exception: Functions, object literals, array literals and string literals go snug to front and back of the parentheses - but ONLY when it's the only argument.
foo(function() { });
foo({ });
foo([ ]);
foo(" ");
Exception: Multi-line function/object/array literals go snug at end (and at start ONLY if the only argument).
WRONG:
foo( true, { blah: "baz" });
RIGHT:
foo( true, {
blah: "baz"
});
Empty functions, object literals and array literals don't need filler spaces.
foo();
{}
[]
Always have spaces after commas and colons.
Empty objects and arrays don't need filler spaces.
[]
{}
Always have spaces after commas and colons.
Assignments should always have a semicolon after them.
Semicolons should always be followed by an endline.
Assignments in a declaration should always be on their own line. Declarations that don't have an assignment should be listed together at the start of the declaration. For example:
var a, b c,
test = true,
test2 = false;
typeof object === "string"
typeof object === "number"
typeof object === "boolean"
typeof object === "object"
jQuery.isPlainObject(object)
jQuery.isFunction(object)
jQuery.isArray(object)
object.nodeType
object === null
object == null
typeof variable === "undefined"
variable === undefined
object.prop === undefined
All RegExp operations should be done using .test() and .exec(). "string".match() is no longer used.
Strings should always use double-quotes instead of single-quotes.
.nodeName should always be used in favor of .tagName.
.nodeType should be used to determine the classification of a node (not .nodeName).
Don't attach expandos to nodes - only attach data using .data().