我正在使用eslint
来typescript
约定。我检查了这个规则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 包非常适合此。 它使您可以完全控制项目结构的外观。
以下是当前功能的列表:
✅ 验证项目结构。
✅ 验证文件夹和文件名。
✅ 名称大小写验证。
✅ 名称正则表达式验证。
✅ 文件扩展名验证。
✅ 继承父级的名称(子级继承其所在文件夹的名称(。
✅ 文件夹递归。
✅ 强制嵌套/平面结构。
例:
- 只有符合
snake_case
的文件才能位于src
文件夹中(添加允许的扩展名(。 - 只有符合
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