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

6k views Asked by At

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?

3

There are 3 answers

3
Sven 31415 On BEST ANSWER

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'
  ]
}
0
Sibiraj On

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

0
Mm.Mirzaei.dev On

If it didn't

extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
          options: {
            fix: true
          }
        })
      }
    }
You. You can use this on the first page
    const colors = require('vuetify/es5/util/colors').default
    const pkg = require('./package')
    require('dotenv').config()

    module.exports = {
      mode: 'universal',
      /*
      ** Headers of the page
      */
      head: {
        titleTemplate: '%s - ' + process.env.npm_package_name,
        title: process.env.npm_package_name || '',
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          {
            hid: 'description',
            name: 'description',
            content: process.env.npm_package_description || ''
          }
        ],
        link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
      },
      /*
      ** Customize the progress-bar color
      */
      loading: { color: '#fff' },
      /*
      ** Global CSS
      */
      css: [],
      /*
      ** Plugins to load before mounting the App
      */
      plugins: [],
      /*
      ** Nuxt.js dev-modules
      */
      buildModules: [
        // Doc: https://github.com/nuxt-community/eslint-module
        '@nuxtjs/eslint-module',
        '@nuxtjs/vuetify'
      ],
      /*
      ** Nuxt.js modules
      */
      modules: [
        // Doc: https://axios.nuxtjs.org/usage
        '@nuxtjs/axios',
        '@nuxtjs/pwa',
        // Doc: https://github.com/nuxt-community/dotenv-module
        '@nuxtjs/dotenv'
      ],
      /*
      ** Axios module configuration
      ** See https://axios.nuxtjs.org/options
      */
      axios: {},
      /*
      ** vuetify module configuration
      ** https://github.com/nuxt-community/vuetify-module
      */
      /*
      ** Build configuration
      */
      build: {
        extend(config, ctx) {
          // Run ESLint on save
          if (ctx.isDev && ctx.isClient) {
            config.module.rules.push({
              enforce: 'pre',
              test: /\.(js|vue)$/,
              loader: 'eslint-loader',
              exclude: /(node_modules)/,
              options: {
                fix: true
              }
            })
          }
        }
      }
    }