36

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?

Vahid
  • 6,639
  • 5
  • 37
  • 61
Dustwise
  • 557
  • 1
  • 6
  • 10

4 Answers4

31

inside your .eslintrc.json

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

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

in .eslintignore

 /someFolder
 /anotherFolder
 /oneMoreFolder
Jalal
  • 3,308
  • 4
  • 35
  • 43
  • 1
    As others have mentioned, you can use `"ignorePatterns": ["/someFolder", ...]` inside of your eslintrc file. This allows you to keep it all in one place. – Ryan Wheale May 06 '22 at 17:49
20

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.

Vahid
  • 6,639
  • 5
  • 37
  • 61
13

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.

Hozefa
  • 1,676
  • 6
  • 22
  • 34
  • 1
    When I ran this command, I got an error that no paths matched that format. I had luck with this instead: `eslint src/**/*.{js,jsx}` – Chris Sandvik Dec 19 '19 at 15:47
  • 3
    How can I limit this via the .eslintrc.json file? – Vahid Jan 22 '20 at 13:15
  • 2
    @Vahid I recommend using `.eslintignore` instead, if you want to ignore certain files. Another way is while you run the `eslint` command make the path to files more tighter. `eslintrc` file should be used for setting up the config. https://github.com/eslint/eslint/blob/master/.eslintignore – Hozefa Jan 23 '20 at 05:40
5

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.

Karan Attri
  • 171
  • 2
  • 3