简体   繁体   English

ESLint 在配置文件中定义文件夹。 (忽略所有但...,仅包括...)

[英]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 .如何在.eslintrc.json文件中将文件夹设置为 lint,而不是在package.json中的eslint命令之后。

package.json (snippet) package.json (片段)

"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 .并在.eslintrc.json中定义路径和分机。

Alternativ, set .eslintignore to ignore ALL but not ./src .或者,将.eslintignore设置为忽略ALL但不忽略./src I only want to lint the src-folder.我只想检查 src 文件夹。 Not the root.不是根。 Also for the eslint plugin of vscode .也适用于vscodeeslint插件。

My current solution:我目前的解决方案:

.eslintignore .eslintignore

/*
!/src

But I wondering, why is there no option in the config files to set the folder/s to lint.但我想知道,为什么配置文件中没有选项可以将文件夹设置为 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).我已经使用.eslintignore文件调整了您的解决方案,并将规则直接放入我的配置文件的ignorePatterns选项(在我的例子中是.eslintrc.cjs )。 Works like a charm for me:对我来说就像一个魅力:

module.exports = {
  ignorePatterns: ['/*', '!/src']
  [...]
}

Set in overrides inside .eslintrc.json.eslintrc.json中设置覆盖

If you specified directories with CLI (eg, eslint lib), ESLint searches target files in the directory to lint.如果你用 CLI 指定了目录(例如,eslint lib),ESLint 会在目录中搜索目标文件以进行 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 *).目标文件是 *.js 或与任何覆盖条目匹配的文件(但排除任何以 * 结尾的文件的条目)。

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

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

Refer: document of Configuring ESLint参考: 配置 ESLint文档

In addition to @billythetalented 's comment I had to add a dot in the package.json:除了@billythetalented的评论之外,我还必须在 package.json 中添加一个点:

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

Otherwise, It didn't lint anything否则,它没有任何东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM