admin管理员组

文章数量:1201413

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.

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.

Share Improve this question edited May 11, 2020 at 1:09 Siluck asked May 11, 2020 at 0:41 SiluckSiluck 1511 gold badge1 silver badge4 bronze badges 1
  • 3 Your solution with eslintignore is the best I have found so far, thank you! – Renato Commented May 4, 2022 at 15:21
Add a comment  | 

3 Answers 3

Reset to default 12

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 (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

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

本文标签: javascriptESLint define folder in config file (Ignore all butInclude only)Stack Overflow