第 14 章:代码规范
第 14 章:代码规范
14.1 内置代码规范工具
Deno 内置了 lint 和 format 两个代码规范工具,无需安装 ESLint、Prettier 等第三方依赖。
| 工具 | 功能 | 命令 |
|---|---|---|
deno lint | 静态代码分析 | 检查代码质量问题 |
deno fmt | 代码格式化 | 统一代码风格 |
14.2 deno lint
基本用法
# 检查当前目录
deno lint
# 检查特定文件
deno lint src/main.ts
# 检查并修复可自动修复的问题
deno lint --fix
# 检查所有文件(包括第三方模块)
deno lint --unstable
输出示例
error[no-explicit-any]: 不推荐使用 any 类型
--> src/utils.ts:15:12
|
15 | function parse(data: any): object {
| ^^^
| 提示: 使用具体类型代替 any
error[no-unused-vars]: 变量 unusedVar 未被使用
--> src/main.ts:3:7
|
3 | const unusedVar = "hello";
| ^^^^^^^^^
Found 2 problems (2 errors, 0 warnings)
内置规则一览
| 规则 | 类型 | 说明 |
|---|---|---|
no-explicit-any | 警告 | 不推荐使用 any |
no-unused-vars | 错误 | 未使用的变量 |
no-unused-imports | 错误 | 未使用的导入 |
no-inferrable-types | 警告 | 不必要的类型注解 |
no-namespace | 错误 | 不推荐使用 namespace |
no-const-assign | 错误 | 不能重新赋值 const |
no-debugger | 错误 | 不能使用 debugger |
no-empty | 警告 | 空代码块 |
no-eval | 错误 | 不能使用 eval |
no-prototype-builtins | 警告 | 不推荐直接调用 Object.prototype 方法 |
no-regex-spaces | 错误 | 正则表达式中的多余空格 |
no-self-assign | 错误 | 自赋值 |
no-this-alias | 警告 | 不推荐 this 别名 |
prefer-const | 警告 | 推荐使用 const |
eqeqeq | 错误 | 必须使用 === 和 !== |
deno lint 配置
// deno.json
{
"lint": {
"rules": {
"tags": ["recommended"],
"include": ["eqeqeq", "no-eval", "prefer-const"],
"exclude": ["no-explicit-any"]
},
"files": {
"include": ["src/", "tests/"],
"exclude": ["src/vendor/"]
}
}
}
忽略特定规则
// 忽略单行
// deno-lint-ignore no-explicit-any
function parse(data: any): object {
return JSON.parse(data);
}
// 忽略多行
// deno-lint-ignore-file no-explicit-any
// 忽略整个文件
// deno-lint-ignore-file
export const legacy = true;
14.3 deno fmt
基本用法
# 格式化所有文件
deno fmt
# 格式化特定文件
deno fmt src/main.ts
# 检查但不修改(CI 模式)
deno fmt --check
# 显示差异
deno fmt --check --diff
格式化规则
Deno fmt 使用类似 Prettier 的规则:
| 规则 | 默认值 |
|---|---|
| 行宽 | 80 字符 |
| 缩进 | 2 空格 |
| 引号 | 双引号 |
| 分号 | 有分号 |
| 尾逗号 | 有 |
deno fmt 配置
// deno.json
{
"fmt": {
"options": {
"useTabs": false,
"indentWidth": 2,
"lineWidth": 100,
"singleQuote": false,
"proseWrap": "preserve",
"semiColon": true
},
"files": {
"include": ["src/", "tests/"],
"exclude": ["src/generated/", "node_modules/"]
}
}
}
忽略格式化
// 忽略单行
// deno-fmt-ignore
const x = 1;
// 忽略代码块
// deno-fmt-ignore-start
const matrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
];
// deno-fmt-ignore-end
// 忽略整个文件
// deno-fmt-ignore-file
14.4 Git hooks 集成
使用 Deno 任务配置 pre-commit
// deno.json
{
"tasks": {
"lint": "deno lint",
"fmt": "deno fmt",
"fmt:check": "deno fmt --check",
"check": "deno lint && deno fmt --check",
"pre-commit": "deno lint && deno fmt --check"
}
}
创建 Git hook
# .git/hooks/pre-commit
#!/bin/sh
deno lint && deno fmt --check
chmod +x .git/hooks/pre-commit
14.5 CI/CD 集成
GitHub Actions
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v2.x
- name: Lint
run: deno lint
- name: Format Check
run: deno fmt --check
- name: Type Check
run: deno check main.ts
- name: Test
run: deno test
14.6 ESLint 迁移
如果你有 ESLint 经验,以下是规则对照表:
| ESLint 规则 | Deno lint 规则 |
|---|---|
no-eval | no-eval |
no-debugger | no-debugger |
no-unused-vars | no-unused-vars |
no-const-assign | no-const-assign |
eqeqeq | eqeqeq |
prefer-const | prefer-const |
no-var | no-var |
no-empty | no-empty |
如需使用 ESLint,Deno 也支持:
# 使用 ESLint
deno run -A npm:eslint src/
14.7 代码规范最佳实践
项目配置模板
// deno.json — 推荐配置
{
"lint": {
"rules": {
"tags": ["recommended"]
}
},
"fmt": {
"options": {
"lineWidth": 100,
"indentWidth": 2
}
},
"tasks": {
"check": "deno lint && deno fmt --check && deno test",
"fix": "deno lint --fix && deno fmt"
}
}
团队规范
# 每次提交前运行
deno task check
# 自动修复
deno task fix
14.8 本章小结
| 要点 | 说明 |
|---|---|
deno lint | 内置 linter,检查代码质量 |
deno fmt | 内置格式化工具,统一代码风格 |
| 配置 | deno.json 中配置规则 |
| 忽略 | // deno-lint-ignore 和 // deno-fmt-ignore |
| CI 集成 | deno fmt --check 用于 CI |
| Git hooks | 提交前自动检查 |
📖 扩展阅读
下一章:第 15 章:部署 → 学习 Deno 应用的部署方案。