Static analysis tool for JavaScript that detects errors and potential problems in code
npx @tessl/cli install tessl/npm-jshint@2.13.00
# JSHint
1
2
JSHint is a community-driven static code analysis tool that detects errors and potential problems in JavaScript code. It provides comprehensive linting capabilities with flexible configuration options to help developers catch typos, language gotchas, syntax errors, and potential bugs before they become runtime issues.
3
4
## Package Information
5
6
- **Package Name**: jshint
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install jshint`
10
11
## Core Imports
12
13
```javascript
14
const { JSHINT } = require('jshint');
15
```
16
17
For CLI usage via binary:
18
```bash
19
jshint
20
```
21
22
## Basic Usage
23
24
```javascript
25
const { JSHINT } = require('jshint');
26
27
// Basic linting
28
const code = `
29
function hello(name) {
30
console.log("Hello " + name);
31
}
32
`;
33
34
const options = {
35
esversion: 6,
36
undef: true,
37
unused: true
38
};
39
40
const globals = {
41
console: false // console is read-only global
42
};
43
44
const result = JSHINT(code, options, globals);
45
46
if (!result) {
47
// Errors found
48
console.log(JSHINT.errors);
49
} else {
50
// No errors
51
console.log("Code is clean!");
52
}
53
54
// Get detailed analysis data
55
const data = JSHINT.data();
56
console.log(data.functions, data.globals, data.unused);
57
```
58
59
## Architecture
60
61
JSHint is built around several key components:
62
63
- **Core Linting Engine**: `JSHINT` function that analyzes JavaScript code and returns boolean results
64
- **Configuration System**: Flexible options system supporting enforcing, relaxing, and environment-specific settings
65
- **CLI Interface**: Command-line tool with file processing, reporting, and configuration management
66
- **Reporter System**: Pluggable output formatters for different use cases (default, unix, checkstyle, XML)
67
- **Message System**: Structured error, warning, and info messages with codes for programmatic handling
68
- **Environment Support**: Predefined global variable sets for different JavaScript environments and libraries
69
70
## Capabilities
71
72
### Core Linting
73
74
Primary JavaScript code analysis functionality that performs static analysis and error detection.
75
76
```javascript { .api }
77
/**
78
* Main linting function that analyzes JavaScript code
79
* @param source - JavaScript source code (string or array of strings)
80
* @param options - Configuration options object (optional)
81
* @param globals - Global variables declaration object (optional)
82
* @returns boolean - true if no errors found, false if errors found
83
*/
84
function JSHINT(source, options, globals);
85
86
// Properties available after calling JSHINT function
87
JSHINT.errors; // Array of error objects
88
JSHINT.scope; // Current scope name (string)
89
JSHINT.internals; // Array of internal eval code
90
JSHINT.blacklist; // Object of blacklisted variables
91
92
/**
93
* Returns detailed analysis data from the last linting run
94
* @returns object with comprehensive analysis results
95
*/
96
JSHINT.data();
97
98
/**
99
* Adds external modules to the linting process
100
* @param func - Module function to add to JSHint
101
*/
102
JSHINT.addModule(func);
103
104
// Self-reference to the JSHINT function
105
JSHINT.jshint;
106
```
107
108
[Core Linting](./core-linting.md)
109
110
### Command Line Interface
111
112
Complete CLI functionality for file processing, configuration management, and output formatting.
113
114
```javascript { .api }
115
/**
116
* Main CLI entry point that parses arguments and runs JSHint
117
* @param args - Command line arguments array (process.argv format)
118
*/
119
function interpret(args);
120
121
/**
122
* Runs linting with processed CLI options
123
* @param options - Processed CLI options object
124
* @param callback - Optional completion callback function
125
* @returns boolean result or null for async operations
126
*/
127
function run(options, callback);
128
129
/**
130
* Gathers files to be linted based on patterns and options
131
* @param options - File gathering options object
132
* @returns Array of file paths to be processed
133
*/
134
function gather(options);
135
136
/**
137
* Retrieves configuration for a specific file path
138
* @param filepath - File path for configuration lookup
139
* @returns Configuration object merged from various sources
140
*/
141
function getConfig(filepath);
142
143
/**
144
* Loads and parses a JSHint configuration file
145
* @param filepath - Path to configuration file
146
* @returns Parsed configuration object
147
*/
148
function loadConfig(filepath);
149
150
/**
151
* Extracts JavaScript code from HTML script tags
152
* @param code - Source code string (potentially HTML)
153
* @param option - Extraction mode ('always'|'auto'|'never')
154
* @returns Extracted JavaScript code string
155
*/
156
function extract(code, option);
157
158
/**
159
* Exits the process with specified code
160
* @param code - Exit code number
161
*/
162
function exit(code);
163
164
/**
165
* Helper function for testing stdout buffer size
166
* @returns Current buffer size number
167
*/
168
function getBufferSize();
169
```
170
171
[Command Line Interface](./cli.md)
172
173
### Configuration Options
174
175
Comprehensive configuration system with enforcing, relaxing, and environment options for customizing linting behavior.
176
177
```javascript { .api }
178
// JSHint Configuration Options Object
179
// Enforcing Options (stricter checking)
180
{
181
bitwise: boolean, // Prohibit bitwise operators
182
freeze: boolean, // Prohibit native prototype extension
183
camelcase: boolean, // Force camelCase (deprecated)
184
curly: boolean, // Require curly braces
185
eqeqeq: boolean, // Prohibit == and !=
186
futurehostile: boolean, // Warn about future reserved words
187
es3: boolean, // ES3 compliance (deprecated)
188
es5: boolean, // ES5 compliance (deprecated)
189
forin: boolean, // Require hasOwnProperty in for-in
190
immed: boolean, // Require immediate function invocation wrapping
191
latedef: boolean, // Prohibit use before definition
192
newcap: boolean, // Require constructor names capitalization
193
noarg: boolean, // Prohibit arguments.caller/callee
194
noempty: boolean, // Prohibit empty blocks
195
nonew: boolean, // Prohibit constructor usage for side effects
196
plusplus: boolean, // Prohibit ++ and --
197
quotmark: boolean, // Enforce quote marks
198
undef: boolean, // Require variable declarations
199
unused: boolean, // Warn about unused variables
200
strict: boolean, // Require strict mode
201
varstmt: boolean, // Disallow var statements
202
regexpu: boolean, // Enable RegExp u flag support
203
204
// Relaxing Options (more permissive)
205
asi: boolean, // Allow missing semicolons
206
boss: boolean, // Allow assignment in conditions
207
debug: boolean, // Allow debugger statements
208
eqnull: boolean, // Allow == null comparisons
209
esnext: boolean, // Enable ES6 syntax (deprecated)
210
evil: boolean, // Allow eval usage
211
expr: boolean, // Allow expression statements
212
funcscope: boolean, // Allow variables defined inside control structures
213
globalstrict: boolean, // Allow global strict mode (deprecated)
214
iterator: boolean, // Allow __iterator__ property
215
lastsemic: boolean, // Allow missing semicolon in one-line blocks
216
laxbreak: boolean, // Allow unsafe line breaks
217
laxcomma: boolean, // Allow comma-first coding style
218
loopfunc: boolean, // Allow functions in loops
219
multistr: boolean, // Allow multiline strings
220
noyield: boolean, // Allow generators without yield
221
proto: boolean, // Allow __proto__ property
222
scripturl: boolean, // Allow script-targeted URLs
223
shadow: boolean, // Allow variable shadowing
224
sub: boolean, // Allow subscript notation
225
supernew: boolean, // Allow new function() and new Object()
226
validthis: boolean, // Allow this in non-constructor functions
227
notypeof: boolean, // Allow invalid typeof comparisons
228
elision: boolean, // Allow array element elision
229
230
// Environment Options
231
browser: boolean, // Browser globals
232
couch: boolean, // CouchDB globals
233
devel: boolean, // Development globals (console, alert)
234
dojo: boolean, // Dojo globals
235
jasmine: boolean, // Jasmine globals
236
jquery: boolean, // jQuery globals
237
mocha: boolean, // Mocha globals
238
mootools: boolean, // MooTools globals
239
node: boolean, // Node.js globals
240
nonstandard: boolean, // Non-standard globals
241
phantom: boolean, // PhantomJS globals
242
prototypejs: boolean, // Prototype globals
243
qunit: boolean, // QUnit globals
244
rhino: boolean, // Rhino globals
245
shelljs: boolean, // ShellJS globals
246
typed: boolean, // Typed Array globals
247
worker: boolean, // Web Worker globals
248
wsh: boolean, // Windows Scripting Host globals
249
yui: boolean, // YUI globals
250
251
// Value-based Options
252
esversion: number, // ECMAScript version
253
maxlen: number, // Maximum line length
254
maxerr: number, // Maximum errors
255
maxdepth: number, // Maximum nested block depth
256
maxstatements: number, // Maximum statements per function
257
maxcomplexity: number, // Maximum cyclomatic complexity
258
maxparams: number, // Maximum parameters per function
259
indent: number, // Indentation width
260
predef: Array, // Predefined globals
261
globals: Object, // Global variables
262
quotmark: string, // Quote mark preference
263
shadow: string, // Variable shadowing mode
264
unused: string, // Unused variable checking mode
265
latedef: string, // Late definition checking mode
266
strict: string, // Strict mode requirement
267
ignore: string, // Ignore warnings by code
268
ignoreDelimiters: Array // Ignore code blocks
269
}
270
```
271
272
[Configuration Options](./configuration.md)
273
274
### Reporting System
275
276
Pluggable output formatters for different use cases including default, unix, checkstyle, and XML formats.
277
278
```javascript { .api }
279
/**
280
* Reporter function interface for formatting JSHint output
281
* @param results - Array of linting result objects
282
* @param data - Array of detailed analysis data objects
283
* @param opts - Reporter options object
284
*/
285
function reporter(results, data, opts) {
286
// results: Array of {file: string, error: object} objects
287
// data: Array of JSHINT.data() objects
288
// opts: {verbose: boolean, filename: string, ...}
289
}
290
```
291
292
[Reporting System](./reporting.md)
293
294
## Types
295
296
```javascript { .api }
297
// Error Object Structure
298
{
299
line: number, // Line number where error occurred
300
character: number, // Character position on line
301
reason: string, // Human-readable error message
302
code: string // Error code (e.g., 'W033', 'E041')
303
}
304
305
// Result Object Structure
306
{
307
file: string, // File path where error occurred
308
error: { // Error details object
309
line: number,
310
character: number,
311
reason: string,
312
code: string
313
}
314
}
315
316
// Data Object Structure (from JSHINT.data())
317
{
318
functions: Array, // Function analysis data
319
options: Object, // Options used for linting
320
errors: Array, // Error objects (if any)
321
implieds: Array, // Implied global variables
322
globals: Array, // Used/defined global variables
323
unused: Array, // Unused variables
324
member: Object, // Member usage data
325
json: boolean // JSON mode flag
326
}
327
```