Task lint
Validate files with JSHint.
About ⚑
This task is a multi task, meaning that grunt will automatically iterate over all lint
targets if a target is not specified.
Need some help getting started with grunt? Visit the getting started page as well as the [API documentation|api]].
A Very Important Note ⚑
Your grunt.js
gruntfile must contain this code, once and only once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
module.exports = function(grunt) {
// Your grunt code goes in here.
};
Project configuration ⚑
This example shows a brief overview of the config properties used by the lint
task. For a more in-depth explanation, see the usage examples.
// Project configuration.
grunt.initConfig({
// Lists of files to be linted with JSHint.
lint: {}
});
Usage examples ⚑
Wildcards ⚑
In this example, running grunt lint
will lint the project's gruntfile as well as all JavaScript files in the lib
and test
directories, using the default JSHint options
and globals
.
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'lib/*.js', 'test/*.js']
}
});
With a slight modification, running grunt lint
will also lint all JavaScript files in the lib
and test
directories and all subdirectories. See the minimatch module documentation for more details on wildcard patterns.
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
}
});
Linting before and after concat ⚑
In this example, running grunt lint
will lint two separate sets of files using the default JSHint options
and globals
: one "beforeconcat" set, and one "afterconcat" set. Running grunt lint
will lint both sets of files all at once, because lint is a multi task. This is not ideal, because dist/output.js
may get linted before it gets created via the [concat task|Task-concat]]!
In this case, you should lint the "beforeconcat" set first, then concat, then lint the "afterconcat" set, by running grunt lint:beforeconcat concat lint:afterconcat
.
// Project configuration.
grunt.initConfig({
concat: {
dist: {
src: ['src/foo.js', 'src/bar.js'],
dest: 'dist/output.js'
}
},
lint: {
beforeconcat: ['src/foo.js', 'src/bar.js'],
afterconcat: ['dist/output.js']
}
});
// Default task.
grunt.registerTask('default', 'lint:beforeconcat concat lint:afterconcat');
Note: in the above example, a default alias task was created that runs the 'lint:beforeconcat', 'concat' and 'lint:afterconcat' tasks. If you didn't want this to be the default grunt task, you could give it a different name.
Dynamic filenames ⚑
Building on the previous example, if you want to avoid duplication, you can use a directive like '<config:concat.dist.dest>'
in place of 'dist/output.js'
in the afterconcat
lint target. This allows you to generate the output filename dynamically. In this example, the concat:dist
destination filename is generated from the name
and version
properties of the referenced package.json
file through the pkg
config property.
// Project configuration.
grunt.initConfig({
pkg: '<json:package.json>',
concat: {
dist: {
src: ['src/foo.js', 'src/bar.js'],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
}
},
lint: {
beforeconcat: ['src/foo.js', 'src/bar.js'],
afterconcat: ['<config:concat.dist.dest>']
}
});
Specifying JSHint options and globals ⚑
In this example, taken from the Sample jQuery plugin gruntfile, custom JSHint options
and globals
are specified. These options are explained in the JSHint documentation.
Note: config jshint.options
and jshint.globals
apply to the entire project, but can be overridden with per-file comments like /*global exports:false*/
.
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'src/**/*.js', 'test/**/*.js']
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
eqnull: true,
browser: true
},
globals: {
jQuery: true
}
},
});
Per-target JSHint options and globals ⚑
For each lint
target, grunt looks for a target-named object underneath the jshint
config object. If this object is found, its options
and globals
sub-objects will be used instead of the global ones. this allows per-lint-target JSHint options/globals overrides.
In this example, there are default JSHint settings, as well as per-target overrides:
// Project configuration.
grunt.initConfig({
lint: {
src: 'src/*.js',
grunt: 'grunt.js',
tests: 'tests/unit/**/*.js'
},
jshint: {
// Defaults.
options: {curly: true},
globals: {},
// Just for the lint:grunt target.
grunt: {
options: {node: true},
globals: {task: true, config: true, file: true, log: true, template: true}
},
// Just for the lint:src target.
src: {
options: {browser: true},
globals: {jQuery: true}
},
// Just for the lint:tests target.
tests: {
options: {jquery: true},
globals: {module: true, test: true, ok: true, equal: true, deepEqual: true, QUnit: true}
}
}
});
Helpers ⚑
A generic lint
helper is available for use in any other task where file linting might be useful. For example:
var filename = 'example.js';
var src = grunt.file.read(filename);
grunt.helper('lint', src, {browser: true}, {jQuery: true}, filename);
See the lint task source for more information.
Getting started ⚑
- Getting started - A guide to configuring grunt for your project.
- Example gruntfiles - A few samples, to help you get started.
- The grunt API - Complete API documentation here!
- FAQ - Frequently asked questions, along with their answers.
- Exit codes - A list of all built-in grunt exit codes.
Built-in tasks ⚑
- concat - Concatenate files.
- init - Generate project scaffolding from a predefined template.
- lint - Validate files with JSHint.
- min - Minify files with UglifyJS.
- qunit - Run QUnit unit tests in a headless PhantomJS instance.
- server - Start a static web server.
- (more documentation coming soon)
Other built-ins ⚑
- Helpers and Directives - Ones not specifically covered in other task documentation.
Development ⚑
- Contributing to grunt - Development advice.