简体   繁体   English

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

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

I have a folder structure similar to this:我有一个类似于这样的文件夹结构:

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

I'm working with create-react-app and am applying airbnb rules.我正在使用 create-react-app 并正在应用 airbnb 规则。 I have been able to run the linter in a specific folder easily from the CLI but on compile, it targets ALL folders.我已经能够从 CLI 轻松地在特定文件夹中运行 linter,但在编译时,它针对所有文件夹。

I want to run the linter on compile on just the files within the /src folder.我只想在/src文件夹中的文件上运行 linter 编译。

How can I go about this?我该怎么办? I've tried a number of solutions.我尝试了多种解决方案。

Thank you!谢谢!

TL:DR How do I target just one subfolder and all of its files on compile when using eslint and create-react-app? TL:DR 使用 eslint 和 create-react-app 时,如何仅针对一个子文件夹及其所有文件进行编译?

inside your .eslintrc.json在你的.eslintrc.json里面

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

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

in .eslintignore.eslintignore

 /someFolder
 /anotherFolder
 /oneMoreFolder

You need to do something like eslint src/**/*.js[x] .你需要做一些类似eslint src/**/*.js[x]事情。 If you are running a webpack build(or precommit hook) that does linting check then add it within the scripts object inside package.json .如果您正在运行执行 linting 检查的 webpack 构建(或预提交钩子),则将其添加到package.jsonscripts对象中。

I used ignorePatterns option.我使用了ignorePatterns选项。 It'll tell ESLint to ignore specific files and directories.它会告诉 ESLint 忽略特定的文件和目录。 It's useful when your IDE (ex. VS Code) is reporting errors in unwanted files.当您的 IDE(例如 VS Code)报告不需要的文件中的错误时,它很有用。

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

You can Also use the .eslintignore file.您也可以使用.eslintignore文件。

Where you have written the script to run lint in package.json , there only mention the folders you want to target在您编写脚本以在package.json运行 lint 的地方,只提到了您想要定位的文件夹

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

Then if you want to ignore some type of files in the respective folders, you can mention in ESLint config file.那么如果你想忽略相应文件夹中的某些类型的文件,你可以在 ESLint 配置文件中提及。

暂无
暂无

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

相关问题 create-react-app eslint 错误“解析错误:‘import’和‘export’可能只出现在‘sourceType:module’中” - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'" 安装 npm create-react-app 后 ESLint 出现问题 - Problem with ESLint after installing npm create-react-app 如何扩展 Eslint 以使用 create-react-app - How to Extend Eslint to work with create-react-app 有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint - Is there a way to change the eslint of the webpack for the create-react-app without ejecting 有没有办法更改弹出的 create-react-app 的 eslint 规则? - Is there a way to change eslint rules for ejected create-react-app? 如何使用特定版本的create-react-app创建react项目? - How to create a react project with specific version of create-react-app? ESLint 错误/警告应阻止在 React (create-react-app) 中编译 - ESLint errors/warnings should prevent compiling in React (create-react-app) src 目录之外的 create-react-app 导入限制 - The create-react-app imports restriction outside of src directory Eslint precommit husky hook在create-react-app项目中未按预期工作 - Eslint precommit husky hook is not working as expected in create-react-app project 使用 Create-React-App 创建 React 应用程序失败并出现 ERR:在“…er”附近解析时,JSON 输入意外结束。”^2.0,0“,”eslint”' - Create React app with Create-React-App fails with ERR! Unexpected end of JSON input while parsing near '…er“:”^2.0.0“,”eslint"'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM