如何在打字稿 eslint 中强制执行文件名和文件夹名称约定?



我正在使用eslinttypescript约定。我检查了这个规则https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md它只适用于代码。

我正在寻找一个可以强制camelCase文件名和snake_case文件夹名称的规则。如何在打字稿中设置此规则?

Eslint 默认不实现文件名检查,请参阅 2015 年的 GitHub 问题。

一些插件确实喜欢eslint-plugin-unicorn或eslint-plugin-filenames。


因为 Tslint 现在已经不推荐使用,所以他们创建了处理文件命名大小写的项目打字稿-eslint,你可以:)查看它

eslint 插件 eslint-plugin-check-file 允许您为文件名和文件夹强制实施一致的命名模式。

camelCase样式应用于文件名:

{
"rules":{
"check-file/filename-naming-convention":[
"error",
{
"*.{js,ts}":"CAMEL_CASE"
}
]
}
}

snake_case样式应用于文件夹名称:

{
"rules":{
"check-file/folder-naming-convention":[
"error",
{
"src/**/":"SNAKE_CASE"
}
]
}
}

查看此链接以获取有关eslint-plugin-check-file的更多信息。

事后添加更多资源 - 我发现对于文件名,eslint-plugin-filename-rules效果很好。

为了在文件夹上强制执行 eslint,eslint-plugin-folder 为我提供了包含文件的目录。文档说只有.js.jsx文件,但.ts.tsx文件也对我有用。

我的 eslint-plugin-project-structure 包非常适合此。 它使您可以完全控制项目结构的外观。

以下是当前功能的列表:

✅ 验证项目结构。
✅ 验证文件夹和文件名。
✅ 名称大小写验证。
✅ 名称正则表达式验证。
✅ 文件扩展名验证。
✅ 继承父级的名称(子级继承其所在文件夹的名称(。
✅ 文件夹递归。
✅ 强制嵌套/平面结构。

例:

  1. 只有符合snake_case的文件才能位于src文件夹中(添加允许的扩展名(。
  2. 只有符合camelCase的文件夹才能位于src文件夹中(您可以为文件夹子级添加规则(。

您还可以使用文件夹递归为每个嵌套文件夹添加上述规则

{
"structure": {
"name": "src",
"children": [
{
"name": "/^${{snake_case}}$/",
"children": [
// ... rules for folder children. 
// If you only care about the folder name, leave it as [].
]
},
{
"name": "/^${{camelCase}}$/",
"extension": "*"
},
]
},
}

在这里,您将找到 API 的详细说明。 https://github.com/Igorkowalski94/eslint-plugin-project-structure#api

最新更新