简体   繁体   中英

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.

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']
  [...]
}

Set in overrides inside .eslintrc.json

If you specified directories with CLI (eg, 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

In addition to @billythetalented 's comment I had to add a dot in the package.json:

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

Otherwise, It didn't lint anything

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