Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt-eslint & enabling `--fix` flag to auto fix violations

I know eslint CLI itself has a --fix flag, but I can't tell from the documentation how to use this via eslintConfig (in package.json) or in the grunt-eslint configuration in my Gruntfile.

I have the following config in package.json:

"env": {
  "browser": true,
  "amd": true
},
"extends": "eslint:recommended",

and invoke it via a lint task using this Grunt config:

    eslint: {
        target: [
            'src/app/**/*.js'
        ],
        format: 'checkstyle'
    },

How can I enable the --fix flag in this scenario?

like image 369
Alex Avatar asked May 10 '16 08:05

Alex


2 Answers

For the --fix flag, you only have to add an options: { fix: true } to your gruntfile.

Here is an example of my gruntfile eslint task (grunt-eslint 18.1.0 with eslint 2.12.0):

eslint: {
  options: {
    configFile: '.eslintrc.json',
    format: 'html',
    outputFile: 'report.html',
    fix: true
  },
  target: [
    'routes/**',
    'server.js',
    'gruntfile.js'
  ]
}
like image 184
Sven 31415 Avatar answered Sep 22 '22 17:09

Sven 31415


Adding to the answer, If You don't wan't always to fix, You could pass the flag to the grunt like

grunt eslint --fix

And in the grunt config for eslint

eslint: {
  options: {
    fix: grunt.option('fix') // this will get params from the flags
  }
}

So Running grunt eslint won't fix anything. You have to run grunt eslint --fix for eslint to fix errors.

Read More about grunt.option

like image 21
Sibiraj Avatar answered Sep 20 '22 17:09

Sibiraj