sindresorhus / grunt-eslint

Validate files with ESLint
MIT License
200 stars 72 forks source link

w/ grunt watch, just lint the changed file(s) #156

Closed calvinjuarez closed 6 years ago

calvinjuarez commented 6 years ago

I'm using this in tandem with grunt-contrib-watch and trying to get Grunt to only lint the file that changed.

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    //...
    eslint: {
      target: ['path/to/js/'],
      options: {
        configFile: '.eslintrc.js',
      },
    },
    //...
    watch: {
      //...
      eslint: {
        files: ['path/to/js/**/*.js'],
        tasks: ['eslint'],
        options: {
          event: ['changed','added'],
        },
      },
    },
  })

  grunt.event.on('watch', function(action, filepath) {
    grunt.config('eslint.target', filepath) // when 'watch' triggers, set the eslint.target to just the changed file
  })

  //...
}

But when I save a file matching the pattern, it lints every file, not just the one that changed. What am I doing wrong?

Note that this example was lifted almost verbatim from the recommendation made by grunt-contrib-watch at https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed.

sindresorhus commented 6 years ago

This question is better asked on Stack Overflow or the grunt-contrib-watch repo. This is just a normal Grunt task. Any issues with watching are with grunt-contrib-watch or your config.