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

Ale*_*lex 14 javascript gruntjs eslint grunt-eslint

我知道eslintCLI本身有一个--fix标志,但我无法从文档中看出如何在我的Gruntfile中使用this via eslintConfig(in package.json)或grunt-eslint配置.

我有以下配置package.json:

"env": {
  "browser": true,
  "amd": true
},
"extends": "eslint:recommended",
Run Code Online (Sandbox Code Playgroud)

lint使用此Grunt配置通过任务调用它:

    eslint: {
        target: [
            'src/app/**/*.js'
        ],
        format: 'checkstyle'
    },
Run Code Online (Sandbox Code Playgroud)

如何--fix在此方案中启用该标志?

Sve*_*415 16

对于--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'
  ]
}
Run Code Online (Sandbox Code Playgroud)


Sib*_*raj 6

添加到答案中,如果您不想总是修复,您可以将标志传递给咕噜声

grunt eslint --fix
Run Code Online (Sandbox Code Playgroud)

在 eslint 的 grunt 配置中

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

所以跑步grunt eslint不会解决任何问题。你必须运行grunt eslint --fixeslint 来修复错误。

阅读更多关于grunt.option