Grunt plugin that provides JSHint integration for automated JavaScript code quality validation
npx @tessl/cli install tessl/npm-grunt-contrib-jshint@3.2.0grunt-contrib-jshint is a Grunt plugin that provides JSHint integration for automated JavaScript code quality validation. It enables developers to lint JavaScript files using JSHint rules and configurations, supporting various options like custom reporters, output formatting, global variable declarations, and .jshintrc configuration files.
npm install grunt-contrib-jshint --save-dev// Load the plugin in your Gruntfile.js
grunt.loadNpmTasks('grunt-contrib-jshint');
// Or using require to access the library directly (less common)
const jshint = require('grunt-contrib-jshint/tasks/lib/jshint');// Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
options: {
curly: true,
eqeqeq: true,
browser: true,
globals: {
jQuery: true
}
},
all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
// Run with: grunt jshint
};grunt-contrib-jshint consists of two main components:
Registers the 'jshint' multi-task with Grunt, enabling JSHint validation within Grunt build processes.
/**
* Main plugin registration function exported by tasks/jshint.js
* @param grunt - Grunt instance
*/
function(grunt) {
// Registers 'jshint' multi-task
grunt.registerMultiTask('jshint', 'Validate files with JSHint.', taskFunction);
}Core wrapper library that provides JSHint functionality integrated with Grunt.
/**
* Initialize the JSHint wrapper library
* @param grunt - Grunt instance
* @returns JSHint wrapper object with exposed methods
*/
function init(grunt) {
return {
lint: function(files, options, done) { /* ... */ },
selectReporter: function(options) { /* ... */ },
reporter: function(results, data) { /* ... */ },
usingGruntReporter: boolean
};
}
/**
* Run JSHint on specified files with given options
* @param files - Array of file paths to lint
* @param options - JSHint and plugin configuration options
* @param done - Callback function called with (results, data) where results is JshintResult[] and data is file data array
*/
function lint(files, options, done) {
// Executes JSHint and calls done(results, data)
}
/**
* Select and return appropriate reporter based on options
* @param options - Options object containing reporter configuration
* @returns Reporter function
*/
function selectReporter(options) {
// Returns reporter function based on options.reporter
}
/**
* Default Grunt reporter for JSHint results
* @param results - Array of JSHint error/warning results
* @param data - Array of file data processed by JSHint
*/
function reporter(results, data) {
// Outputs formatted results to Grunt console
}Configuration options for the jshint Grunt task.
interface JshintTaskOptions {
// Core JSHint Options (all JSHint options are supported)
curly?: boolean; // Require curly braces
eqeqeq?: boolean; // Require === and !==
eqnull?: boolean; // Allow == null comparisons
undef?: boolean; // Require variables to be declared
unused?: boolean; // Warn about unused variables
strict?: boolean; // Require strict mode
browser?: boolean; // Browser environment globals
node?: boolean; // Node.js environment globals
jquery?: boolean; // jQuery globals
devel?: boolean; // Development globals (console, alert, etc)
debug?: boolean; // Debug globals
// Plugin-specific Options
force?: boolean; // Report errors but don't fail task (default: false)
reporter?: string | function; // Custom reporter path or function (default: null)
reporterOutput?: string; // File path to output reporter results (default: null)
reporterOutputRelative?: boolean; // Use relative paths in output (default: true)
'show-non-errors'?: boolean; // Use reporter showing additional JSHint data
// JSHint Configuration
globals?: { [key: string]: boolean }; // Global variables with assignability flags
jshintrc?: string | boolean; // Path to .jshintrc or true for auto-discovery
extensions?: string; // Non-JS file extensions to check
ignores?: string[]; // Files/directories to ignore
extract?: string; // Extract JS from HTML files
}Built-in reporter types available through the reporter option.
type BuiltInReporter =
| 'jslint' // JSLint XML reporter
| 'checkstyle'; // CheckStyle XML reporterStructure for configuring the jshint task in Gruntfile.js.
interface JshintTaskConfig {
options?: JshintTaskOptions; // Global options for all targets
[target: string]: { // Named targets
options?: JshintTaskOptions; // Target-specific options
src?: string[]; // File patterns to lint
files?: { // Alternative file specification
src: string[];
};
} | JshintTaskOptions | string[];
}Structure of JSHint error/warning results returned by the lint function.
interface JshintResult {
file: string; // File path where error occurred
error: {
line: number; // Line number
character: number; // Character position
reason: string; // Error/warning message
evidence?: string; // Code line that caused the error
code?: string; // JSHint error code (e.g., 'W015')
} | null;
}Basic file pattern configuration:
grunt.initConfig({
jshint: {
all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
}
});Multiple targets with different options:
grunt.initConfig({
jshint: {
options: {
curly: true,
eqeqeq: true,
browser: true,
globals: {
jQuery: true
}
},
beforeconcat: ['src/foo.js', 'src/bar.js'],
afterconcat: {
options: {
curly: false,
undef: true
},
files: {
src: ['dist/output.js']
}
}
}
});Custom reporter configuration:
grunt.initConfig({
jshint: {
options: {
reporter: require('jshint-stylish'),
reporterOutput: 'reports/jshint.txt'
},
src: ['src/**/*.js']
}
});Using .jshintrc file:
grunt.initConfig({
jshint: {
options: {
jshintrc: true // Auto-discover .jshintrc files
// OR
// jshintrc: '.jshintrc' // Specific path
},
src: ['src/**/*.js']
}
});Ignoring specific warnings:
grunt.initConfig({
jshint: {
options: {
'-W015': true // Ignore indentation warnings
},
src: ['src/**/*.js']
}
});