Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JSHint for a single file in Grunt

Tags:

gruntjs

jshint

I have JSHint setup with multiple subtasks (src, lib, and test) in Grunt which works great. However, as we're just starting out using this setup there are a lot of errors in our many source files.

$ grunt jshint:src
... lots of errors ...

While working on one file at a time, is it possible to re-lint that single file?

$ grunt jshint:src:one.js
... only errors from one.js ...

Update

One complication is that the watch task also has multiple subtasks to fire off different tasks based on which type of file is edited.

watch: {
    src: {
        files: [ SRC_DIR + "hgm*.js" ],
        tasks: [ "jshint:src", "test" ]
    },
    lib: {
        files: [ "lib/hgm-test-setup.js", "lib/hgm.loader.js" ],
        tasks: [ "jshint:lib", "test" ]
    },
    test: {
        files: [ "tests/**/*.js" ],
        tasks: [ "jshint:test", "test" ]
    }
}

The reason for this is that src and lib use one .jshint while test uses a different one that specifies all the globals used for testing such as assertions. I can combine src and lib, but can I override the JSHint config file for test?

like image 725
David Harkness Avatar asked Oct 03 '22 19:10

David Harkness


1 Answers

the grunt-contrib-watch-task, provides an example how to configure your tasks, and use the watch-event for linting only changed files:

grunt.initConfig({
  watch: {
    scripts: {
      files: ['lib/*.js'],
      tasks: ['jshint'],
      options: {
        nospawn: true,
      },
    },
  },
  jshint: {
    all: ['lib/*.js'],
  },
});

// on watch events configure jshint:all to only run on changed file
grunt.event.on('watch', function(action, filepath) {
  grunt.config(['jshint', 'all'], filepath);
});
like image 138
hereandnow78 Avatar answered Oct 09 '22 15:10

hereandnow78