Grunt-eslint&启用`--fix`标志以自动修复违规

时间:2016-05-10 08:21:33

标签: javascript gruntjs eslint grunt-eslint

我知道new CLI本身有一个eslint标记,但我无法通过文档告诉我如何通过--fix使用此标记(在eslintConfig中)或者在我的Gruntfile中的grunt-eslint配置中。

我在package.json中有以下配置:

package.json

使用此Grunt配置通过"env": { "browser": true, "amd": true }, "extends": "eslint:recommended", 任务调用它:

lint

如何在此方案中启用 eslint: { target: [ 'src/app/**/*.js' ], format: 'checkstyle' }, 标志?

3 个答案:

答案 0 :(得分:16)

对于--fix标志,您只需要在gruntfile中添加options: { fix: true }

以下是我的gruntfile eslint任务(grunt-eslint 18.1.0eslint 2.12.0)的示例:

eslint: {
  options: {
    configFile: '.eslintrc.json',
    format: 'html',
    outputFile: 'report.html',
    fix: true
  },
  target: [
    'routes/**',
    'server.js',
    'gruntfile.js'
  ]
}

答案 1 :(得分:2)

添加到答案中,如果您不总是要修复,则可以将标志传递给咕unt声,就像

grunt eslint --fix

在eslint的grunt配置中

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

因此,运行grunt eslint无法解决任何问题。您必须运行grunt eslint --fix才能保留文本,以纠正错误。

详细了解grunt.option

答案 2 :(得分:0)

如果没有

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
              }
            })
          }
        }
      }
    }