强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Deno 入门教程 / 第 14 章:代码规范

第 14 章:代码规范

14.1 内置代码规范工具

Deno 内置了 lintformat 两个代码规范工具,无需安装 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-evalno-eval
no-debuggerno-debugger
no-unused-varsno-unused-vars
no-const-assignno-const-assign
eqeqeqeqeqeq
prefer-constprefer-const
no-varno-var
no-emptyno-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 应用的部署方案。