0
# Core Linting
1
2
Core JavaScript code analysis functionality that performs static analysis, error detection, and provides detailed analysis data.
3
4
## Capabilities
5
6
### JSHINT Function
7
8
Main linting function that analyzes JavaScript code and returns analysis results.
9
10
```javascript { .api }
11
/**
12
* Analyzes JavaScript source code for errors and potential problems
13
* @param source - JavaScript source code as string or array of strings
14
* @param options - Configuration options object (optional)
15
* @param globals - Global variables declaration object (optional)
16
* @returns boolean - true if no errors found, false if errors detected
17
*/
18
function JSHINT(source, options, globals);
19
```
20
21
**Usage Examples:**
22
23
```javascript
24
const { JSHINT } = require('jshint');
25
26
// Basic usage
27
const result = JSHINT('var x = 1;');
28
console.log(result); // true (no errors)
29
30
// With options
31
const code = 'function test() { return undefined == null; }';
32
const options = { eqeqeq: true }; // require === and !==
33
const result2 = JSHINT(code, options);
34
console.log(result2); // false (== not allowed)
35
36
// With globals
37
const codeWithGlobals = 'console.log("hello");';
38
const globals = { console: false }; // console is read-only
39
const result3 = JSHINT(codeWithGlobals, {}, globals);
40
console.log(result3); // true
41
```
42
43
### Error Information
44
45
Access to detailed error information after linting.
46
47
```javascript { .api }
48
/**
49
* Array of error objects from the last linting run
50
* Available after calling JSHINT() function
51
*/
52
JSHINT.errors: Array<{
53
file: string,
54
error: {
55
line: number,
56
character: number,
57
reason: string,
58
code: string
59
}
60
}>;
61
62
/**
63
* Current scope identifier string
64
*/
65
JSHINT.scope: string;
66
67
/**
68
* Internal processing data array
69
*/
70
JSHINT.internals: any[];
71
72
/**
73
* Blacklisted identifiers object
74
*/
75
JSHINT.blacklist: object;
76
```
77
78
**Usage Examples:**
79
80
```javascript
81
const { JSHINT } = require('jshint');
82
83
const badCode = `
84
function test() {
85
var x = 1
86
var y = 2 // missing semicolon
87
return x + y;
88
}
89
`;
90
91
const result = JSHINT(badCode, { asi: false }); // require semicolons
92
93
if (!result) {
94
console.log('Errors found:');
95
JSHINT.errors.forEach(errorInfo => {
96
const { error } = errorInfo;
97
console.log(`Line ${error.line}, Column ${error.character}: ${error.reason}`);
98
console.log(`Error code: ${error.code}`);
99
});
100
}
101
```
102
103
### Analysis Data
104
105
Comprehensive analysis data about the linted code.
106
107
```javascript { .api }
108
/**
109
* Returns detailed analysis data from the last linting run
110
* @returns object containing comprehensive analysis results
111
*/
112
JSHINT.data(): {
113
functions: Array<{
114
name: string,
115
line: number,
116
character: number,
117
last: number,
118
lastcharacter: number,
119
params: string[],
120
closure: any[],
121
exceptions: any[],
122
undefined: any[],
123
unused: any[],
124
global: any[],
125
label: any[]
126
}>,
127
options: object,
128
errors: any[],
129
implieds: Array<{
130
name: string,
131
line: number[]
132
}>,
133
globals: Array<{
134
name: string,
135
line: number[]
136
}>,
137
unused: Array<{
138
name: string,
139
line: number,
140
character: number
141
}>,
142
member: object,
143
json: boolean
144
};
145
```
146
147
**Usage Examples:**
148
149
```javascript
150
const { JSHINT } = require('jshint');
151
152
const code = `
153
function calculate(x, y) {
154
var result = x + y;
155
console.log(result);
156
return result;
157
}
158
159
var unused = 42; // unused variable
160
globalVar = 'implicit global'; // implied global
161
`;
162
163
JSHINT(code, { undef: true, unused: true });
164
165
const data = JSHINT.data();
166
167
// Function information
168
console.log('Functions found:');
169
data.functions.forEach(func => {
170
console.log(`- ${func.name} at line ${func.line}, params: ${func.params.join(', ')}`);
171
});
172
173
// Unused variables
174
if (data.unused.length > 0) {
175
console.log('Unused variables:');
176
data.unused.forEach(unused => {
177
console.log(`- ${unused.name} at line ${unused.line}`);
178
});
179
}
180
181
// Implied globals
182
if (data.implieds.length > 0) {
183
console.log('Implied globals:');
184
data.implieds.forEach(implied => {
185
console.log(`- ${implied.name} used at lines: ${implied.line.join(', ')}`);
186
});
187
}
188
189
// Defined globals
190
if (data.globals.length > 0) {
191
console.log('Defined globals:');
192
data.globals.forEach(global => {
193
console.log(`- ${global.name} at lines: ${global.line.join(', ')}`);
194
});
195
}
196
```
197
198
### Module System
199
200
Extension mechanism for adding external modules to the linting process.
201
202
```javascript { .api }
203
/**
204
* Adds external modules to the linting process
205
* @param func - Module function to add to JSHint
206
*/
207
JSHINT.addModule(func);
208
209
/**
210
* Self-reference to the JSHINT function
211
*/
212
JSHINT.jshint: typeof JSHINT;
213
```
214
215
**Usage Examples:**
216
217
```javascript
218
const { JSHINT } = require('jshint');
219
220
// Add a custom module
221
JSHINT.addModule(function(api) {
222
// Custom linting logic using JSHint's internal API
223
api.on('Identifier', function(data) {
224
if (data.name === 'badName') {
225
api.warn('Avoid using badName', data);
226
}
227
});
228
});
229
230
// Use JSHINT with custom module
231
const code = 'var badName = 42;';
232
const result = JSHINT(code);
233
// Custom warning will be included in results
234
```
235
236
## Error Codes
237
238
JSHint uses structured error codes for programmatic handling:
239
240
- **E### codes**: Syntax errors and fatal issues (E001-E070)
241
- **W### codes**: Warnings about problematic patterns (W001-W148)
242
- **I### codes**: Informational messages (I001-I003)
243
244
## Common Workflow
245
246
```javascript
247
const { JSHINT } = require('jshint');
248
249
function lintCode(sourceCode, userOptions = {}) {
250
// Default options
251
const defaultOptions = {
252
esversion: 6,
253
undef: true,
254
unused: true,
255
strict: true
256
};
257
258
const options = { ...defaultOptions, ...userOptions };
259
260
// Common globals
261
const globals = {
262
console: false, // read-only
263
require: false, // read-only (Node.js)
264
module: true, // writable (Node.js)
265
exports: true // writable (Node.js)
266
};
267
268
// Run linting
269
const isClean = JSHINT(sourceCode, options, globals);
270
271
// Collect results
272
const results = {
273
success: isClean,
274
errors: JSHINT.errors || [],
275
data: JSHINT.data()
276
};
277
278
return results;
279
}
280
281
// Usage
282
const code = `
283
function add(a, b) {
284
return a + b;
285
}
286
module.exports = add;
287
`;
288
289
const results = lintCode(code);
290
if (results.success) {
291
console.log('Code passed linting!');
292
} else {
293
console.log('Linting errors:', results.errors);
294
}
295
```