Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 427
Siluck Avatar asked Jan 26 '26 16:01

Siluck


2 Answers

I've adapted your solution with the .eslintignore file and put the rules directly into my config file's ignorePatterns option (.eslintrc.cjs in my case). Works like a charm for me:

module.exports = {
  ignorePatterns: ['/*', '!/src']
  [...]
}
like image 200
billythetalented Avatar answered Jan 29 '26 04:01

billythetalented


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

like image 39
keikai Avatar answered Jan 29 '26 06:01

keikai