|
|
Allow one var statement per function
In many languages, a block introduces a scope. Variables introduced in a block are not visible outside of the block.
In JavaScript, blocks do not introduce a scope. There is only function-scope. A variable introduced anywhere in a function is visible everywhere in the function. JavaScript's blocks confuse experienced programmers and lead to errors because the familiar syntax makes a false promise.
JSLint expects blocks with function, if, switch, while, for, do, and try statements and nowhere else.
In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function.
|
Javascript
|
Maintainability |
|
|
Cyclomatic complexity
Checks function cyclomatic complexity against a specified limit. The complexity is measured by the number of "if", "while", "do", "for", "?:", "catch", "switch", "case", "&&" and "||" statements (plus one) in the body of the member. It is a measure of the minimum number of possible paths through the source and therefore the number of required tests. Generally 1-4 is considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now!
|
MAX_COMPLEXITY:
|
|
| Key: |
CYCLOMATIC_COMPLEXITY |
|
Javascript
|
Maintainability |
|
|
Disallow ++ and --
The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces.
As warning messages that are used for JSLint warning mapping to rules for this rule is the same as for "Disallow bitwise operators", please enable or disable them together
|
Javascript
|
Maintainability |
|
|
Disallow == and !=
The == and != operators do type coercion before comparing. This is bad because it causes ' \t\r\n' == 0 to be true. This can mask type errors.
When comparing to any of the following values, use the === or !== operators (which do not do type coercion): 0 '' undefined null false true
If you only care that a value is truthy or falsy, then use the short form. Instead of
(foo != 0)
just say
(foo)
and instead of
(foo == 0)
say
(!foo)
Use the === and !== operators.
|
Javascript
|
Maintainability |
|
|
Disallow bitwise operators
JavaScript does not have an integer type, but it does have bitwise operators. The bitwise operators convert their operands from floating point to integers and back, so they are not as efficient as in C or other languages. They are rarely useful in browser applications. The similarity to the logical operators can mask some programming errors. This option prohibits the use of these operators: << >> >>> ~ & |.
As warning messages that are used for JSLint warning mapping to rules for this rule is the same as for "Disallow ++ and --", please enable or disable them together
|
Javascript
|
Maintainability |
|
|
Disallow dangling _ in identifiers
Do not use _ (underbar) as the first character of a name. It is sometimes used to indicate privacy, but it does not actually provide privacy. If privacy is important, use the forms that provide private members.
|
Javascript
|
Maintainability |
|
|
Disallow insecure . and [^...] in /RegExp/
Regular expressions are written in a terse and cryptic notation. JSLint looks for problems that may cause portability problems. It also attempts to resolve visual ambiguities by recommending explicit escapement.
JavaScript's syntax for regular expression literals overloads the / character. To avoid ambiguity, JSLint expects that the character preceding a regular expression literal is a ( or = or : or , character.
|
Javascript
|
Maintainability |
|
|
Disallow undefined variables
Variables must be declared before used.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate CSS workarounds
JSLint can inspect CSS files. It expects the first line of a CSS file to be
@charset "UTF-8";
This feature is experimental. Please report any problems or limitations. Enabling this option will not tolerate some of the non-standard-but-customary workarounds.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate EcmaScript 5 syntax
EcmaScript 5 syntax should not be allowed. It is likely that programs not using this option will produce syntax errors on EcmaScript 3 systems.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate HTML case
Checks if all HTML tags are lower case. Expects <html> instead of <Html> or <HTML>
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate HTML event handlers
Inline HTML event handlers should be avoided
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate HTML fragments
Inspects if HTML fragment is well formatted.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate debugger statements
Debugger statements should be allowed. Enable this option before going into production.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate eval
The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. The eval function is the most misused feature of JavaScript.
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate inefficient subscripting
Use o.foo instead of o['foo']
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate sloppy line breaking
Do Not Tolerate sloppy line breaking
|
Javascript
|
Maintainability |
|
|
Do Not Tolerate unfiltered for in
|
Javascript
|
Maintainability |
|
|
Do not use wrapper forms
|
Javascript
|
Maintainability |
|
|
Line too long.
Checks the maximum number of characters in a line.
|
Javascript
|
Maintainability |
|
|
Missing semicolon.
JavaScript uses a C-like syntax which requires the use of semicolons to delimit certain statements. JavaScript attempts to make those semicolons optional with a semicolon insertion mechanism. This is dangerous because it can mask errors.
Like C, JavaScript has ++ and -- and ( operators which can be prefixes or suffixes. The disambiguation is done by the semicolon.
In JavaScript, a linefeed can be whitespace or it can act as a semicolon. This replaces one ambiguity with another.
JSLint expects that every statement be followed by ; except for for, function, if, switch, try, and while. JSLint does not expect to see unnecessary semicolons or the empty statement.
|
Javascript
|
Maintainability |
|
|
Mixed spaces and tabs.
Avoid using tabs in source code as they appear differently in different editors
|
Javascript
|
Maintainability |
|
|
Other Rules
Not all warning/error messages in JSLint can be enabled/disabled. This rule is used to report all warnings/errors that are not mapped to any other rule. It is recommended to keep this rule enabled at all times.
|
Javascript
|
Maintainability |
|
|
Require Initial Caps for constructors
JSLint enforces the convention that constructor functions be given names with initial uppercase. JSLint does not expect to see a function invocation with an initial uppercase name unless it has the new prefix. JSLint does not expect to see the new prefix used with functions whose names do not start with initial uppercase.
|
Javascript
|
Maintainability |
|
|
Require parens around immediate invocations
Require parens around immediate invocations
|
Javascript
|
Maintainability |
|
|
Strict white space
Blank spaces should be used in the following circumstances:
- A keyword followed by ( (left parenthesis) should be separated by a space
while (true) {
- A blank space should not be used between a function value and its ( (left parenthesis). This helps to distinguish between keywords and function invocations.
- All binary operators except . (period) and ( (left parenthesis) and [ (left bracket) should be separated from their operands by a space.
- No space should separate a unary operator and its operand except when the operator is a word such as typeof.
- Each ; (semicolon) in the control part of a for statement should be followed with a space.
- Whitespace should follow every , (comma).
|
Javascript
|
Maintainability |
|
|
Unsafe character.
There are characters that are handled inconsistently in browsers, and so must be escaped when placed in strings.
\u0000-\u001f
\u007f-\u009f
\u00ad
\u0600-\u0604
\u070f
\u17b4
\u17b5
\u200c-\u200f
\u2028-\u202f
\u2060-\u206f
\ufeff
\ufff0-\uffff
|
Javascript
|
Maintainability |
|
|
Unused names
The variables that are declared in the function that are not used. This may be an indication of an error.
|
Javascript
|
Maintainability |
|
|
Use the array literal notation [].
|
Javascript
|
Maintainability |
|
|
Use the object literal notation {}.
|
Javascript
|
Maintainability |