Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 711 lines (671 sloc) 39.093 kb
ca120a73 »
2010-11-12 first commit
1 <html>
2 <head>
3 <title>JSLint: The JavaScript Code Quality Tool</title>
4 <link rel="icon" type="image/gif" href="http://www.JSLint.com/favicon.gif">
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="http://javascript.crockford.com/">JavaScript</a> Code Quality Tool</big></p>
38 <p><a href="http://www.crockford.com/" 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="http://www.JSLint.com" target="_blank"><code>JSLint</code></a>
45 will hurt your feelings.</p>
46 <h2 id=what>What is <code>JSLint</code>?</h2>
47
48 <p><a href="http://www.JSLint.com" 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>
51
52 <p>When <a href="http://en.wikipedia.org/wiki/C_programming_language">C</a>
53 was a <a href="http://cm.bell-labs.com/cm/cs/who/dmr/chist.html">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="http://en.wikipedia.org/wiki/Lint_programming_tool">lint</a></code>
57 was developed that would scan a source file, looking for problems.</p>
58
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>
62
63 <p><a href="http://javascript.crockford.com/">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
6735394d »
2010-12-02 Cleanup.
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="http://www.JSLint.com/"><code>JSLint</code></a>,
ca120a73 »
2010-11-12 first commit
68 a JavaScript syntax checker and validator.</p>
69
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>
76
77 <p><code>JSLint</code> defines a professional subset of JavaScript, a stricter
78 language than that defined by <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" 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="http://javascript.crockford.com/code.html" 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
6735394d »
2010-12-02 Cleanup.
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="http://www.JSON.org/">JSON</a>
ca120a73 »
2010-11-12 first commit
86 text.</p>
87 <h2 id=global>Global Variables</h2>
88 <p>JavaScript's <a href="http://yuiblog.com/blog/2006/06/01/global-domination/">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>
9f0303cf »
2011-05-22 appear
102 <p>The declaration should appear near the top of the file. It must appear before the use of the variables
ca120a73 »
2010-11-12 first commit
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>
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
105 <p><code>JSLint</code> also recognizes a <code>/*global */</code> directive 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). The directive respects function scope.</p>
ca120a73 »
2010-11-12 first commit
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>
df391013 »
2011-02-11 option.browser
112 <code>/*global
e752a553 »
2011-03-30 option.node
113 clearInterval: false, clearTimeout: false, document: false, event: false, frames: false, history: false, Image: false, location: false, name: false, navigator: false, Option: false, parent: false, screen: false, setInterval: false, setTimeout: false, window: false, XMLHttpRequest: false
df391013 »
2011-02-11 option.browser
114 */</code></blockquote>
e752a553 »
2011-03-30 option.node
115 <p>Select the
116 <em>Assume console, alert, ...</em>
ca120a73 »
2010-11-12 first commit
117 (<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
118 effect as this comment:</p>
119 <pre>/*global alert: false, confirm: false, console: false, Debug: false, opera: false, prompt: false */</pre>
706c4858 »
2011-03-22 option.node
120 <p id=node>Select the
e752a553 »
2011-03-30 option.node
121 <em>Assume Node.js</em>
706c4858 »
2011-03-22 option.node
122 (<code>node</code>) <a href="#options">option</a> to predefine globals that are used in the Node.js environment<code></code>. It has the same
123 effect as this comment:</p>
8d035d3c »
2011-03-31 e
124 <pre>/*global Buffer: false, clearInterval: false, clearTimeout: false, console: false, global: false, module: false, process: false, querystring: false, require: false, setInterval: false, setTimeout: false, util: false, __filename: false, __dirname: false */</pre>
ca120a73 »
2010-11-12 first commit
125 <p id=rhino>Select the <i>Assume Rhino</i> (<code>rhino</code>) <a href="#options">option</a>
706c4858 »
2011-03-22 option.node
126 to predefine the global properties provided by the Rhino environment.
ca120a73 »
2010-11-12 first commit
127 It has the same effect as this statement:</p>
128 <blockquote>
129 <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>
130 </blockquote>
131 <p id=widget>Select the <i>Assume a Yahoo Widget</i> (<code>widget</code>)
132 <a href="#options">option</a> to predefine the global properties provided
133 by the Yahoo! Widgets environment. It has the same effect as this statement:</p>
134 <blockquote>
135 <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>
136 </blockquote>
137 <p id=windows>Select the <i>Assume Windows</i> (<code>windows</code>)
138 <a href="#options">option</a> to predefine the global properties provided by Microsoft Windows. It has the same effect as this statement:</p>
139 <blockquote>
140 <p><code>/*global ActiveXObject: false, CScript: false, Debug: false, Enumerator: false, System: false, VBArray: false, WScript: false */</code></p>
141 </blockquote>
142 <h2 id=semicolon>Semicolon</h2>
6735394d »
2010-12-02 Cleanup.
143 <p>JavaScript uses a C-like syntax which requires the use of semicolons to delimit certain
144 statements. JavaScript attempts to make those semicolons optional with a semicolon
145 insertion mechanism. This is dangerous because it can mask errors.</p>
ca120a73 »
2010-11-12 first commit
146 <p>Like C, JavaScript has <code>++</code> and <code>--</code> and <code>(</code> operators
147 which can be prefixes or suffixes. The disambiguation is done by the semicolon.</p>
148 <p>In JavaScript, a linefeed can be whitespace or it can act as a semicolon.
149 This replaces one ambiguity with another. </p>
150 <p><code>JSLint</code> expects that every statement be followed by <code>;</code> except
151 for <code>for</code>, <code>function</code>, <code>if</code>, <code>switch</code>, <code>try</code>, and
152 <code>while</code>. <code>JSLint</code> does not expect to see unnecessary semicolons or the
153 empty statement.</p>
154 <h2 id=comma>Comma</h2>
155 <p>The comma operator can lead to excessively tricky expressions. It can also
156 mask some programming errors.</p>
157 <p><code>JSLint</code> expects to see the comma used as a separator, but not as an
158 operator (except in the initialization and incrementation parts of the <code>for</code>
159 statement). It does not expect to see elided elements in array literals. Extra
160 commas should not be used. A comma should not appear after the last element
161 of an array literal or object literal because it can be misinterpreted by some
162 browsers. </p>
163 <h2 id=scope>Scope</h2>
164
165 <p>In many languages, a block introduces a scope. Variables introduced in
166 a block are not visible outside of the block.</p>
167
168 <p>In JavaScript, blocks do not introduce a scope. There is only function-scope.
169 A variable introduced anywhere in a function is visible everywhere in
170 the function. JavaScript's blocks confuse experienced programmers and
171 lead to errors because the familiar syntax makes a false promise.</p>
172
173 <p><code>JSLint</code> expects blocks with <code>function</code>, <code>if</code>,
174 <code>switch</code>, <code>while</code>, <code>for</code>, <code>do</code>,
175 and <code>try</code> statements and nowhere else. </p>
176 <p>In languages with block scope, it is usually recommended that variables
177 be declared at the site of first use. But because JavaScript does not
178 have block scope, it is wiser to declare all of a function's variables
179 at the top of the function. It is recommended that a single <code>var</code>
a27d17cd »
2011-06-09 Tolerate
180 statement be used per function. This can be declined with the <code>vars</code>
ca120a73 »
2010-11-12 first commit
181 <a href="#options">option</a>.</p>
182
183 <h2 id=required>Required Blocks</h2>
184
185 <p><code>JSLint</code> expects that <code>if</code>, <code>while</code>,
186 <code>do</code> and <code>for</code> statements will be made with blocks
187 <code>{</code>that is, with statements enclosed in braces<code>}</code>.</p>
188
189 <p>JavaScript allows an <code>if</code> to be written like this:</p>
190
191 <pre>if (<i>condition</i><code>)
192 </code><i>statement</i>;</pre>
193
194 <p>That form is known to contribute to mistakes in projects where many programmers
195 are working on the same code. That is why <code>JSLint</code> expects the use of
196 a block:</p>
197
198 <pre>if (<i>condition</i>) {
199 <i>statements</i>;
200 }</pre>
201
202 <p>Experience shows that this form is more resilient.</p>
203
204 <h2 id=expression>Expression Statements</h2>
205 <p>An expression statement is expected to be an assignment or a function/method
206 call or <code>delete</code>. All other expression statements are considered
207 to be errors.</p>
208 <h2 id=forin><code>for</code> <code>in</code></h2>
209 <p>The <code>for</code> <code>in</code> statement allows for looping through
210 the names of all of the properties of an object. <a href="http://yuiblog.com/blog/2006/09/26/for-in-intrigue/">Unfortunately,
d4189fff »
2011-03-11 properties
211 it also loops through all of the properties that were inherited through
ca120a73 »
2010-11-12 first commit
212 the prototype chain.</a> This has the bad side effect of serving up method
d4189fff »
2011-03-11 properties
213 functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail.</p>
ca120a73 »
2010-11-12 first commit
214 <p>The body of every <code>for</code> <code>in</code> statement should be
215 wrapped in an <code>if</code> statement that does filtering. It can select
216 for a particular type or range of values, or it can exclude functions,
217 or it can exclude properties from the prototype. For example,</p>
218 <pre>for (name in object) {
219 if (object.hasOwnProperty(name)) {
220 ....
221 }
222
223 }</pre>
224
225 <h2 id=switch><code>switch</code></h2>
226 <p>A <a href="http://yuiblog.com/blog/2007/04/25/id-rather-switch-than-fight/">common
227 error</a> in <code>switch</code> statements is to forget to place a <code>break</code>
228 statement after each case, resulting in unintended fall-through. <code>JSLint</code>
229 expects that the statement before the next <code>case</code> or <code>default</code>
230 is one of these: <code>break</code>, <code>return</code>, or <code>throw</code>.
231 </p>
232 <h2 id=var><code>var</code></h2>
233
234 <p>JavaScript allows <code>var</code> definitions to occur anywhere
235 within a function. <code>JSLint</code> is more strict.</p>
236
237 <p><code>JSLint</code> expects that a <code>var</code> will be declared
238 only once, and that it will be declared before it is used.</p>
239 <p><code></code><code>JSLint</code> expects that a <code>function</code>
240 will be declared before it is used.</p>
241 <p><code>JSLint</code> expects that parameters will not also be declared
242 as vars. </p>
243
244 <p><code>JSLint</code> does not expect the <code>arguments</code> array to be declared
245 as a <code>var</code>.</p>
246 <p><code>JSLint</code> does not expect that a var will be defined in a block.
247 This is because JavaScript blocks do not have block scope. This can have
248 unexpected consequences. Define all variables at the top of the function.</p>
249
250 <h2 id=with><code>with</code></h2>
251
252 <p>The <code>with</code> statement was intended to provide a shorthand in accessing
d4189fff »
2011-03-11 properties
253 properties in deeply nested objects. Unfortunately, it behaves <a href="http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/">very
254 badly</a> when setting new properties. Never use the <code>with</code> statement. Use
ca120a73 »
2010-11-12 first commit
255 a <code>var</code> instead.</p>
256
257 <p><code>JSLint</code> does not expect to see a <code>with</code> statement.</p>
258
259 <h2 id=assignment>=</h2>
260 <p><code>JSLint</code> does not expect to see an assignment statement in
261 the condition part of an <code>if</code> or <code>for</code> or <code>while</code>
262 <code></code> or <code>do</code> statement. This is because it is more
263 likely that </p>
264 <pre>if (a = b) {
265 ...
266 }</pre>
267 <p>was intended to be </p>
268 <pre>if (a == b) {
269 ...
270 }</pre>
271 <p>It is difficult to write correct programs while using idioms that are
9d80d800 »
2011-01-30 asignment unwrapped
272 hard to distinguish from obvious errors.</p>
ca120a73 »
2010-11-12 first commit
273 <h2 id=equal>== and !=</h2>
274 <p>The <code>==</code> and <code>!=</code> operators do type coercion before
275 comparing. This is bad because it causes <code>' \t\r\n' == 0</code> to
276 be <code>true</code>. This can mask type errors.</p>
277 <p align="left">If you only care that a value is <i>truthy</i> or <i>falsy</i>,
278 then use the short form. Instead of </p>
279 <pre align="left">(foo != 0)</pre>
280 <p align="left">just say </p>
281 <pre align="left">(foo)</pre>
282 <p align="left">and instead of</p>
283 <pre align="left">(foo == 0)</pre>
284 <p align="left"> say</p>
285 <pre align="left">(!foo)</pre>
5675d2c6 »
2011-01-07 http://tech.groups.yahoo.com/group/jslint_com/message/1730
286 <p>Always use the <code>===</code> and <code>!==</code> operators. </p>
ca120a73 »
2010-11-12 first commit
287 <h2 id=labels>Labels</h2>
288 <p>JavaScript allows any statement to have a label, and labels have a
289 separate name space. <code>JSLint</code> is more strict.</p>
290
291 <p><code>JSLint</code> expects labels only on statements that interact
292 with <code>break</code>: <code>switch</code>, <code>while</code>,
293 <code>do</code>, and <code>for</code>. <code>JSLint</code> expects that labels
294 will be distinct from vars and parameters.</p>
295
296 <h2 id=unreachable>Unreachable Code</h2>
297 <p><code>JSLint</code> expects that
298 a <code>return</code>, <code>break</code>, <code>continue</code>,
299 or <code>throw</code> statement will be followed by
300 a <code>}</code> or <code>case</code> or <code>default</code>.</p>
301
302 <h2 id=pluses>Confusing Pluses and Minuses</h2>
303
304 <p><code>JSLint</code> expects that <code>+</code> will not be followed by
305 <code>+</code> or <code>++</code>, and that <code>-</code> will not be followed
306 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>
307 <h2 id=inc><code>++</code> and <code>--</code></h2>
308 <p>The <code>++</code> <small>(increment)</small> and <code>--</code> <small>(decrement)</small>
309 operators have been known to contribute to bad code by encouraging excessive
310 trickiness. They are second only to faulty architecture in enabling to
a27d17cd »
2011-06-09 Tolerate
311 viruses and other security menaces. Also, preincrement/postincrement confusion can produce off-by-one errors that are extrenmely difficult to diagnose. There is a <code>plusplus</code> <a href="#options">option</a>
312 that allows the use of these operators.</p>
ca120a73 »
2010-11-12 first commit
313 <h2 id=bitwise>Bitwise Operators</h2>
314 <p>JavaScript does not have an integer type, but it does have bitwise operators.
315 The bitwise operators convert their operands from floating point to integers
316 and back, so they are not as efficient as in C or other languages. They
317 are rarely useful in browser applications. The similarity to the logical
318 operators can mask some programming errors. The <code>bitwise</code> <a href="#options">option</a>
a27d17cd »
2011-06-09 Tolerate
319 allows the use of these operators: <code>&lt;&lt; &gt;&gt; &gt;&gt;&gt;
ca120a73 »
2010-11-12 first commit
320 ~ &amp; |</code>.</p>
321 <h2 id=evil><code>eval</code> is evil</h2>
322 <p>The <code>eval</code> function (and its relatives, <code>Function</code>,
323 <code>setTimeout</code>, and <code>setInterval</code>) provide access
324 to the JavaScript compiler. This is sometimes necessary, but in most cases
325 it indicates the presence of extremely bad coding. The <code>eval</code>
326 function is the most misused feature of JavaScript.</p>
327
328 <h2 id=void><code>void</code></h2>
329 <p>In most C-like languages, <code>void</code> is a type. In
330 JavaScript, <code>void</code> is a prefix operator that always
331 returns <code>undefined</code>. <code>JSLint</code> does not expect to
332 see <code>void</code> because it is confusing and not very useful.</p>
333
334 <h2 id=regexp>Regular Expressions</h2>
335 <p>Regular expressions are written in a terse and cryptic notation. <code>JSLint</code>
336 looks for problems that may cause portability problems. It also attempts
337 to resolve visual ambiguities by recommending explicit escapement.</p>
338 <p>JavaScript's syntax for regular expression literals overloads the <code>/</code>
339 character. To avoid ambiguity, <code>JSLint</code> expects that the character
340 preceding a regular expression literal is a <code>(</code> or <code>=</code>
341 or <code>:</code> or <code>,</code> character. </p>
342 <h2 id=new>Constructors and <code>new</code></h2>
343 <p>Constructors are functions that are designed to be used with the <code>new</code>
344 prefix. The <code>new</code> prefix creates a new object based on the
345 function's <code>prototype</code>, and binds that object to the function's
346 implied <code>this</code> parameter. If you neglect to use the <code>new</code>
347 prefix, no new object will be made and <code>this</code> will be bound
348 to the global object. This is a <a href="http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/">serious
349 mistake</a>.</p>
350 <p><code>JSLint</code> enforces the convention that constructor functions
351 be given names with initial uppercase. <code>JSLint</code> does not expect
352 to see a function invocation with an initial uppercase name unless it
353 has the <code>new</code> prefix. <code>JSLint</code> does not expect to
354 see the <code>new</code> prefix used with functions whose names do not
a27d17cd »
2011-06-09 Tolerate
355 start with initial uppercase. This can be disabled with the <code>newcap</code>
ca120a73 »
2010-11-12 first commit
356 <a href="#options">option</a>.</p>
357 <p><code>JSLint</code> does not expect to see the wrapper forms <code>new Number</code>,
358 <code>new String</code>, <code>new Boolean</code>. </p>
359 <p><code>JSLint</code> does not expect to see <code>new Object</code> (use <code>{}</code>
360 instead). </p>
361 <p><code>JSLint</code> does not expect to see <code>new Array</code> (use <code>[]</code>
362 instead).</p>
363 <h2 id=unsafe>Unsafe Characters</h2>
364 <p> There are characters that are handled inconsistently in browsers, and
365 so must be escaped when placed in strings. </p>
366 <pre>\u0000-\u001f
367 \u007f-\u009f
368 \u00ad
369 \u0600-\u0604
370 \u070f
371 \u17b4
372 \u17b5
373 \u200c-\u200f
374 \u2028-\u202f
375 \u2060-\u206f
376 \ufeff
377 \ufff0-\uffff</pre>
378 <h2 id=not>Not Looked For</h2>
379
380 <p><code>JSLint</code> does not do flow analysis to determine that variables are assigned
381 values before used. This is because variables are given a value (<code>undefined</code>)
6735394d »
2010-12-02 Cleanup.
382 that is a reasonable default for many applications.</p>
ca120a73 »
2010-11-12 first commit
383
384 <p><code>JSLint</code> does not do any kind of global analysis. It does
385 not attempt to determine that functions used with <code>new</code> are
386 really constructors (<a href="#new">except by enforcing capitalization
d4189fff »
2011-03-11 properties
387 conventions</a>), or that property names are spelled correctly (<a href="#properties">except
388 for matching against the <code>/*properties */</code> comment</a>).</p>
ca120a73 »
2010-11-12 first commit
389 <h2 id=html>HTML</h2>
390 <p><code>JSLint</code> is able to handle HTML text. It can inspect the JavaScript content
391 contained within <code>&lt;script&gt;</code>...<code>&lt;/script&gt;</code> tags. It
392 also inspects the HTML content, looking for problems that are known to interfere
393 with JavaScript:</p>
394 <ul>
395 <li>All tag names must be in lower case.</li>
396 <li>All tags that can take a close tag (such as <code>&lt;/p&gt;</code>)
397 must have a close tag.</li>
398 <li>All tags are correctly nested.</li>
399 <li>The entity <code>&amp;lt;</code> must be used for literal <code>'&lt;'</code>.</li>
400 </ul>
401 <p><code>JSLint</code> is less anal than the sycophantic conformity demanded
402 by XHTML, but more strict than the popular browsers. </p>
403 <p><code>JSLint</code> also checks for the occurrence of<code> '&lt;/' </code>in
404 string literals. You should always write<code> '&lt;\/' </code>instead.
405 The extra backslash is ignored by the JavaScript compiler but not by the
406 HTML parser. Tricks like this should not be necessary, and yet they are.</p>
407 <p>There is a <code>cap</code> <a href="#options">option</a> that allows
408 use of upper case tag names. There is also an <code>on</code> <a href="#options">option</a>
409 that allows the use of inline HTML event handlers.</p>
410 <p>There is a <code>fragment</code> <a href="#options">option</a> that can
411 inspect a well formed HTML fragment. If the <code>adsafe</code> <a href="#options">option</a>
412 is also used, then the fragment must be a <code>&lt;div&gt;</code> that
413 conforms to the <a href="http://www.ADsafe.org/">ADsafe</a> widget rules.</p>
414 <h2 id=css>CSS</h2>
415 <p><code>JSLint</code> can inspect CSS files. It expects the first line
416 of a CSS file to be </p>
417 <pre>@charset &quot;UTF-8&quot;;</pre>
418 <p>This feature is experimental. Please report any problems or limitations.
419 There is a <code>css</code> <a href="#options">option</a> that will tolerate
420 some of the non-standard-but-customary workarounds. <br>
421 </p>
422
423 <h2 id=options>Options</h2>
424 <p><code>JSLint</code> provides several options that control its operation and
425 its sensitivity. In the <a href="http://www.JSLint.com/">web edition</a>, the
a27d17cd »
2011-06-09 Tolerate
426 options are selected with several checkboxes and two fields. </p>
ca120a73 »
2010-11-12 first commit
427 <p>It also provides assistance in constructing <code>/*jslint*/</code>
428 comments.
429 </p>
430 <p>When <code>JSLINT</code> is called as a function, it accepts an <code>option</code> object
431 parameter that allows you to determine the subset of JavaScript that is
432 acceptable to you. The web page version of <code>JSLint</code> at <a href="http://www.JSLint.com/">http://www.JSLint.com/</a>
433 does this for you. </p>
434 <p>Options can also be specified within a script with a <code>/*jslint */</code>
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
435 directive:</p>
ca120a73 »
2010-11-12 first commit
436 <pre>/*jslint nomen: true, debug: true,
a27d17cd »
2011-06-09 Tolerate
437 evil: false, vars: true */</pre>
ca120a73 »
2010-11-12 first commit
438 <p>An option specification starts with <code>/*jslint</code>. Notice that
439 there is no space before the <code>j</code>. The specification contains
440 a sequence of name value pairs, where the names are <code>JSLint</code>
441 options, and the values are <code>true</code> or <code>false</code>. The
442 <code>indent</code> <a href="#options">option</a> can take a number. A <code>/*jslint */</code>
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
443 comment takes precedence over the <code>option</code> object. The directive respects function scope.</p>
ca120a73 »
2010-11-12 first commit
444 <table>
445 <tbody>
446 <tr>
447 <th>Description</th>
448 <th><code>option</code></th>
449 <th>Meaning</th>
450 </tr>
451 <tr>
452 <td>ADsafe</td>
453 <td><code>adsafe</code></td>
454 <td><code>true</code> if <a href="http://www.ADsafe.org/">AD<span style="color: blue;">safe</span></a>
455 rules should be enforced. See <a href="http://www.ADsafe.org/">http://www.ADsafe.org/</a>.</td>
456 </tr>
457 <tr>
a27d17cd »
2011-06-09 Tolerate
458 <td>Tolerate bitwise operators </td>
ca120a73 »
2010-11-12 first commit
459 <td><code>bitwise</code></td>
a27d17cd »
2011-06-09 Tolerate
460 <td><code>true</code> if bitwise operators should be allowed. <a href="#bitwise"><small>(more)</small></a></td>
ca120a73 »
2010-11-12 first commit
461 </tr>
462 <tr>
463 <td>Assume a browser </td>
464 <td><code>browser</code></td>
465 <td><code>true</code> if the standard browser globals should be predefined.
466 <a href="#browser"><small>(more)</small></a> </td>
467 </tr>
468 <tr>
469 <td>Tolerate HTML case </td>
470 <td><code>cap</code></td>
471 <td><code>true</code> if upper case HTML should be allowed.</td>
472 </tr>
473 <tr>
3c8016ec »
2011-02-02 option.continue
474 <td>Tolerate <code>continue</code></td>
475 <td><code>continue</code></td>
476 <td><code>true</code> if the <code>continue</code> statement should be allowed.</td>
477 </tr>
478 <tr>
ca120a73 »
2010-11-12 first commit
479 <td>Tolerate CSS workarounds</td>
480 <td><code>css</code></td>
481 <td><code>true</code> if CSS workarounds should be tolerated. <a href="#css"><small>(more)</small></a></td>
482 </tr>
483 <tr>
484 <td>Tolerate debugger statements</td>
485 <td><code>debug</code></td>
486 <td><code>true</code> if <code>debugger</code> statements should be
487 allowed. Set this option to <code>false</code> before going into production.</td>
488 </tr>
489 <tr>
490 <td>Assume <code>console</code>, <code>alert</code>, ...</td>
491 <td><code>devel</code></td>
492 <td><code>true</code> if browser globals that are useful in development should be
493 predefined. (<a href="#devel">more</a>)</td>
494 </tr>
495 <tr>
5675d2c6 »
2011-01-07 http://tech.groups.yahoo.com/group/jslint_com/message/1730
496 <td>Tolerate ES5 syntax</td>
497 <td><code>es5</code></td>
40e3f731 »
2011-01-12 It
498 <td><code>true</code> if ES5 syntax should be allowed.</a> It is likely that programs using this option will produce syntax errors on ES3 systems.</td>
ca120a73 »
2010-11-12 first commit
499 </tr>
500 <tr>
501 <td>Tolerate <code>eval</code> </td>
502 <td><code>evil</code></td>
503 <td><code>true</code> if <code>eval</code> should be allowed. <a href="#evil"><small>(more)</small></a></td>
504 </tr>
505 <tr>
506 <td>Tolerate unfiltered for in </td>
507 <td><code>forin</code></td>
508 <td><code>true</code> if unfiltered <code>for</code> <code>in</code>
509 statements should be allowed. <a href="#forin"><small>(more)</small></a></td>
510 </tr>
511 <tr>
512 <td>Tolerate HTML fragments </td>
513 <td><code>fragment</code></td>
514 <td><code>true</code> if HTML fragments should be allowed. <a href="#html"><small>(more)</small></a></td>
515 </tr>
516 <tr>
5675d2c6 »
2011-01-07 http://tech.groups.yahoo.com/group/jslint_com/message/1730
517 <td>Strict white space indentation</td>
518 <td><code>indent</code></td>
dedfd852 »
2011-01-20 option.indent
519 <td>The number of spaces used for indentation (default is 4). If 0, then no indentation checking takes place.</td>
ca120a73 »
2010-11-12 first commit
520 </tr>
521 <tr>
5675d2c6 »
2011-01-07 http://tech.groups.yahoo.com/group/jslint_com/message/1730
522 <td>Maximum number of errors</td>
523 <td><code>maxerr</code></td>
524 <td>The maximum number of warnings reported (default is 50)</td>
ca120a73 »
2010-11-12 first commit
525 </tr>
526 <tr>
527 <td>Maximum line length</td>
528 <td><code>maxlen</code></td>
529 <td>The maximum number of characters in a line</td>
530 </tr>
531 <tr>
a27d17cd »
2011-06-09 Tolerate
532 <td>Tolerate uncapitalized constructors</td>
ca120a73 »
2010-11-12 first commit
533 <td><code>newcap</code></td>
a27d17cd »
2011-06-09 Tolerate
534 <td><code>true</code> if Initial Caps with constructor
535 functions is optional. <a href="#new"><small>(more)</small></a></td>
ca120a73 »
2010-11-12 first commit
536 </tr>
537 <tr>
706c4858 »
2011-03-22 option.node
538 <td>Assume Node.js</td>
539 <td><code>node</code></td>
540 <td><code>true</code> if Node.js globals should be predefined <a href="#node"><small>(more)</small></a></td>
541 </tr>
542 <tr>
a27d17cd »
2011-06-09 Tolerate
543 <td>Tolerate dangling _ in identifiers </td>
706c4858 »
2011-03-22 option.node
544 <td><code>nomen</code></td>
a27d17cd »
2011-06-09 Tolerate
545 <td><code>true</code> if names should not be checked for initial or trailing underbars</td>
706c4858 »
2011-03-22 option.node
546 </tr>
547 <tr>
ca120a73 »
2010-11-12 first commit
548 <td>Tolerate HTML event handlers </td>
549 <td><code>on</code></td>
550 <td><code>true</code> if HTML event handlers should be allowed. <a href="#html"><small>(more)</small></a></td>
551 </tr>
552 <tr>
553 <td>Stop on first error </td>
554 <td><code>passfail</code></td>
555 <td><code>true</code> if the scan should stop on first error.</td>
556 </tr>
557 <tr>
a27d17cd »
2011-06-09 Tolerate
558 <td>Tolerate <code>++</code> and <code>--</code> </td>
ca120a73 »
2010-11-12 first commit
559 <td><code>plusplus</code></td>
560 <td><code>true</code> if <code>++</code> and <code>--</code> should
a27d17cd »
2011-06-09 Tolerate
561 be allowed. <a href="#inc"><small>(more)</small></a></td>
ca120a73 »
2010-11-12 first commit
562 </tr>
563 <tr>
564 <td>Predefined <small>( , separated)</small></td>
565 <td><code>predef</code></td>
6735394d »
2010-12-02 Cleanup.
566 <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
00d8d1f2 »
2010-12-02 option.predef
567 with the <code>/*jslint */</code> comment. You can also use the <code>var</code>
ca120a73 »
2010-11-12 first commit
568 statement to declare global variables in a script file.</td>
569 </tr>
570 <tr>
a27d17cd »
2011-06-09 Tolerate
571 <td>Tolerate <code>.</code> and <code>[^</code>...<code>]</code>. in /RegExp/ </td>
ca120a73 »
2010-11-12 first commit
572 <td><code>regexp</code></td>
a27d17cd »
2011-06-09 Tolerate
573 <td><code>true</code> if <code>.</code> and <code>[^</code>...<code>]</code> should be allowed in RegExp
8d1c4eb6 »
2011-01-10 clarification
574 literals. They match more material than might be expected, allowing attackers to confuse applications. These forms should not be used when validating in secure applications. </td>
ca120a73 »
2010-11-12 first commit
575 </tr>
576 <tr>
577 <td>Assume Rhino </td>
578 <td><code>rhino</code></td>
579 <td><code>true</code> if the <a href="http://www.mozilla.org/rhino/">Rhino</a>
580 environment globals should be predefined. <a href="#rhino"><small>(more)</small></a></td>
581 </tr>
582 <tr>
583 <td>Safe Subset </td>
584 <td><code>safe</code></td>
585 <td><code>true</code> if the safe subset rules are enforced. These rules
586 are used by <a href="http://www.ADsafe.org/">ADsafe</a>. It enforces
587 the safe subset rules but not the widget structure rules.</td>
588 </tr>
589 <tr>
a27d17cd »
2011-06-09 Tolerate
590 <td> Tolerate missing&nbsp;<code>'use strict'</code>&nbsp;pragma </td>
591 <td><code>sloppy</code></td>
592 <td><code>true</code> if the ES5 <code><a href="http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/">'use strict';</a></code> pragma
593 is not required. Do not use this pragma unless you know what yo are doing.</td>
ca120a73 »
2010-11-12 first commit
594 </tr>
595 <tr>
596 <td>Tolerate inefficient subscripting<br>
597 </td>
598 <td><code>sub</code></td>
599 <td><code>true</code> if subscript notation may be used for expressions
600 better expressed in dot notation.</td>
601 </tr>
602 <tr>
ae6a9f8c »
2011-06-02 option.type
603 <td>Tolerate type inconsistency<br>
604 </td>
605 <td><code>type</code></td>
606 <td><code>true</code> if variables are allowed to contain more than one type of value.</td>
607 </tr>
608 <tr>
a27d17cd »
2011-06-09 Tolerate
609 <td> Tolerate misordered definitions </td>
ca120a73 »
2010-11-12 first commit
610 <td><code>undef</code></td>
a27d17cd »
2011-06-09 Tolerate
611 <td><code>true</code> if variables and functions need not be declared before used. <a href="#undefined"><small>(more)</small></a></td>
ca120a73 »
2010-11-12 first commit
612 </tr>
613 <tr>
0b060c1a »
2011-06-09 unparam
614 <td> Tolerate unused parameters</td>
615 <td><code>unparam</code></td>
616 <td><code>true</code> if warnings should not be given for unused paramters></td>
617 </tr>
618 <tr>
619 <td>Tolerate many&nbsp;<tt>var</tt>&nbsp;statements per function</td>
620 <td><code>vars</code></td>
621 <td><code>true</code> if multiple <code>var</code> statement per function
622 should be allowed. <a href="#scope"><small>(more)</small></a></td>
623 </tr>
624 <tr>
a27d17cd »
2011-06-09 Tolerate
625 <td> Tolerate messy white space</td>
ca120a73 »
2010-11-12 first commit
626 <td><code>white</code></td>
0b060c1a »
2011-06-09 unparam
627 <td><code>true</code> if strict whitespace rules should be ignored.</td>
ca120a73 »
2010-11-12 first commit
628 </tr>
629 <tr>
630 <td>Assume a Yahoo Widget </td>
631 <td><code>widget</code></td>
632 <td><code>true</code> if the <a href="http://widgets.yahoo.com/gallery/view.php?widget=37484">Yahoo
633 Widgets</a> globals should be predefined. <a href="#widget"><small>(more)</small></a></td>
634 </tr>
635 <tr>
0b060c1a »
2011-06-09 unparam
636 <td>Assume Windows</td>
ca120a73 »
2010-11-12 first commit
637 <td><code>windows</code></td>
638 <td><code>true</code> if the Windows</a> globals should be predefined. <a href="#windows"><small>(more)</small></a></td>
639 </tr>
640 </tbody>
641 </table>
d4189fff »
2011-03-11 properties
642 <h2 id=properties>Properties</h2>
ca120a73 »
2010-11-12 first commit
643 <p>Since JavaScript is a loosely-typed, dynamic-object language, it is not
644 possible to determine at compile time if property names are spelled correctly.
645 <code>JSLint</code> provides some assistance with this.</p>
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
646 <p>At the bottom of its report, <code>JSLint</code> displays a <code>/*properties*/</code>
ca120a73 »
2010-11-12 first commit
647 comment. It contains all of the names and string literals that were used
648 with dot notation, subscript notation, and object literals to name the
d4189fff »
2011-03-11 properties
649 properties of objects. You can look through the list for misspellings. Property
ca120a73 »
2010-11-12 first commit
650 names that were only used once are shown in italics. This is to make misspellings
651 easier to spot.</p>
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
652 <p>You can copy the <code>/*properties*/</code> comment into your script file.
ca120a73 »
2010-11-12 first commit
653 <code>JSLint</code> will check the spelling of all property names against
654 the list. That way, you can have <code>JSLint</code> look for misspellings
475768b9 »
2011-03-06 /*properties*/ and /*global*/ now respect function scope
655 for you. The directive respects function scope.</p>
ca120a73 »
2010-11-12 first commit
656 <h2 id=report>Report</h2>
657
658 <p>If <code>JSLint</code> is able to complete its scan, it generates a function
659 report. It lists for each function:</p>
660
661 <ul>
662 <li>The line number on which it starts.</li>
663 <li>Its name. In the case of anonymous functions, <code>JSLint</code>
664 will &quot;guess&quot; the name.</li>
665 <li>The parameters.</li>
666 <li><i>Closure</i>: The variables and parameters that are declared in
667 the function that are used by its inner functions.</li>
668 <li><i>Variables</i>: The variables that are declared in the function
669 that are used only by the function.</li>
670 <li><i>Exceptions</i>: The variables that are declared by try statements.</li>
671 <li><i>Unused</i>: The variables that are declared in the function that
672 are not used. This may be an indication of an error.</li>
673 <li><i>Outer</i>: Variables used by this function that are declared in
674 another function.</li>
675 <li><i>Global</i>: Global variables that are used by this function. Keep
676 these to a minimum.</li>
677 <li><i>Label</i>: Statement labels that are used by this function.</li>
678 </ul>
d4189fff »
2011-03-11 properties
679 <p>The report will also include a list of all of the <a href="#properties">property
ca120a73 »
2010-11-12 first commit
680 names</a> that were used. There is a <a href="msgs.html">list of <code>JSLint</code>
681 messages</a>.</p>
682 <h2 id=feedback>Feedback</h2>
683 <p>Please let me know if <code>JSLint</code> is useful for you. Is it too
684 strict? Is there a check or a report that could help you to improve the
685 quality of your programs? <a href="mailto:douglas@crockford.com">douglas@crockford.com</a></p>
686
687 <p>I intend to continue to adapt <code>JSLint</code> based on your comments.
688 Keep watching for improvements. Updates are announced at <a href="http://tech.groups.yahoo.com/group/jslint_com/">http://tech.groups.yahoo.com/group/jslint_com/</a>.</p>
689
690 <h2 id=try>Try it</h2>
691
692 <p><a href="http://www.JSLint.com" target="_blank">Try it.</a> Paste your script
693 into the window and click the
694 <a href="http://www.JSLint.com" target=jslint><input type="button" value="JSLint"></a>
695 button. The analysis is done by a script running on your machine.
696 Your script is not sent over the network. You can set the options used.
697 The <a href="http://www.amazon.com/exec/obidos/ASIN/0596517742/wrrrldwideweb"
698 target="_blank"><input type="button" value="Good Parts"></a> button in the
699 Options area will preset the best options for you.
bdd35767 »
2010-12-08 k
700 </p>
701 <p>
702 JSLint is written entirely in JavaScript, so it can run anywhere that JavaScript can run. See for example <a href="http://tech.groups.yahoo.com/group/jslint_com/database?method=reportRows&tbl=1">http://tech.groups.yahoo.com/group/jslint_com/database?method=reportRows&amp;tbl=1</a>.</p>
ca120a73 »
2010-11-12 first commit
703 <h2 id=implementation>Implementation</h2>
704 <p><code>JSLint</code> uses a <a href="http://javascript.crockford.com/tdop/tdop.html">Pratt
705 Parser (Top Down Operator Precedence)</a>. It is written in JavaScript.
35ec4a53 »
2010-11-18 groove
706 The full source code is available: <a href="https://github.com/douglascrockford/JSLint">https://github.com/douglascrockford/JSLint</a>.</p>
ca120a73 »
2010-11-12 first commit
707 <a href="http://www.JSLint.com/"><img src="jslintpill.gif" width="36" height="17" border="0"></a>
708 <a href="http://www.ADsafe.org/"><img src="adsafepill.gif" width="36" height="17" border="0"></a>
709 <a href="http://www.JSON.org/"><img src="jsonpill.gif" width="36" height="17" border="0"></a>
710 </body>
711 </html>
Something went wrong with that request. Please try again.