Static analysis tool for JavaScript that detects errors and potential problems in code
npx @tessl/cli install tessl/npm-jshint@2.13.0JSHint 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.
npm install jshintconst { JSHINT } = require('jshint');For CLI usage via binary:
jshintconst { JSHINT } = require('jshint');
// Basic linting
const code = `
function hello(name) {
console.log("Hello " + name);
}
`;
const options = {
esversion: 6,
undef: true,
unused: true
};
const globals = {
console: false // console is read-only global
};
const result = JSHINT(code, options, globals);
if (!result) {
// Errors found
console.log(JSHINT.errors);
} else {
// No errors
console.log("Code is clean!");
}
// Get detailed analysis data
const data = JSHINT.data();
console.log(data.functions, data.globals, data.unused);JSHint is built around several key components:
JSHINT function that analyzes JavaScript code and returns boolean resultsPrimary JavaScript code analysis functionality that performs static analysis and error detection.
/**
* Main linting function that analyzes JavaScript code
* @param source - JavaScript source code (string or array of strings)
* @param options - Configuration options object (optional)
* @param globals - Global variables declaration object (optional)
* @returns boolean - true if no errors found, false if errors found
*/
function JSHINT(source, options, globals);
// Properties available after calling JSHINT function
JSHINT.errors; // Array of error objects
JSHINT.scope; // Current scope name (string)
JSHINT.internals; // Array of internal eval code
JSHINT.blacklist; // Object of blacklisted variables
/**
* Returns detailed analysis data from the last linting run
* @returns object with comprehensive analysis results
*/
JSHINT.data();
/**
* Adds external modules to the linting process
* @param func - Module function to add to JSHint
*/
JSHINT.addModule(func);
// Self-reference to the JSHINT function
JSHINT.jshint;Complete CLI functionality for file processing, configuration management, and output formatting.
/**
* Main CLI entry point that parses arguments and runs JSHint
* @param args - Command line arguments array (process.argv format)
*/
function interpret(args);
/**
* Runs linting with processed CLI options
* @param options - Processed CLI options object
* @param callback - Optional completion callback function
* @returns boolean result or null for async operations
*/
function run(options, callback);
/**
* Gathers files to be linted based on patterns and options
* @param options - File gathering options object
* @returns Array of file paths to be processed
*/
function gather(options);
/**
* Retrieves configuration for a specific file path
* @param filepath - File path for configuration lookup
* @returns Configuration object merged from various sources
*/
function getConfig(filepath);
/**
* Loads and parses a JSHint configuration file
* @param filepath - Path to configuration file
* @returns Parsed configuration object
*/
function loadConfig(filepath);
/**
* Extracts JavaScript code from HTML script tags
* @param code - Source code string (potentially HTML)
* @param option - Extraction mode ('always'|'auto'|'never')
* @returns Extracted JavaScript code string
*/
function extract(code, option);
/**
* Exits the process with specified code
* @param code - Exit code number
*/
function exit(code);
/**
* Helper function for testing stdout buffer size
* @returns Current buffer size number
*/
function getBufferSize();Comprehensive configuration system with enforcing, relaxing, and environment options for customizing linting behavior.
// JSHint Configuration Options Object
// Enforcing Options (stricter checking)
{
bitwise: boolean, // Prohibit bitwise operators
freeze: boolean, // Prohibit native prototype extension
camelcase: boolean, // Force camelCase (deprecated)
curly: boolean, // Require curly braces
eqeqeq: boolean, // Prohibit == and !=
futurehostile: boolean, // Warn about future reserved words
es3: boolean, // ES3 compliance (deprecated)
es5: boolean, // ES5 compliance (deprecated)
forin: boolean, // Require hasOwnProperty in for-in
immed: boolean, // Require immediate function invocation wrapping
latedef: boolean, // Prohibit use before definition
newcap: boolean, // Require constructor names capitalization
noarg: boolean, // Prohibit arguments.caller/callee
noempty: boolean, // Prohibit empty blocks
nonew: boolean, // Prohibit constructor usage for side effects
plusplus: boolean, // Prohibit ++ and --
quotmark: boolean, // Enforce quote marks
undef: boolean, // Require variable declarations
unused: boolean, // Warn about unused variables
strict: boolean, // Require strict mode
varstmt: boolean, // Disallow var statements
regexpu: boolean, // Enable RegExp u flag support
// Relaxing Options (more permissive)
asi: boolean, // Allow missing semicolons
boss: boolean, // Allow assignment in conditions
debug: boolean, // Allow debugger statements
eqnull: boolean, // Allow == null comparisons
esnext: boolean, // Enable ES6 syntax (deprecated)
evil: boolean, // Allow eval usage
expr: boolean, // Allow expression statements
funcscope: boolean, // Allow variables defined inside control structures
globalstrict: boolean, // Allow global strict mode (deprecated)
iterator: boolean, // Allow __iterator__ property
lastsemic: boolean, // Allow missing semicolon in one-line blocks
laxbreak: boolean, // Allow unsafe line breaks
laxcomma: boolean, // Allow comma-first coding style
loopfunc: boolean, // Allow functions in loops
multistr: boolean, // Allow multiline strings
noyield: boolean, // Allow generators without yield
proto: boolean, // Allow __proto__ property
scripturl: boolean, // Allow script-targeted URLs
shadow: boolean, // Allow variable shadowing
sub: boolean, // Allow subscript notation
supernew: boolean, // Allow new function() and new Object()
validthis: boolean, // Allow this in non-constructor functions
notypeof: boolean, // Allow invalid typeof comparisons
elision: boolean, // Allow array element elision
// Environment Options
browser: boolean, // Browser globals
couch: boolean, // CouchDB globals
devel: boolean, // Development globals (console, alert)
dojo: boolean, // Dojo globals
jasmine: boolean, // Jasmine globals
jquery: boolean, // jQuery globals
mocha: boolean, // Mocha globals
mootools: boolean, // MooTools globals
node: boolean, // Node.js globals
nonstandard: boolean, // Non-standard globals
phantom: boolean, // PhantomJS globals
prototypejs: boolean, // Prototype globals
qunit: boolean, // QUnit globals
rhino: boolean, // Rhino globals
shelljs: boolean, // ShellJS globals
typed: boolean, // Typed Array globals
worker: boolean, // Web Worker globals
wsh: boolean, // Windows Scripting Host globals
yui: boolean, // YUI globals
// Value-based Options
esversion: number, // ECMAScript version
maxlen: number, // Maximum line length
maxerr: number, // Maximum errors
maxdepth: number, // Maximum nested block depth
maxstatements: number, // Maximum statements per function
maxcomplexity: number, // Maximum cyclomatic complexity
maxparams: number, // Maximum parameters per function
indent: number, // Indentation width
predef: Array, // Predefined globals
globals: Object, // Global variables
quotmark: string, // Quote mark preference
shadow: string, // Variable shadowing mode
unused: string, // Unused variable checking mode
latedef: string, // Late definition checking mode
strict: string, // Strict mode requirement
ignore: string, // Ignore warnings by code
ignoreDelimiters: Array // Ignore code blocks
}Pluggable output formatters for different use cases including default, unix, checkstyle, and XML formats.
/**
* Reporter function interface for formatting JSHint output
* @param results - Array of linting result objects
* @param data - Array of detailed analysis data objects
* @param opts - Reporter options object
*/
function reporter(results, data, opts) {
// results: Array of {file: string, error: object} objects
// data: Array of JSHINT.data() objects
// opts: {verbose: boolean, filename: string, ...}
}// Error Object Structure
{
line: number, // Line number where error occurred
character: number, // Character position on line
reason: string, // Human-readable error message
code: string // Error code (e.g., 'W033', 'E041')
}
// Result Object Structure
{
file: string, // File path where error occurred
error: { // Error details object
line: number,
character: number,
reason: string,
code: string
}
}
// Data Object Structure (from JSHINT.data())
{
functions: Array, // Function analysis data
options: Object, // Options used for linting
errors: Array, // Error objects (if any)
implieds: Array, // Implied global variables
globals: Array, // Used/defined global variables
unused: Array, // Unused variables
member: Object, // Member usage data
json: boolean // JSON mode flag
}