繁体   English   中英

ESLint 仅针对特定目录(eslintrc、create-react-app)

[英]ESLint only target a specific directory (eslintrc, create-react-app)

我有一个类似于这样的文件夹结构:

/root
 .eslintrc.json
 package.json
 /someFolder
   /sub
   /sub
 /anotherFolder
 /src
   /containers
   /components
 /oneMoreFolder
   /sub
   /sub

我正在使用 create-react-app 并正在应用 airbnb 规则。 我已经能够从 CLI 轻松地在特定文件夹中运行 linter,但在编译时,它针对所有文件夹。

我只想在/src文件夹中的文件上运行 linter 编译。

我该怎么办? 我尝试了多种解决方案。

谢谢!

TL:DR 使用 eslint 和 create-react-app 时,如何仅针对一个子文件夹及其所有文件进行编译?

在你的.eslintrc.json里面

{
  "rules": {
    "quotes": [ 2, "double" ]
  },

  "overrides": [
    {
      "files": [ "src/**/*.js" ],
      "rules": {
        "quotes": [ 2, "single" ]
      }
    }
  ]
}

.eslintignore

 /someFolder
 /anotherFolder
 /oneMoreFolder

你需要做一些类似eslint src/**/*.js[x]事情。 如果您正在运行执行 linting 检查的 webpack 构建(或预提交钩子),则将其添加到package.jsonscripts对象中。

我使用了ignorePatterns选项。 它会告诉 ESLint 忽略特定的文件和目录。 当您的 IDE(例如 VS Code)报告不需要的文件中的错误时,它很有用。

{
  "ignorePatterns": ["gulpfile.js", "gulp/**/*", "webpack.config.js"]
}

您也可以使用.eslintignore文件。

在您编写脚本以在package.json运行 lint 的地方,只提到了您想要定位的文件夹

scripts:{"lint": "eslint src public"}

那么如果你想忽略相应文件夹中的某些类型的文件,你可以在 ESLint 配置文件中提及。

暂无
暂无

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

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