Skip to content
100755 699 lines (657 sloc) 37.5 KB
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
1 <html>
2 <head>
3 <title>JSLint: The JavaScript Code Quality Tool</title>
4 <link rel="icon" type="image/gif" href="">
5 <style>
6 body {
7 background-color: linen;
8 margin-left: 8%;
9 margin-right: 8%;
10 }
11 pre {margin-left: 40px;}
12 table {margin: 10px; border: 0px;}
13 th, td {border: black solid 1pt; padding-left: 10px;
14 padding-right: 10px; vertical-align: top;}
15 th {
16 background-color: thistle;
17 }
18 td {
19 background-color: white;
20 }
21 #top table {margin: 0px;}
22 #top td {background-color: linen; border: 0pt; vertical-align: middle;}
23 ul {list-style-type: square;}
24 input[type="button"] {border: 2px solid black;}
25 a:link {color: darkblue;}
26 a:visited {color: purple;}
27 a:hover {color: blue; text-decoration: underline;}
28 a:active {color: red;}
29 </style>
30 </head>
31 <body bgcolor="gainsboro">
32 <table id="top" border="0">
33 <tr>
34 <td><img src="jslint.gif" width="383" height="120" alt="JSLint"> </td>
35 <td>
36 <p><big><code>JSLint</code>: The
37 <a href="">JavaScript</a> Code Quality Tool</big></p>
38 <p><a href="" target="_top">&copy;2002 Douglas Crockford</a></p>
39 </td>
40 </tr>
41 </table>
42 <br clear="all">
43 <h2 id=warning>Warning!</h2>
44 <p><a href="" target="_blank"><code>JSLint</code></a>
45 will hurt your feelings.</p>
46 <h2 id=what>What is <code>JSLint</code>?</h2>
48 <p><a href="" target="_blank"><code>JSLint</code></a>
49 is a JavaScript program that looks for problems in JavaScript programs.
50 It is a code quality tool.</p>
52 <p>When <a href="">C</a>
53 was a <a href="">young</a>
54 programming language, there were several common programming errors that
55 were not caught by the primitive compilers, so an accessory program called
56 <code><a href="">lint</a></code>
57 was developed that would scan a source file, looking for problems.</p>
59 <p>As the language matured, the definition of the language was
60 strengthened to eliminate some insecurities, and compilers got better
61 at issuing warnings. <code>lint</code> is no longer needed.</p>
63 <p><a href="">JavaScript</a> is a young-for-its-age
64 language. It was originally intended to do small tasks in webpages, tasks
65 for which Java was too heavy and clumsy. But JavaScript is a very capable
66 language, and it is now being used in larger projects. Many of the features
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
67 that were intended to make the language easy to use are troublesome when projects become complicated. A <code>lint</code> for JavaScript is needed: <a href=""><code>JSLint</code></a>,
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
68 a JavaScript syntax checker and validator.</p>
70 <p><code>JSLint</code> takes a JavaScript source and scans it. If it finds
71 a problem, it returns a message describing the problem and an approximate
72 location within the source. The problem is not necessarily a syntax error,
73 although it often is. <code>JSLint</code> looks at some style conventions
74 as well as structural problems. It does not prove that your program is
75 correct. It just provides another set of eyes to help spot problems.</p>
77 <p><code>JSLint</code> defines a professional subset of JavaScript, a stricter
78 language than that defined by <a href="" target="ecma">Third
79 Edition of the <i>ECMAScript Programming Language Standard</i></a>. The
80 subset is related to recommendations found in <a href="" target="sun"><i>Code
81 Conventions for the JavaScript Programming Language</i></a>. </p>
82 <p>JavaScript is a sloppy language, but inside it there is an elegant, better
83 language. <code>JSLint</code> helps you to program in that better language
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
84 and to avoid most of the slop. JSLint will reject programs that browsers will accept because JSLint is concerned with the quality of your code and browsers are not. You should accept all of JSLint's advice.</p>
85 <p><code>JSLint</code> can operate on JavaScript source, HTML source, CSS source, or <a href="">JSON</a>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
86 text.</p>
87 <h2 id=global>Global Variables</h2>
88 <p>JavaScript's <a href="">biggest
89 problem</a> is its dependence on global variables, particularly implied
90 global variables. If a variable is not explicitly declared (usually with
91 the <code>var</code> statement), then JavaScript assumes that the variable
92 was global. This can mask misspelled names and other problems.</p>
93 <p><code>JSLint</code> expects that all variables and functions are declared
94 before they are used or invoked. This allows it to detect implied global
95 variables. It is also good practice because it makes programs easier to
96 read.</p>
97 <p>Sometimes a file is dependent on global variables and functions that
98 are defined elsewhere. You can identify these to <code>JSLint</code> with a <code>var</code> statement that lists the global functions and objects
99 that your program depends on. </p>
100 <p>A global declaration can look like this:</p>
101 <pre>var getElementByAttribute, breakCycles, hanoi;</pre>
102 <p>The declaration should appears near the top of the file. It must appear before the use of the variables
103 it declares. </p>
104 <p>It is necessary to use a <code>var</code> statement to declare a variable before that variable is assigned to. </p>
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
105 <p><code>JSLint</code> also recognizes a <code>/*global */</code> comment that can indicate to <code>JSLint</code> that variables used in this file were defined in other files. The comment can contain a comma separated list of names. Each name can optionally be followed by a colon and either <code>true</code> or <code>false</code>, <code>true</code> indicated that the variable may be assigned to by this file, and <code>false</code> indicating that assignment is not allowed (which is the default).</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
106 <p id=browser>Some globals can be predefined for you. Select the <i>Assume
107 a browser</i> (<code>browser</code>) <a href="#options">option</a> to
108 predefine the standard global properties that are supplied by web browsers,
109 such as <code>document</code> and <code>addEventListener</code>. It has the same
110 effect as this comment:</p>
111 <blockquote>
112 <code>/*global addEventListener: false, blur: false, clearInterval: false, clearTimeout: false, close: false, closed: false, defaultStatus: false, document: false, event: false, focus: false, frames: false, getComputedStyle: false, history: false, Image: false, length: false, location: false, moveBy: false, moveTo: false, name: false, navigator: false, onblur: true, onerror: true, onfocus: true, onload: true, onresize: true, onunload: true, open: false, opener: false, Option: false, parent: false, print: false, resizeBy: false, resizeTo: false, screen: false, scroll: false, scrollBy: false, scrollTo: false, setInterval: false, setTimeout: false, status: false, top: false, XMLHttpRequest: false */</code></blockquote>
113 <p> The <code>browser</code> <a href="#options">option</a> does
114 not include the aliases of the global object, <code>window</code> and
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
115 <code>self</code> because the potential for misuse is so great.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
116 <p id=devel>Select the
117 <label for="JSLINT_DEVEL" title="devel"><em>Assume console, alert, ...</em></label>
118 (<code>devel</code>) <a href="#options">option</a> to predefine globals that are useful in development but that should be avoided in production, such as <code>console</code> and <code>alert</code>. It has the same
119 effect as this comment:</p>
120 <pre>/*global alert: false, confirm: false, console: false, Debug: false, opera: false, prompt: false */</pre>
121 <p id=rhino>Select the <i>Assume Rhino</i> (<code>rhino</code>) <a href="#options">option</a>
122 to predefine the global properties provided by the Rhino environment.
123 It has the same effect as this statement:</p>
124 <blockquote>
125 <code>/*global defineClass: false, deserialize: false, gc: false, help: false, load: false, loadClass: false, print: false, quit: false, readFile: false, readUrl: false, runCommand: false, seal: false, serialize: false, spawn: false, sync: false, toint32: false, version: false */ </code>
126 </blockquote>
127 <p id=widget>Select the <i>Assume a Yahoo Widget</i> (<code>widget</code>)
128 <a href="#options">option</a> to predefine the global properties provided
129 by the Yahoo! Widgets environment. It has the same effect as this statement:</p>
130 <blockquote>
131 <code>/*global alert: true, animator: true, appleScript: true, beep: true, bytesToUIString: true, Canvas: true, chooseColor: true, chooseFile: true, chooseFolder: true, closeWidget: true, COM: true, convertPathToHFS: true, convertPathToPlatform: true, CustomAnimation: true, escape: true, FadeAnimation: true, filesystem: true, Flash: true, focusWidget: true, form: true, FormField: true, Frame: true, HotKey: true, Image: true, include: true, isApplicationRunning: true, iTunes: true, konfabulatorVersion: true, log: true, md5: true, MenuItem: true, MoveAnimation: true, openURL: true, play: true, Point: true, popupMenu: true, preferenceGroups: true, preferences: true, print: true, prompt: true, random: true, Rectangle: true, reloadWidget: true, ResizeAnimation: true, resolvePath: true, resumeUpdates: true, RotateAnimation: true, runCommand: true, runCommandInBg: true, saveAs: true, savePreferences: true, screen: true, ScrollBar: true, showWidgetPreferences: true, sleep: true, speak: true, Style: true, suppressUpdates: true, system: true, tellWidget: true, Text: true, TextArea: true, Timer: true, unescape: true, updateNow: true, URL: true, Web: true, widget: true, Window: true, XMLDOM: true, XMLHttpRequest: true, yahooCheckLogin: true, yahooLogin: true, yahooLogout: true */</code>
132 </blockquote>
133 <p id=windows>Select the <i>Assume Windows</i> (<code>windows</code>)
134 <a href="#options">option</a> to predefine the global properties provided by Microsoft Windows. It has the same effect as this statement:</p>
135 <blockquote>
136 <p><code>/*global ActiveXObject: false, CScript: false, Debug: false, Enumerator: false, System: false, VBArray: false, WScript: false */</code></p>
137 </blockquote>
138 <h2 id=semicolon>Semicolon</h2>
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
139 <p>JavaScript uses a C-like syntax which requires the use of semicolons to delimit certain
140 statements. JavaScript attempts to make those semicolons optional with a semicolon
141 insertion mechanism. This is dangerous because it can mask errors.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
142 <p>Like C, JavaScript has <code>++</code> and <code>--</code> and <code>(</code> operators
143 which can be prefixes or suffixes. The disambiguation is done by the semicolon.</p>
144 <p>In JavaScript, a linefeed can be whitespace or it can act as a semicolon.
145 This replaces one ambiguity with another. </p>
146 <p><code>JSLint</code> expects that every statement be followed by <code>;</code> except
147 for <code>for</code>, <code>function</code>, <code>if</code>, <code>switch</code>, <code>try</code>, and
148 <code>while</code>. <code>JSLint</code> does not expect to see unnecessary semicolons or the
149 empty statement.</p>
150 <h2 id=comma>Comma</h2>
151 <p>The comma operator can lead to excessively tricky expressions. It can also
152 mask some programming errors.</p>
153 <p><code>JSLint</code> expects to see the comma used as a separator, but not as an
154 operator (except in the initialization and incrementation parts of the <code>for</code>
155 statement). It does not expect to see elided elements in array literals. Extra
156 commas should not be used. A comma should not appear after the last element
157 of an array literal or object literal because it can be misinterpreted by some
158 browsers. </p>
159 <h2 id=scope>Scope</h2>
161 <p>In many languages, a block introduces a scope. Variables introduced in
162 a block are not visible outside of the block.</p>
164 <p>In JavaScript, blocks do not introduce a scope. There is only function-scope.
165 A variable introduced anywhere in a function is visible everywhere in
166 the function. JavaScript's blocks confuse experienced programmers and
167 lead to errors because the familiar syntax makes a false promise.</p>
169 <p><code>JSLint</code> expects blocks with <code>function</code>, <code>if</code>,
170 <code>switch</code>, <code>while</code>, <code>for</code>, <code>do</code>,
171 and <code>try</code> statements and nowhere else. </p>
172 <p>In languages with block scope, it is usually recommended that variables
173 be declared at the site of first use. But because JavaScript does not
174 have block scope, it is wiser to declare all of a function's variables
175 at the top of the function. It is recommended that a single <code>var</code>
176 statement be used per function. This can be enforced with the <code>onevar</code>
177 <a href="#options">option</a>.</p>
179 <h2 id=required>Required Blocks</h2>
181 <p><code>JSLint</code> expects that <code>if</code>, <code>while</code>,
182 <code>do</code> and <code>for</code> statements will be made with blocks
183 <code>{</code>that is, with statements enclosed in braces<code>}</code>.</p>
185 <p>JavaScript allows an <code>if</code> to be written like this:</p>
187 <pre>if (<i>condition</i><code>)
188 </code><i>statement</i>;</pre>
190 <p>That form is known to contribute to mistakes in projects where many programmers
191 are working on the same code. That is why <code>JSLint</code> expects the use of
192 a block:</p>
194 <pre>if (<i>condition</i>) {
195 <i>statements</i>;
196 }</pre>
198 <p>Experience shows that this form is more resilient.</p>
200 <h2 id=expression>Expression Statements</h2>
201 <p>An expression statement is expected to be an assignment or a function/method
202 call or <code>delete</code>. All other expression statements are considered
203 to be errors.</p>
204 <h2 id=forin><code>for</code> <code>in</code></h2>
205 <p>The <code>for</code> <code>in</code> statement allows for looping through
206 the names of all of the properties of an object. <a href="">Unfortunately,
207 it also loops through all of the members which were inherited through
208 the prototype chain.</a> This has the bad side effect of serving up method
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
209 functions when the interest is in data members. If a program is written without awareness of this situation, then it can fail.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
210 <p>The body of every <code>for</code> <code>in</code> statement should be
211 wrapped in an <code>if</code> statement that does filtering. It can select
212 for a particular type or range of values, or it can exclude functions,
213 or it can exclude properties from the prototype. For example,</p>
214 <pre>for (name in object) {
215 if (object.hasOwnProperty(name)) {
216 ....
217 }
219 }</pre>
221 <h2 id=switch><code>switch</code></h2>
222 <p>A <a href="">common
223 error</a> in <code>switch</code> statements is to forget to place a <code>break</code>
224 statement after each case, resulting in unintended fall-through. <code>JSLint</code>
225 expects that the statement before the next <code>case</code> or <code>default</code>
226 is one of these: <code>break</code>, <code>return</code>, or <code>throw</code>.
227 </p>
228 <h2 id=var><code>var</code></h2>
230 <p>JavaScript allows <code>var</code> definitions to occur anywhere
231 within a function. <code>JSLint</code> is more strict.</p>
233 <p><code>JSLint</code> expects that a <code>var</code> will be declared
234 only once, and that it will be declared before it is used.</p>
235 <p><code></code><code>JSLint</code> expects that a <code>function</code>
236 will be declared before it is used.</p>
237 <p><code>JSLint</code> expects that parameters will not also be declared
238 as vars. </p>
240 <p><code>JSLint</code> does not expect the <code>arguments</code> array to be declared
241 as a <code>var</code>.</p>
242 <p><code>JSLint</code> does not expect that a var will be defined in a block.
243 This is because JavaScript blocks do not have block scope. This can have
244 unexpected consequences. Define all variables at the top of the function.</p>
246 <h2 id=with><code>with</code></h2>
248 <p>The <code>with</code> statement was intended to provide a shorthand in accessing
249 members in deeply nested objects. Unfortunately, it behaves <a href="">very
250 badly</a> when setting new members. Never use the <code>with</code> statement. Use
251 a <code>var</code> instead.</p>
253 <p><code>JSLint</code> does not expect to see a <code>with</code> statement.</p>
255 <h2 id=assignment>=</h2>
256 <p><code>JSLint</code> does not expect to see an assignment statement in
257 the condition part of an <code>if</code> or <code>for</code> or <code>while</code>
258 <code></code> or <code>do</code> statement. This is because it is more
259 likely that </p>
260 <pre>if (a = b) {
261 ...
262 }</pre>
263 <p>was intended to be </p>
264 <pre>if (a == b) {
265 ...
266 }</pre>
267 <p>It is difficult to write correct programs while using idioms that are
268 hard to distinguish from obvious errors. If you really intend an assignment,
269 wrap it in another set of parens:</p>
270 <pre>if ((a = b)) {
271 ...
272 }</pre>
274 <h2 id=equal>== and !=</h2>
275 <p>The <code>==</code> and <code>!=</code> operators do type coercion before
276 comparing. This is bad because it causes <code>' \t\r\n' == 0</code> to
277 be <code>true</code>. This can mask type errors.</p>
278 <p>When comparing to any of the following values, use the <code>===</code>
279 or <code>!==</code> operators (which do not do type coercion): <code>0
280 '' undefined null false true</code></p>
281 <p align="left">If you only care that a value is <i>truthy</i> or <i>falsy</i>,
282 then use the short form. Instead of </p>
283 <pre align="left">(foo != 0)</pre>
284 <p align="left">just say </p>
285 <pre align="left">(foo)</pre>
286 <p align="left">and instead of</p>
287 <pre align="left">(foo == 0)</pre>
288 <p align="left"> say</p>
289 <pre align="left">(!foo)</pre>
5675d2c @douglascrockford
authored Jan 7, 2011
290 <p>Always use the <code>===</code> and <code>!==</code> operators. </p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
291 <h2 id=labels>Labels</h2>
292 <p>JavaScript allows any statement to have a label, and labels have a
293 separate name space. <code>JSLint</code> is more strict.</p>
295 <p><code>JSLint</code> expects labels only on statements that interact
296 with <code>break</code>: <code>switch</code>, <code>while</code>,
297 <code>do</code>, and <code>for</code>. <code>JSLint</code> expects that labels
298 will be distinct from vars and parameters.</p>
300 <h2 id=unreachable>Unreachable Code</h2>
301 <p><code>JSLint</code> expects that
302 a <code>return</code>, <code>break</code>, <code>continue</code>,
303 or <code>throw</code> statement will be followed by
304 a <code>}</code> or <code>case</code> or <code>default</code>.</p>
306 <h2 id=pluses>Confusing Pluses and Minuses</h2>
308 <p><code>JSLint</code> expects that <code>+</code> will not be followed by
309 <code>+</code> or <code>++</code>, and that <code>-</code> will not be followed
310 by <code>-</code> or <code>--</code>. A misplaced space can turn <code>+ +</code> into <code>++</code>, an error that is difficult to see. Use parens to avoid confusion..</p>
311 <h2 id=inc><code>++</code> and <code>--</code></h2>
312 <p>The <code>++</code> <small>(increment)</small> and <code>--</code> <small>(decrement)</small>
313 operators have been known to contribute to bad code by encouraging excessive
314 trickiness. They are second only to faulty architecture in enabling to
315 viruses and other security menaces. There is a <code>plusplus</code> <a href="#options">option</a>
316 that prohibits the use of these operators.</p>
317 <h2 id=bitwise>Bitwise Operators</h2>
318 <p>JavaScript does not have an integer type, but it does have bitwise operators.
319 The bitwise operators convert their operands from floating point to integers
320 and back, so they are not as efficient as in C or other languages. They
321 are rarely useful in browser applications. The similarity to the logical
322 operators can mask some programming errors. The <code>bitwise</code> <a href="#options">option</a>
323 prohibits the use of these operators: <code>&lt;&lt; &gt;&gt; &gt;&gt;&gt;
324 ~ &amp; |</code>.</p>
325 <h2 id=evil><code>eval</code> is evil</h2>
326 <p>The <code>eval</code> function (and its relatives, <code>Function</code>,
327 <code>setTimeout</code>, and <code>setInterval</code>) provide access
328 to the JavaScript compiler. This is sometimes necessary, but in most cases
329 it indicates the presence of extremely bad coding. The <code>eval</code>
330 function is the most misused feature of JavaScript.</p>
332 <h2 id=void><code>void</code></h2>
333 <p>In most C-like languages, <code>void</code> is a type. In
334 JavaScript, <code>void</code> is a prefix operator that always
335 returns <code>undefined</code>. <code>JSLint</code> does not expect to
336 see <code>void</code> because it is confusing and not very useful.</p>
338 <h2 id=regexp>Regular Expressions</h2>
339 <p>Regular expressions are written in a terse and cryptic notation. <code>JSLint</code>
340 looks for problems that may cause portability problems. It also attempts
341 to resolve visual ambiguities by recommending explicit escapement.</p>
342 <p>JavaScript's syntax for regular expression literals overloads the <code>/</code>
343 character. To avoid ambiguity, <code>JSLint</code> expects that the character
344 preceding a regular expression literal is a <code>(</code> or <code>=</code>
345 or <code>:</code> or <code>,</code> character. </p>
346 <h2 id=new>Constructors and <code>new</code></h2>
347 <p>Constructors are functions that are designed to be used with the <code>new</code>
348 prefix. The <code>new</code> prefix creates a new object based on the
349 function's <code>prototype</code>, and binds that object to the function's
350 implied <code>this</code> parameter. If you neglect to use the <code>new</code>
351 prefix, no new object will be made and <code>this</code> will be bound
352 to the global object. This is a <a href="">serious
353 mistake</a>.</p>
354 <p><code>JSLint</code> enforces the convention that constructor functions
355 be given names with initial uppercase. <code>JSLint</code> does not expect
356 to see a function invocation with an initial uppercase name unless it
357 has the <code>new</code> prefix. <code>JSLint</code> does not expect to
358 see the <code>new</code> prefix used with functions whose names do not
359 start with initial uppercase. This can be controlled with the <code>newcap</code>
360 <a href="#options">option</a>.</p>
361 <p><code>JSLint</code> does not expect to see the wrapper forms <code>new Number</code>,
362 <code>new String</code>, <code>new Boolean</code>. </p>
363 <p><code>JSLint</code> does not expect to see <code>new Object</code> (use <code>{}</code>
364 instead). </p>
365 <p><code>JSLint</code> does not expect to see <code>new Array</code> (use <code>[]</code>
366 instead).</p>
367 <h2 id=unsafe>Unsafe Characters</h2>
368 <p> There are characters that are handled inconsistently in browsers, and
369 so must be escaped when placed in strings. </p>
370 <pre>\u0000-\u001f
371 \u007f-\u009f
372 \u00ad
373 \u0600-\u0604
374 \u070f
375 \u17b4
376 \u17b5
377 \u200c-\u200f
378 \u2028-\u202f
379 \u2060-\u206f
380 \ufeff
381 \ufff0-\uffff</pre>
382 <h2 id=not>Not Looked For</h2>
384 <p><code>JSLint</code> does not do flow analysis to determine that variables are assigned
385 values before used. This is because variables are given a value (<code>undefined</code>)
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
386 that is a reasonable default for many applications.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
388 <p><code>JSLint</code> does not do any kind of global analysis. It does
389 not attempt to determine that functions used with <code>new</code> are
390 really constructors (<a href="#new">except by enforcing capitalization
391 conventions</a>), or that property names are spelled correctly (<a href="#members">except
392 for matching against the <code>/*members */</code> comment</a>).</p>
393 <h2 id=html>HTML</h2>
394 <p><code>JSLint</code> is able to handle HTML text. It can inspect the JavaScript content
395 contained within <code>&lt;script&gt;</code>...<code>&lt;/script&gt;</code> tags. It
396 also inspects the HTML content, looking for problems that are known to interfere
397 with JavaScript:</p>
398 <ul>
399 <li>All tag names must be in lower case.</li>
400 <li>All tags that can take a close tag (such as <code>&lt;/p&gt;</code>)
401 must have a close tag.</li>
402 <li>All tags are correctly nested.</li>
403 <li>The entity <code>&amp;lt;</code> must be used for literal <code>'&lt;'</code>.</li>
404 </ul>
405 <p><code>JSLint</code> is less anal than the sycophantic conformity demanded
406 by XHTML, but more strict than the popular browsers. </p>
407 <p><code>JSLint</code> also checks for the occurrence of<code> '&lt;/' </code>in
408 string literals. You should always write<code> '&lt;\/' </code>instead.
409 The extra backslash is ignored by the JavaScript compiler but not by the
410 HTML parser. Tricks like this should not be necessary, and yet they are.</p>
411 <p>There is a <code>cap</code> <a href="#options">option</a> that allows
412 use of upper case tag names. There is also an <code>on</code> <a href="#options">option</a>
413 that allows the use of inline HTML event handlers.</p>
414 <p>There is a <code>fragment</code> <a href="#options">option</a> that can
415 inspect a well formed HTML fragment. If the <code>adsafe</code> <a href="#options">option</a>
416 is also used, then the fragment must be a <code>&lt;div&gt;</code> that
417 conforms to the <a href="">ADsafe</a> widget rules.</p>
418 <h2 id=css>CSS</h2>
419 <p><code>JSLint</code> can inspect CSS files. It expects the first line
420 of a CSS file to be </p>
421 <pre>@charset &quot;UTF-8&quot;;</pre>
422 <p>This feature is experimental. Please report any problems or limitations.
423 There is a <code>css</code> <a href="#options">option</a> that will tolerate
424 some of the non-standard-but-customary workarounds. <br>
425 </p>
427 <h2 id=options>Options</h2>
428 <p><code>JSLint</code> provides several options that control its operation and
429 its sensitivity. In the <a href="">web edition</a>, the
430 options are selected with several checkboxes and two fields. Clicking on
431 the <a href=""
432 target="_blank"><input type="button" value="Good Parts"></a> button will give
433 you the ideal settings.
434 </p>
435 <p>It also provides assistance in constructing <code>/*jslint*/</code>
437 </p>
438 <p>When <code>JSLINT</code> is called as a function, it accepts an <code>option</code> object
439 parameter that allows you to determine the subset of JavaScript that is
440 acceptable to you. The web page version of <code>JSLint</code> at <a href=""></a>
441 does this for you. </p>
442 <p>Options can also be specified within a script with a <code>/*jslint */</code>
443 comment:</p>
444 <pre>/*jslint nomen: true, debug: true,
445 evil: false, onevar: true */</pre>
446 <p>An option specification starts with <code>/*jslint</code>. Notice that
447 there is no space before the <code>j</code>. The specification contains
448 a sequence of name value pairs, where the names are <code>JSLint</code>
449 options, and the values are <code>true</code> or <code>false</code>. The
450 <code>indent</code> <a href="#options">option</a> can take a number. A <code>/*jslint */</code>
451 comment takes precedence over the <code>option</code> object. </p>
452 <table>
453 <tbody>
454 <tr>
455 <th>Description</th>
456 <th><code>option</code></th>
457 <th>Meaning</th>
458 </tr>
459 <tr>
460 <td>ADsafe</td>
461 <td><code>adsafe</code></td>
462 <td><code>true</code> if <a href="">AD<span style="color: blue;">safe</span></a>
463 rules should be enforced. See <a href=""></a>.</td>
464 </tr>
465 <tr>
466 <td>Disallow bitwise operators </td>
467 <td><code>bitwise</code></td>
468 <td><code>true</code> if bitwise operators should not be allowed. <a href="#bitwise"><small>(more)</small></a></td>
469 </tr>
470 <tr>
471 <td>Assume a browser </td>
472 <td><code>browser</code></td>
473 <td><code>true</code> if the standard browser globals should be predefined.
474 <a href="#browser"><small>(more)</small></a> </td>
475 </tr>
476 <tr>
477 <td>Tolerate HTML case </td>
478 <td><code>cap</code></td>
479 <td><code>true</code> if upper case HTML should be allowed.</td>
480 </tr>
481 <tr>
482 <td>Tolerate CSS workarounds</td>
483 <td><code>css</code></td>
484 <td><code>true</code> if CSS workarounds should be tolerated. <a href="#css"><small>(more)</small></a></td>
485 </tr>
486 <tr>
487 <td>Tolerate debugger statements</td>
488 <td><code>debug</code></td>
489 <td><code>true</code> if <code>debugger</code> statements should be
490 allowed. Set this option to <code>false</code> before going into production.</td>
491 </tr>
492 <tr>
493 <td>Assume <code>console</code>, <code>alert</code>, ...</td>
494 <td><code>devel</code></td>
495 <td><code>true</code> if browser globals that are useful in development should be
496 predefined. (<a href="#devel">more</a>)</td>
497 </tr>
498 <tr>
5675d2c @douglascrockford
authored Jan 8, 2011
499 <td>Tolerate ES5 syntax</td>
500 <td><code>es5</code></td>
501 <td><code>true</code> if ES5 syntax should be allowed.</a></td>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
502 </tr>
503 <tr>
504 <td>Tolerate <code>eval</code> </td>
505 <td><code>evil</code></td>
506 <td><code>true</code> if <code>eval</code> should be allowed. <a href="#evil"><small>(more)</small></a></td>
507 </tr>
508 <tr>
509 <td>Tolerate unfiltered for in </td>
510 <td><code>forin</code></td>
511 <td><code>true</code> if unfiltered <code>for</code> <code>in</code>
512 statements should be allowed. <a href="#forin"><small>(more)</small></a></td>
513 </tr>
514 <tr>
515 <td>Tolerate HTML fragments </td>
516 <td><code>fragment</code></td>
517 <td><code>true</code> if HTML fragments should be allowed. <a href="#html"><small>(more)</small></a></td>
518 </tr>
519 <tr>
5675d2c @douglascrockford
authored Jan 8, 2011
520 <td>Strict white space indentation</td>
521 <td><code>indent</code></td>
522 <td>The number of spaces used for indentation (default is 4)</td>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
523 </tr>
524 <tr>
5675d2c @douglascrockford
authored Jan 8, 2011
525 <td>Maximum number of errors</td>
526 <td><code>maxerr</code></td>
527 <td>The maximum number of warnings reported (default is 50)</td>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
528 </tr>
529 <tr>
530 <td>Maximum line length</td>
531 <td><code>maxlen</code></td>
532 <td>The maximum number of characters in a line</td>
533 </tr>
534 <tr>
535 <td>Disallow dangling _ in identifiers </td>
536 <td><code>nomen</code></td>
537 <td><code>true</code> if names should be checked for initial or trailing underbars</td>
538 </tr>
539 <tr>
540 <td>Require Initial Caps for constructors </td>
541 <td><code>newcap</code></td>
542 <td><code>true</code> if Initial Caps must be used with constructor
543 functions. <a href="#new"><small>(more)</small></a></td>
544 </tr>
545 <tr>
546 <td>Tolerate HTML event handlers </td>
547 <td><code>on</code></td>
548 <td><code>true</code> if HTML event handlers should be allowed. <a href="#html"><small>(more)</small></a></td>
549 </tr>
550 <tr>
551 <td>Allow one <code>var</code> statement per function</td>
552 <td><code>onevar</code></td>
553 <td><code>true</code> if only one <code>var</code> statement per function
554 should be allowed. <a href="#scope"><small>(more)</small></a></td>
555 </tr>
556 <tr>
557 <td>Stop on first error </td>
558 <td><code>passfail</code></td>
559 <td><code>true</code> if the scan should stop on first error.</td>
560 </tr>
561 <tr>
562 <td>Disallow <code>++</code> and <code>--</code> </td>
563 <td><code>plusplus</code></td>
564 <td><code>true</code> if <code>++</code> and <code>--</code> should
565 not be allowed. <a href="#inc"><small>(more)</small></a></td>
566 </tr>
567 <tr>
568 <td>Predefined <small>( , separated)</small></td>
569 <td><code>predef</code></td>
6735394 @douglascrockford Cleanup.
authored Dec 2, 2010
570 <td>An array of strings, the names of predefined global variables, or an object whose keys are global variable names, and whose values are booleans that determine if each variable is assignable (also see <a href="#global">global</a>). <code>predef</code> is used with the <code>option</code> object, but not
00d8d1f @douglascrockford option.predef
authored Dec 2, 2010
571 with the <code>/*jslint */</code> comment. You can also use the <code>var</code>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
572 statement to declare global variables in a script file.</td>
573 </tr>
574 <tr>
575 <td>Disallow insecure <code>.</code> and <code>[^</code>...<code>]</code>. in /RegExp/ </td>
576 <td><code>regexp</code></td>
577 <td><code>true</code> if <code>.</code> and <code>[^</code>...<code>]</code> should not be allowed in RegExp
578 literals. These forms should not be used when validating in secure applications.</td>
579 </tr>
580 <tr>
581 <td>Assume Rhino </td>
582 <td><code>rhino</code></td>
583 <td><code>true</code> if the <a href="">Rhino</a>
584 environment globals should be predefined. <a href="#rhino"><small>(more)</small></a></td>
585 </tr>
586 <tr>
587 <td>Safe Subset </td>
588 <td><code>safe</code></td>
589 <td><code>true</code> if the safe subset rules are enforced. These rules
590 are used by <a href="">ADsafe</a>. It enforces
591 the safe subset rules but not the widget structure rules.</td>
592 </tr>
593 <tr>
594 <td>Require <code>&quot;use strict&quot;;</code> </td>
595 <td><code>strict</code></td>
caa8885 @douglascrockford use strict
authored Dec 16, 2010
596 <td><code>true</code> if the ES5 <code><a href="">"use strict";</a></code> pragma
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
597 is required. Do not use this option carelessly.</td>
598 </tr>
599 <tr>
600 <td>Tolerate inefficient subscripting<br>
601 </td>
602 <td><code>sub</code></td>
603 <td><code>true</code> if subscript notation may be used for expressions
604 better expressed in dot notation.</td>
605 </tr>
606 <tr>
607 <td>Disallow undefined variables </td>
608 <td><code>undef</code></td>
609 <td><code>true</code> if variables must be declared before used. <a href="#undefined"><small>(more)</small></a></td>
610 </tr>
611 <tr>
612 <td>Strict white space </td>
613 <td><code>white</code></td>
614 <td><code>true</code> if strict whitespace rules apply.</td>
615 </tr>
616 <tr>
617 <td>Assume a Yahoo Widget </td>
618 <td><code>widget</code></td>
619 <td><code>true</code> if the <a href="">Yahoo
620 Widgets</a> globals should be predefined. <a href="#widget"><small>(more)</small></a></td>
621 </tr>
622 <tr>
623 <td>AssumeWindows</td>
624 <td><code>windows</code></td>
625 <td><code>true</code> if the Windows</a> globals should be predefined. <a href="#windows"><small>(more)</small></a></td>
626 </tr>
627 </tbody>
628 </table>
629 <h2 id=members>Members</h2>
630 <p>Since JavaScript is a loosely-typed, dynamic-object language, it is not
631 possible to determine at compile time if property names are spelled correctly.
632 <code>JSLint</code> provides some assistance with this.</p>
633 <p>At the bottom of its report, <code>JSLint</code> displays a <code>/*members*/</code>
634 comment. It contains all of the names and string literals that were used
635 with dot notation, subscript notation, and object literals to name the
636 members of objects. You can look through the list for misspellings. Member
637 names that were only used once are shown in italics. This is to make misspellings
638 easier to spot.</p>
639 <p>You can copy the <code>/*members*/</code> comment into your script file.
640 <code>JSLint</code> will check the spelling of all property names against
641 the list. That way, you can have <code>JSLint</code> look for misspellings
642 for you.</p>
643 <h2 id=report>Report</h2>
645 <p>If <code>JSLint</code> is able to complete its scan, it generates a function
646 report. It lists for each function:</p>
648 <ul>
649 <li>The line number on which it starts.</li>
650 <li>Its name. In the case of anonymous functions, <code>JSLint</code>
651 will &quot;guess&quot; the name.</li>
652 <li>The parameters.</li>
653 <li><i>Closure</i>: The variables and parameters that are declared in
654 the function that are used by its inner functions.</li>
655 <li><i>Variables</i>: The variables that are declared in the function
656 that are used only by the function.</li>
657 <li><i>Exceptions</i>: The variables that are declared by try statements.</li>
658 <li><i>Unused</i>: The variables that are declared in the function that
659 are not used. This may be an indication of an error.</li>
660 <li><i>Outer</i>: Variables used by this function that are declared in
661 another function.</li>
662 <li><i>Global</i>: Global variables that are used by this function. Keep
663 these to a minimum.</li>
664 <li><i>Label</i>: Statement labels that are used by this function.</li>
665 </ul>
666 <p>The report will also include a list of all of the <a href="#members">member
667 names</a> that were used. There is a <a href="msgs.html">list of <code>JSLint</code>
668 messages</a>.</p>
669 <h2 id=feedback>Feedback</h2>
670 <p>Please let me know if <code>JSLint</code> is useful for you. Is it too
671 strict? Is there a check or a report that could help you to improve the
672 quality of your programs? <a href=""></a></p>
674 <p>I intend to continue to adapt <code>JSLint</code> based on your comments.
675 Keep watching for improvements. Updates are announced at <a href=""></a>.</p>
677 <h2 id=try>Try it</h2>
679 <p><a href="" target="_blank">Try it.</a> Paste your script
680 into the window and click the
681 <a href="" target=jslint><input type="button" value="JSLint"></a>
682 button. The analysis is done by a script running on your machine.
683 Your script is not sent over the network. You can set the options used.
684 The <a href=""
685 target="_blank"><input type="button" value="Good Parts"></a> button in the
686 Options area will preset the best options for you.
bdd3576 @douglascrockford k
authored Dec 8, 2010
687 </p>
688 <p>
689 JSLint is written entirely in JavaScript, so it can run anywhere that JavaScript can run. See for example <a href="">;tbl=1</a>.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
690 <h2 id=implementation>Implementation</h2>
691 <p><code>JSLint</code> uses a <a href="">Pratt
692 Parser (Top Down Operator Precedence)</a>. It is written in JavaScript.
35ec4a5 @douglascrockford groove
authored Nov 18, 2010
693 The full source code is available: <a href=""></a>.</p>
ca120a7 @douglascrockford first commit
authored Nov 12, 2010
694 <a href=""><img src="jslintpill.gif" width="36" height="17" border="0"></a>
695 <a href=""><img src="adsafepill.gif" width="36" height="17" border="0"></a>
696 <a href=""><img src="jsonpill.gif" width="36" height="17" border="0"></a>
697 </body>
698 </html>
Something went wrong with that request. Please try again.