ESLint define folder in config file. (Ignore all but..., Include only...)

Viewed 4204

How to set the folder to lint in the .eslintrc.json file, instead after the eslint command in package.json.

package.json (snippet)

"scripts: {
  "lint": "eslint ./src --ext .js,.jsx,.ts,.tsx",
}

I want only:

"scripts: {
  "lint": "eslint",
}

and define the path and ext in the .eslintrc.json.

Alternativ, set .eslintignore to ignore ALL but not ./src. I only want to lint the src-folder. Not the root. Also for the eslint plugin of vscode.

My current solution:

.eslintignore

/*
!/src

But I wondering, why is there no option in the config files to set the folder/s to lint. I'm looking for the most common and elegant solution. Maybe it sounds like a duplicate here. But I searched a lot of topics and found nothing similar to solve my problem.

1 Answers

Set in overrides inside .eslintrc.json

If you specified directories with CLI (e.g., eslint lib), ESLint searches target files in the directory to lint. The target files are *.js or the files that match any of overrides entries (but exclude entries that are any of files end with *).

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

  "overrides": [
    {
      "files": ["bin/*.js", "lib/*.js"],
      "excludedFiles": "*.test.js",
      "rules": {
        "quotes": ["error", "single"]
      }
    }
  ]
}

Refer: document of Configuring ESLint

Related