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

17

我知道 eslint 的CLI本身有一个--fix标志,但我无法从文档中了解如何通过eslintConfig(在package.json中)或在我的Gruntfile中的grunt-eslint配置中使用它。

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

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

通过这个Grunt配置,使用一个lint任务来调用它:

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

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

在这种情况下,我该如何启用--fix标志呢?

3个回答

17

对于--fix标志,您只需将options: { fix: true }添加到您的gruntfile中即可。

这是我的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'
  ]
}

你能否确认在使用 eslint 3 和 grunt-eslint 时它仍然有效?谢谢。 - alecxe
这在使用Grunt时有效。如果使用package.json配置,类似的选项可用会更好。 - Alex
1
如何添加一些规则来修复问题,我不想将所有问题都使用 fix:true 修复。 - Arun

7

补充一下,如果您不想总是修复问题,可以像这样将标志传递给Grunt:

grunt eslint --fix

在eslint的Grunt配置中

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

执行 grunt eslint 并不能修复任何问题。你需要运行 grunt eslint --fix 命令来修复 eslint 报告中的错误。

查阅更多关于 grunt.option 的信息。


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接