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 时,如何只针对一个子文件夹及其所有文件进行编译?

您需要做类似 eslint src/**/*.js[x] 的事情。如果您是 运行 进行 linting 检查的 webpack 构建(或预提交挂钩),则将其添加到 package.json.

内的 scripts 对象中

在你的.eslintrc.json

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

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

.eslintignore

 /someFolder
 /anotherFolder
 /oneMoreFolder

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

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

您也可以使用 .eslintignore 文件。

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

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

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