简体   繁体   English

Gulp eslint 找不到我的 .eslintrc 文件

[英]Gulp eslint doesn't find my .eslintrc file

It looks like my .eslintrc file is not found my gulp-eslint看起来我的.eslintrc文件没有找到我gulp-eslint

I defined a lint task:我定义了一个lint任务:

gulp.task('lint', function () {
  gulp.src(['src/**/*.js', 'src/**/*.jsx'])
  .pipe(eslint())
  .pipe(eslint.format());
})

It runs but doesn't show any error.它运行但不显示任何错误。

My .eslintrc file is defined in src folder.我的.eslintrc文件在src文件夹中定义。 I tried to move it to the root folder of my project but it didn't change anything.我试图将它移动到我的项目的根文件夹,但它没有改变任何东西。

It's a pretty simple file:这是一个非常简单的文件:

{
  "parser": "babel-eslint",
  "ecmaFeatures": {
    "classes": true,
    "jsx": true
  },
  "plugins": [
    "react"
  ],

  "extends": "eslint-config-airbnb"
}

When I run eslint src in the terminal, I get a bunch of eslint errors, which is fine.当我在终端中运行eslint src时,我收到了一堆 eslint 错误,这很好。

Any idea what is not properly working?知道什么不能正常工作吗?

According to the docs you need to fail on error in the pipe.根据文档,您需要因管道中的错误而失败。

gulp.task('lint', function () {
    // ESLint ignores files with "node_modules" paths.
    // So, it's best to have gulp ignore the directory as well.
    // Also, Be sure to return the stream from the task;
    // Otherwise, the task may end before the stream has finished.
    return gulp.src(['**/*.js','!node_modules/**'])
        // eslint() attaches the lint output to the "eslint" property
        // of the file object so it can be used by other modules.
        .pipe(eslint())
        // eslint.format() outputs the lint results to the console.
        // Alternatively use eslint.formatEach() (see Docs).
        .pipe(eslint.format())
        // To have the process exit with an error code (1) on
        // lint error, return the stream and pipe to failAfterError last.
        .pipe(eslint.failAfterError());
});

Just a heads-up, the documentation is extremely useful and succinct on using configuration files, their precedence of usage and how they are located.提醒一下,该文档对使用配置文件、它们的使用优先级以及它们的位置非常有用和简洁。 You can also add the path to specify the location of your configuration file for a particular pipe:您还可以添加路径以指定特定管道的配置文件的位置:

gulp.task('lint', function () {
  gulp.src(['src/**/*.js', 'src/**/*.jsx'])
  .pipe(eslint({ configFile: '.eslintrc'}))
  .pipe(eslint.format())
  .pipe(eslint.failAfterError())
})

In the gulp-eslint documentation it should be noted that usage of the failOnError() and failAfterError() methods are advisable in that the task/stream is stopped and hence there is no invalid code written to the output.在 gulp-eslint文档中,应该注意的是,建议使用 failOnError() 和 failAfterError() 方法,因为任务/流已停止,因此没有将无效代码写入输出。

If you use neither then the error is still caught but displayed only in the console output.如果两者都不使用,则错误仍会被捕获,但仅显示在控制台输出中。 So dependent on your task flow and design the destination file may still be written but you can conveniently correct the error immediately and carry on without having to start up your pipe processing/watch task again.因此,取决于您的任务流程和设计,目标文件可能仍会被写入,但您可以方便地立即纠正错误并继续进行,而无需再次启动管道处理/观察任务。 An alternative is to look into gulp-plumber or some other means whereby you're not breaking out of a gulp watch task and yet also not writing a file containing code that doesn't pass linting validation.另一种方法是研究gulp-plumber或其他一些方法,这样您就不会中断 gulp watch 任务,也不会编写包含未通过 linting 验证的代码的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM