JSHint/JSLint

Static code analysis for JavaScript

Máté FARKAS
Budapest.js, 14/02/2012

Lint

JSLint

Created by
Douglas Crockford

YUI videos

JavaScript: The Good Parts

“The JavaScript Code Quality Tool”

First release: 2002

De facto standard for
good quality JS code

But there are alternatives, too: Doctor JS, Closure Linter, JavaScriptLint

MIT license

in 12/11/2010 moved to GitHub…

…then a new story began

JSHint

Forked by Anton Kovalyov
at 19/01/2011, because

“[JSLint] is quickly transforming from a tool that helps developers to prevent bugs to a tool that makes sure you write your code like Douglas Crockford.”

“Warning! JSLint will hurt your feelings.”

versus JSHint:

“Our goal is to help JavaScript developers write complex programs without worrying about typos and language gotchas.”

JSLint “The JavaScript Code Quality Tool”, while JSHint is

A JavaScript Code Quality Tool”

„The (Gentler) JavaScript Code Quality Tool”

„A Static Code Analysis Tool for JavaScript”

Contributors

JSLint: Douglas Crockford
(231 commits)

JSHint: 30+ including Wolfgang Kluge, Josh Perez, Douglas Crockford, Nicholas C. Zakas, Máté Farkas, etc., nearly 400 commits.

Enforcing options

bitwise: bitwise operators (|, &)
curly: curly braces around blocks in loops and conditionals
eqeqeq: use === and !==
forin: requires obj.hasOwnProperty check
immed: immediate function invocations only with wrapping parentheses
latedef: define the variable before using it
newcap: capitalize names of constructor functions
noarg: don't use arguments.caller, arguments.callee
noempty: don't use empty blocks
nonew: constructor functions must not have side effects
plusplus: don't use ++ and --
regexp: don't use '.' in regular expressions
undef: define all variables explicitly
strict: all functions must run in strict mode
trailing: don't put trailing whitespaces in your code

Relaxing Options

asi: allow missing semicolons
boss: allow assignments instead of comparisons
debug: allow debugger statements
eqnull: allow == null
es5: allow es5 features
evil: allow eval
expr: allow use expression instead of assignment or function call
funcscope: allow access to a variable ouside of its scope
globalstrict: allow global "use strict"
iterator: allow use of __iterator__ property
lastsemic: allow missing last semicolon
laxbreak: allow unsafe line breakings
laxcomma: allow comma-first coding style
loopfunc: allow functions inside loops
multistr: allow multiline strings
onecase: allow switch with only one case
proto: allow __proto__ property
regexdash: allow unescaped - in regular expressions
scripturl: allow script-targeted urls
smarttabs: allow tabs + spaces indentation
validthis: allow use of this in a non-constructor function in strict mode

Environments

browser: document, navigator, etc.
couch: CouchDB global variables
devel: console, alert
dojo: Dojo Toolkit global variables
jquery: jQuery global variables
mootols: MooTools global variables
node: Node.js environment
nonstandard: widely adopted non standard globals
prototypejs: Prototype global variables
rhino: Rhino environment
wsh: Windows Scripting Host

Legacy

nomen: allow dangling '_' in variables
onevar: allow only one var statement
passfail: stop on the first error or warning
white: enforce Douglas Crockford's coding style

Integration

Environments: Node.js, WSH, JavaScriptCore, Rhino

Editors: VIM, Emacs, Sublime Text 2, TextMate, Visual Studio, ShiftEdit, Komodo, Eclipse…

Easy to integrate with CI (Jenkins)

Lint or Hint?

JSLint: for generated code (CoffeeScript)

JSHint: for everything else

…or…

have the full control of the source code with a consistent coding style.