简体   繁体   中英

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. I have been able to run the linter in a specific folder easily from the CLI but on compile, it targets ALL folders.

I want to run the linter on compile on just the files within the /src folder.

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?

inside your .eslintrc.json

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

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

in .eslintignore

 /someFolder
 /anotherFolder
 /oneMoreFolder

You need to do something like 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 .

I used ignorePatterns option. It'll tell ESLint to ignore specific files and directories. It's useful when your IDE (ex. VS Code) is reporting errors in unwanted files.

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

You can Also use the .eslintignore file.

Where you have written the script to run lint in package.json , there only mention the folders you want to target

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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