簡體   English   中英

Grunt-eslint 並啟用 `--fix` 標志以自動修復違規

[英]Grunt-eslint & enabling `--fix` flag to auto fix violations

我知道eslint CLI 本身有一個--fix標志,但我無法從文檔中得知如何通過eslintConfig (在package.json )或在我的 Gruntfile 中的 grunt-eslint 配置中使用它。

我在package.json有以下配置:

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

並使用此 Grunt 配置通過lint任務調用它:

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

在這種情況下如何啟用--fix標志?

對於--fix標志,您只需在--fix中添加一個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'
  ]
}

添加到答案中,如果您不想總是修復,您可以將標志傳遞給咕嚕聲

grunt eslint --fix

在 eslint 的 grunt 配置中

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

所以運行grunt eslint不會解決任何問題。 你必須為 eslint 運行grunt eslint --fix --fix 來修復錯誤。

閱讀更多關於grunt.option

如果沒有

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM