説明なし

jshint.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * grunt-contrib-jshint
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. var path = require('path');
  11. var hooker = require('hooker');
  12. var jshint = require('./lib/jshint').init(grunt);
  13. grunt.registerMultiTask('jshint', 'Validate files with JSHint.', function() {
  14. var done = this.async();
  15. // Merge task-specific and/or target-specific options with these defaults.
  16. var options = this.options({
  17. force: false,
  18. reporterOutput: null,
  19. });
  20. // Report JSHint errors but dont fail the task
  21. var force = options.force;
  22. delete options.force;
  23. // Whether to output the report to a file
  24. var reporterOutput = options.reporterOutput;
  25. // Hook into stdout to capture report
  26. var output = '';
  27. if (reporterOutput) {
  28. hooker.hook(process.stdout, 'write', {
  29. pre: function(out) {
  30. output += out;
  31. return hooker.preempt();
  32. }
  33. });
  34. }
  35. jshint.lint(this.filesSrc, options, function(results, data) {
  36. var failed = 0;
  37. if (results.length > 0) {
  38. // Fail task if errors were logged except if force was set.
  39. failed = force;
  40. if (jshint.usingGruntReporter === true) {
  41. var numErrors = grunt.util._.reduce(results,function(memo,result){
  42. return memo + (result.error ? 1 : 0);
  43. },0);
  44. var numFiles = data.length;
  45. grunt.log.error(numErrors + ' ' + grunt.util.pluralize(numErrors,'error/errors') + ' in ' +
  46. numFiles + ' ' + grunt.util.pluralize(numFiles,'file/files'));
  47. }
  48. } else {
  49. if (jshint.usingGruntReporter === true && data.length > 0) {
  50. grunt.log.ok(data.length + ' ' + grunt.util.pluralize(data.length,'file/files') + ' lint free.');
  51. }
  52. }
  53. // Write the output of the reporter if wanted
  54. if (reporterOutput) {
  55. hooker.unhook(process.stdout, 'write');
  56. reporterOutput = grunt.template.process(reporterOutput);
  57. var destDir = path.dirname(reporterOutput);
  58. if (!grunt.file.exists(destDir)) {
  59. grunt.file.mkdir(destDir);
  60. }
  61. grunt.file.write(reporterOutput, output);
  62. grunt.log.ok('Report "' + reporterOutput + '" created.');
  63. }
  64. done(failed);
  65. });
  66. });
  67. };