简体   繁体   English

如何在 eslint 中忽略所有没有扩展名的文件

[英]How can I ignore all files without an extension in eslint

Currently in my.eslintignore I have the following:目前在 my.eslintignore 我有以下内容:

**/*.json
**/*.svg
**/*.bat
**/*.sh
**/*.html
**/*.png
**/*.txt
**/*.ico
**/*.md

How can I prevent eslint from linting files that have no extension?如何防止 eslint 对没有扩展名的文件进行 linting? For example, some of my docker files don't have an extension and I don't want to list them all out explicitly.例如,我的一些 docker 文件没有扩展名,我不想明确列出它们。

The best solution is to only tell ESLint to search for files matching JavaScript extensions you use (eg ts,tsx,js,jsx,cjs,mjs ) instead of telling it to find everything, and then ignoring specific extensions.最好的解决方案是只告诉 ESLint 搜索匹配 JavaScript 扩展名的文件(例如ts,tsx,js,jsx,cjs,mjs ),而不是告诉它查找所有内容,然后忽略特定的扩展名。

To tell ESLint to add extensions, you can use the --ext command-line option, or use the overrides configuration as shown below:要告诉 ESLint 添加扩展,您可以使用--ext命令行选项,或使用overrides配置,如下所示:

.eslintrc.js

module.exports = {
  // [Your config here...]
  // Empty overrides block that lists your globs for the 
  overrides: [
    {
      files: ["**/*.{ts,tsx,js,jsx}"]
    }
  ]
};

Then make sure you are telling ESLint the path you want to lint by using eslint./ or eslint./src in your lint script, without using glob patterns for extensions, as running eslint ** for instance will load every file extension.然后确保你通过在你的 lint 脚本中使用eslint./eslint./src告诉 ESLint 你想要 lint 的路径,而不使用 glob 模式作为扩展名,例如运行eslint **将加载每个文件扩展名。

package.json

{
  "name": "example",
  "version": "1.0.0",
  "scripts": {
    "lint": "eslint ./"
  }
}

After you've made these changes, you should no longer need an ESLint ignore file just to disable common file extensions.完成这些更改后,您应该不再需要 ESLint 忽略文件来禁用常见的文件扩展名。

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

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