jshint.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * grunt-contrib-jshint
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2012 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. // Internal lib.
  11. var jshint = require('./lib/jshint').init(grunt);
  12. grunt.registerMultiTask('jshint', 'Validate files with JSHint.', function() {
  13. // Merge task-specific and/or target-specific options with these defaults.
  14. var options = this.options();
  15. // Read JSHint options from a specified jshintrc file.
  16. if (options.jshintrc) {
  17. options = grunt.file.readJSON(options.jshintrc);
  18. }
  19. // If globals weren't specified, initialize them as an empty object.
  20. if (!options.globals) {
  21. options.globals = {};
  22. }
  23. // Convert deprecated "predef" array into globals.
  24. if (options.predef) {
  25. options.predef.forEach(function(key) {
  26. options.globals[key] = true;
  27. });
  28. delete options.predef;
  29. }
  30. // Extract globals from options.
  31. var globals = options.globals;
  32. delete options.globals;
  33. grunt.verbose.writeflags(options, 'JSHint options');
  34. grunt.verbose.writeflags(globals, 'JSHint globals');
  35. // Lint specified files.
  36. var files = this.filesSrc;
  37. files.forEach(function(filepath) {
  38. jshint.lint(grunt.file.read(filepath), options, globals, filepath);
  39. });
  40. // Fail task if errors were logged.
  41. if (this.errorCount) { return false; }
  42. // Otherwise, print a success message.
  43. grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.');
  44. });
  45. };