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; } // Select a reporter to use var reporter = exports.selectReporter(options); delete options.reporter; // Read JSHint options from a specified jshintrc file. if (options.jshintrc) { options = grunt.file.readJSON(options.jshintrc); delete options.jshintrc; } // Enable/disable debugging if option explicitly set. if (grunt.option('debug') !== undefined) { options.devel = options.debug = grunt.option('debug'); // Tweak a few things. if (grunt.option('debug')) { options.maxerr = Infinity; } } cliOptions.config = options; grunt.verbose.writeflags(options, 'JSHint options'); // Run JSHint on all file and collect results/data var allResults = []; var allData = []; var cliopts = grunt.util._.clone(cliOptions); cliopts.args = files; cliopts.reporter = function(results, data) { reporter(results, data); allResults = allResults.concat(results); allData = allData.concat(data); }; jshintcli.run(cliopts); done(allResults, allData); };
grunt.util.async.forEach(files, function(filepath, next) { jshintcli.run({ args: [filepath], extensions: extraExt, config: options, reporter: function(results, data) { reporter(results, data); allResults = allResults.concat(results); allData = allData.concat(data); next(); }, verbose: grunt.option('verbose') }); }, function() {
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; } // Select a reporter to use var reporter = exports.selectReporter(options); // Remove bad options that may have came in from the cli ['reporter', 'jslint-reporter', 'checkstyle-reporter', 'show-non-errors'].forEach(function(opt) { if (options.hasOwnProperty(opt)) { delete options[opt]; } }); // Read JSHint options from a specified jshintrc file. if (options.jshintrc) { options = grunt.file.readJSON(options.jshintrc); delete options.jshintrc; } // Enable/disable debugging if option explicitly set. if (grunt.option('debug') !== undefined) { options.devel = options.debug = grunt.option('debug'); // Tweak a few things. if (grunt.option('debug')) { options.maxerr = Infinity; } } cliOptions.config = options; // Run JSHint on all file and collect results/data var allResults = []; var allData = []; var cliopts = grunt.util._.clone(cliOptions); cliopts.args = files; cliopts.reporter = function(results, data) { reporter(results, data); allResults = allResults.concat(results); allData = allData.concat(data); }; jshintcli.run(cliopts); done(allResults, allData); };
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, output: null, jshintrc: null, ignores: null, extensions: '', reporter: null }); // Get source and destination files var srcFiles = this.files[0].src; var destFile = this.files[0].dest; // Prepare reporter object var reporter = options.reporter; switch (true) { // Checkstyle xml case reporter === 'checkstyle': reporter = require('jshint/src/reporters/checkstyle.js').reporter; break; // jslint xml reporter case reporter === 'jslint': reporter = require('jshint/src/reporters/jslint_xml.js').reporter; break; // JSHint non_error case reporter === 'non_error': reporter = require('jshint/rc/reporters/non_error.js').reporter; break; // JSHint default reporter case reporter === 'default': reporter = require('jshint/src/reporters/default.js').reporter; break; // Custom reporter case reporter !== undefined: reporter = path.resolve(process.cwd(), options.reporter); break; } // Read JSHint options from a specified jshintrc file. var config = undefined; if (options.jshintrc) { config = grunt.file.readJSON(options.jshintrc); } // Hook into stdout to capture report var data = ''; if (destFile) { grunt.util.hooker.hook(process.stdout, 'write', { pre: function (out) { data += out; return grunt.util.hooker.preempt(); } }); } // Run JSHint through the cli var result = jshint.run({ args: this.filesSrc, config: config, reporter: reporter, ignores: options.ignores, extensions: options.extensions }); // Write the output of the reporter if wanted if (destFile) { grunt.util.hooker.unhook(process.stdout, 'write'); destFile = grunt.template.process(destFile); var destDir = path.dirname(destFile); if (!grunt.file.exists(destDir)) { grunt.file.mkdir(destDir); } grunt.file.write(destFile, data); grunt.log.ok('Report "' + destFile + '" created.'); } done(options.force ? options.force : result); });