docs: 统一规范文档示例格式,使用三级标题 Example 和四级标题 Good/Bad 展示代码示例

This commit is contained in:
Lyda
2026-04-13 13:00:57 +08:00
parent dae54e90b6
commit f34b31f542
8 changed files with 141 additions and 71 deletions
+40 -20
View File
@@ -7,13 +7,15 @@
- 不检查 nodejs 的导包定义,比如 `const fs = require("fs")`
- 常量检查只需检查 `const` 声明的静态值,但是不包含对象和函数
### Good
### Example: 常量命名
#### Good: 使用大写加下划线
```javascript
const MAX_COUNT = 100;
```
### Bad
#### Bad: 使用小驼峰
```javascript
const maxCount = 100;
@@ -23,7 +25,9 @@ const maxCount = 100;
> - severity `warn`
### Good
### Example: 函数命名
#### Good: 使用小驼峰
```javascript
function getUserInfo() {
@@ -31,7 +35,7 @@ function getUserInfo() {
}
```
### Bad
#### Bad: 使用全小写
```javascript
function getuserinfo() {
@@ -57,7 +61,9 @@ function getuserinfo() {
- 无需考虑类型是否合理匹配
- 你需要自行的判断这个字面量的值开发是否可以理解,比如说参数的时间戳、毫秒时长、数量等这些如果一看就是可以理解也无需抽出常量
### Good
### Example: 魔法数字
#### Good: 使用常量替代魔法数字
```javascript
const ADMIN = 1;
@@ -67,7 +73,7 @@ if (user.role === ADMIN) {
}
```
### Bad
#### Bad: 使用字面量魔法数字
```javascript
if (user.role === 1) {
@@ -82,14 +88,16 @@ if (user.role === 1) {
- 需要放在指定模块的 constants 文件里
- constants 命名规则: user.constants.js 或者 constants.js 或者 user.constants.ts 或者 constants.ts
### Good
### Example: 常量定义位置
#### Good: 常量放在对应模块的 constants 文件
```javascript
// user.constants.js
const MAX_COUNT = 100; // 这是 user 模块需要的
```
### Bad
#### Bad: 常量放在错误模块的 constants 文件
```javascript
// class.constants.js
@@ -100,7 +108,9 @@ const MAX_COUNT = 100; // 这是 user 模块需要的
> - severity `warn`
### Good
### Example: 类和接口命名
#### Good: 使用大驼峰
```javascript
class UserInfo {
@@ -108,7 +118,7 @@ class UserInfo {
}
```
### Bad
#### Bad: 使用全小写
```javascript
class userinfo {
@@ -122,14 +132,16 @@ class userinfo {
- 需要注意的是从 require 导入的变量不受检查
### Good
### Example: 变量命名
#### Good: 使用小驼峰或蛇形命名
```javascript
let userName = "John";
let user_name = "John";
```
### Bad
#### Bad: 使用大驼峰
```javascript
let Username = "John";
@@ -137,13 +149,15 @@ let Username = "John";
## 单文件代码不超过 700 行 `[JsTs.Base.CodeNotMoreThan700Lines]`
### Good
### Example: 文件行数
#### Good: 代码不超过 700 行
```javascript
// 代码不超过 700 行
```
### Bad
#### Bad: 代码超过 700 行
```javascript
// 代码超过 700 行
@@ -151,7 +165,9 @@ let Username = "John";
## 单个函数或方法不能超出 200 行 `[JsTs.Base.FuncNotMoreThan200Lines]`
### Good
### Example: 函数行数
#### Good: 函数不超过 200 行
```javascript
function getUserInfo() {
@@ -159,7 +175,7 @@ function getUserInfo() {
}
```
### Bad
#### Bad: 函数超过 200 行
```javascript
function getUserInfo() {
@@ -173,7 +189,9 @@ function getUserInfo() {
- 逻辑判断的复杂度超过 2 个的要添加注释
### Good
### Example: 复杂逻辑注释
#### Good: 复杂逻辑添加注释
```javascript
// 逻辑判断复杂度超过 2 个
@@ -182,7 +200,7 @@ if (a && b || c) {
}
```
### Bad
#### Bad: 复杂逻辑未添加注释
```javascript
if (a && b && c && d) {
@@ -194,7 +212,9 @@ if (a && b && c && d) {
> - severity `warn`
### Good
### Example: 复杂函数注释
#### Good: 复杂函数添加注释
```javascript
/**
@@ -205,7 +225,7 @@ function complexFunc() {
}
```
### Bad
#### Bad: 复杂函数未添加注释
```javascript
function complexFunc() {