grunt-contrib-jshint = function (grunt) { var path = require('path'); var hooker = require('hooker'); var jshint = require('./lib/jshint').init(grunt); grunt.registerMultiTask('jshint', 'Validate files with JSHint.', function() { var done = this.async(); // Merge task-specific and/or target-specific options with these defaults. var options = this.options({ force: false, reporter: null, reporterOutputRelative: true }); // Report JSHint errors but dont fail the task var force = options.force; delete options.force; // Whether to output the report to a file var reporterOutput = options.reporterOutput; // Hook into stdout to capture report var output = ''; if (reporterOutput) { hooker.hook(process.stdout, 'write', { pre: function(out) { output += out; return hooker.preempt(); } }); } jshint.lint(this.filesSrc, options, function(results, data) { var failed = 0; if (results.length > 0) { // Fail task if errors were logged except if force was set. failed = force; if (jshint.usingGruntReporter === true) { var numErrors = grunt.util._.reduce(results, function(memo, result) { return memo + (result.error ? 1 : 0); }, 0); var numFiles = data.length; grunt.log.error(numErrors + ' ' + grunt.util.pluralize(numErrors, 'error/errors') + ' in ' + numFiles + ' ' + grunt.util.pluralize(numFiles, 'file/files')); } } else { if (jshint.usingGruntReporter === true && data.length > 0) { grunt.log.ok(data.length + ' ' + grunt.util.pluralize(data.length, 'file/files') + ' lint free.'); } } // Write the output of the reporter if wanted if (reporterOutput) { hooker.unhook(process.stdout, 'write'); reporterOutput = grunt.template.process(reporterOutput); var destDir = path.dirname(reporterOutput); if (!grunt.file.exists(destDir)) { grunt.file.mkdir(destDir); } grunt.file.write(reporterOutput, output); grunt.log.ok('Report "' + reporterOutput + '" created.'); } done(failed); }); }); }
n/a
init = function (grunt) { var exports = { usingGruntReporter: false }; var pad = function(msg, length) { while (msg.length < length) { msg = ' ' + msg; } return msg; }; // Select a reporter (if not using the default Grunt reporter) // Copied from jshint/src/cli/cli.js until that part is exposed exports.selectReporter = function(options) { switch (true) { // JSLint reporter case options.reporter === 'jslint': case options['jslint-reporter']: options.reporter = 'jshint/src/reporters/jslint_xml.js'; break; // CheckStyle (XML) reporter case options.reporter === 'checkstyle': case options['checkstyle-reporter']: options.reporter = 'jshint/src/reporters/checkstyle.js'; break; // Reporter that displays additional JSHint data case options['show-non-errors']: options.reporter = 'jshint/src/reporters/non_error.js'; break; // Custom reporter case options.reporter !== null && options.reporter !== undefined: options.reporter = path.resolve(process.cwd(), options.reporter.toString()); } var reporter; if (options.reporter) { try { reporter = require(options.reporter).reporter; exports.usingGruntReporter = false; } catch (err) { grunt.fatal(err); } } // Use the default Grunt reporter if none are found if (!reporter) { reporter = exports.reporter; exports.usingGruntReporter = true; } return reporter; }; // Default Grunt JSHint reporter exports.reporter = function(results, data) { // Dont report empty data as its an ignored file if (data.length < 1) { grunt.log.error('0 files linted. Please check your ignored files.'); return; } if (results.length === 0) { // Success! grunt.verbose.ok(); return; } var options = data[0].options; grunt.log.writeln(); var lastfile = null; // Iterate over all errors. results.forEach(function(result) { // Only print file name once per error if (result.file !== lastfile) { grunt.log.writeln(chalk.bold(result.file ? ' ' + result.file : '')); } lastfile = result.file; var e = result.error; // Sometimes there's no error object. if (!e) { return; } if (e.evidence) { // Manually increment errorcount since we're not using grunt.log.error(). grunt.fail.errorcount++; // No idea why JSHint treats tabs as options.indent # characters wide, but it // does. See issue: https://github.com/jshint/jshint/issues/430 // Replacing tabs with appropriate spaces (i.e. columns) ensures that // caret will line up correctly. var evidence = e.evidence.replace(/\t/g, grunt.util.repeat(options.indent, ' ')); grunt.log.writeln(pad(e.line.toString(), 7) + ' |' + chalk.gray(evidence)); grunt.log.write(grunt.util.repeat(9, ' ') + grunt.util.repeat(e.character - 1, ' ') + '^ '); grunt.verbose.write('[' + e.code + '] '); grunt.log.writeln(e.reason); } else { // Generic "Whoops, too many errors" error. grunt.log.error(e.reason); } }); grunt.log.writeln(); }; // Run JSHint on the given files with the given options exports.lint = function(files, options, done) { var cliOptions = { verbose: grunt.option('verbose'), extensions: '' }; // A list of non-dot-js extensions to check if (options.extensions) { cliOptions.extensions = options.extensions; delete options.extensions; } // A list ignored files if (options.ignores) { if (typeof options.ignores === 'string') { options.ignores = [options.ignores]; } cliOptions.ignores = options.ignores; delete options.ignores; } // Option to extract JS from HTML file if (options.extract) { cliOptions.extract = options.extract; delete options.extract; } var reporterOutputDir; // Get reporter output directo ...
...
'use strict';
module.exports = function(grunt) {
var path = require('path');
var hooker = require('hooker');
var jshint = require('./lib/jshint').init(grunt);
grunt.registerMultiTask('jshint', 'Validate files with JSHint.', function() {
var done = this.async();
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
force: false,
...