blog.dopana

Back

在现代JavaScript/TypeScript开发世界中,BiomeOxc是两个突出的Rust工具,为代码检查和格式化提供高性能。两者都用Rust编写,但具有不同的设计理念和架构。本文将深入探讨每个工具的架构并进行比较。

什么是Biome?#

Biome是一个用于Web项目的综合工具链,提供可通过CLI和LSP使用的格式化器和代码检查器。它旨在以更高的性能在一个集成包中替代Prettier和ESLint。

Biome架构#

Biome使用服务器-客户端架构,并在后台运行守护进程:

graph TB
    subgraph "Biome Architecture"
        CLI[CLI Interface]
        LSP[LSP Server]
        Daemon[Daemon Process]
        Workspace[WorkspaceServer]
        Handlers[Language Handlers]
        Foundation[Foundation Layer]
    end
    
    CLI --> Daemon
    LSP --> Daemon
    Daemon --> Workspace
    Workspace --> Handlers
    Handlers --> Foundation

架构层次#

  1. Foundation Layer(biome_rowanbiome_parserbiome_formatterbiome_analyze

    • 提供可重用的原语
    • 基于Rowan的解析基础设施
    • 通用格式化IR
  2. Language Layer(按语言的crates)

    • biome_js_parserbiome_js_formatterbiome_js_analyze
    • biome_json_parserbiome_json_formatterbiome_json_analyze
    • CSS、HTML、GraphQL类似
  3. Service Layer(biome_service

    • 通过WorkspaceServer协调操作
    • 管理状态和缓存
  4. Interface Layer(biome_clibiome_lspbiome_wasm

    • 提供多种使用方式

Biome的格式化器#

Biome使用两阶段格式化系统:

graph LR
    CST[CST from Parser] --> Lowering[Lowering Stage]
    Lowering --> IR[FormatElement IR]
    IR --> Printer[Printing Stage]
    Printer --> Output[Formatted Text]
  • Lowering Stage:将CST转换为语言无关的IR(FormatElement
  • Printing Stage:打印机消耗IR并根据选项决定布局和换行

什么是Oxc?#

Oxc(The Oxidation Compiler)是用Rust编写的高性能JavaScript/TypeScript工具集合。它被设计为一组模块化、可组合的编译器组件,可以独立或一起使用来构建完整的工具链。

Oxc架构#

Oxc使用分层设计,具有模块化组件:

graph TB
    subgraph "Oxc Architecture"
        Apps[Applications]
        Core[Core Libraries]
        Foundation[Foundation Libraries]
    end
    
    Apps --> Core
    Core --> Foundation
    
    Apps --> oxlint[oxlint]
    Apps --> LSP[Language Server]
    Apps --> NAPI[NAPI Bindings]
    
    Core --> Parser[Parser]
    Core --> Semantic[Semantic]
    Core --> Linter[Linter]
    Core --> Transformer[Transformer]
    Core --> Minifier[Minifier]
    Core --> Codegen[Codegen]
    
    Foundation --> AST[AST]
    Foundation --> Allocator[Allocator]
    Foundation --> Diagnostics[Diagnostics]
    Foundation --> Span[Span]
    Foundation --> Syntax[Syntax]

架构原则#

  1. Zero-Copy:使用arena分配器(oxc_allocator)进行零拷贝操作
  2. Visitor Pattern:具有自动访问者生成的AST遍历
  3. Shared Foundation:共享错误报告、源位置、语法定义

Oxc的代码检查器#

Oxlint使用并行执行架构和双分支策略:

graph TB
    LintService[LintService] --> Runtime[Runtime]
    Runtime --> process_source[process_source]
    process_source --> Parser[Parser]
    Parser --> AST[AST]
    AST --> LintContext[LintContext]
    LintContext --> Linter[Linter]
    Linter --> execute_rules[execute_rules]
    
    execute_rules --> strategy1[Strategy 1: nodes → rules]
    execute_rules --> strategy2[Strategy 2: rules → nodes]
    
    strategy1 --> RULE_BUCKETS[RULE_BUCKETS]
    strategy2 --> RULE_BUCKETS
  • Dual Branch Strategy:根据文件大小在(节点→规则)或(规则→节点)之间切换
  • Parallel Execution:使用rayon进行并行文件处理
  • Type-Aware Linting:支持类型感知的代码检查

Oxc的格式化器(oxfmt)#

Oxc格式化器使用三阶段格式化系统:

graph LR
    AST[AST from Parser] --> IRGen[IR Generation]
    IRGen --> IR[FormatElement IR]
    IR --> Transform[IR Transformation]
    Transform --> sort_imports[sort_imports]
    sort_imports --> Printer[Printing]
    Printer --> Output[Formatted Text]
  • IR Generation:使用Format特征进行AST遍历
  • IR Transformation:应用转换如sort_imports
  • Printing:打印机消耗IR并渲染最终输出

[!NOTE] Oxc格式化器核心是从Biome的biome_formatter crate移植的,但具有完全语言无关的设计。

详细比较#

1. 设计理念#

特性BiomeOxc
目标替代Prettier + ESLint的一体化工具链可独立使用的模块化组件
多语言支持JS/TS、JSON、CSS、HTML、GraphQL专注于JS/TS,有扩展计划
部署用于IDE集成的守护进程带有NAPI绑定的传统CLI

2. 内存管理#

特性BiomeOxc
分配器基于Rowan的基础设施Arena分配器(oxc_allocator
Zero-Copy是,但不那么明确是,作为核心设计原则
AST存储Rowan语法树基于Arena的AST节点

3. 性能优化#

特性BiomeOxc
并行执行是,通过WorkspaceServer是,使用rayon
缓存守护进程缓存LintService中的运行时缓存
规则执行顺序执行基于文件大小的双分支策略

4. 扩展性#

特性BiomeOxc
插件系统支持插件用于ESLint兼容性的JS插件(alpha)
语言支持ExtensionHandler模式按语言的模块crates
自定义规则通过biome配置通过规则配置

5. 格式化架构#

特性BiomeOxc
阶段2阶段(Lowering + Printing)3阶段(IR Gen + Transform + Printing)
IR设计biome_formatter中的FormatElementoxc_formatter_core中的FormatElement
注释处理集成在格式化器中基于光标的注释系统
Prettier兼容性是,有测试是,作为设计目标

何时使用哪个?#

选择Biome当:#

  • 您想要一体化解决方案替代Prettier和ESLint
  • 需要多语言支持(CSS、HTML、GraphQL)
  • 想要用于更好IDE集成的守护进程
  • 团队想要具有简单配置的统一工具链

选择Oxc当:#

  • 您想要可以独立使用的模块化组件
  • 需要CI/CD环境的最大性能
  • 想要具有TypeScript语义的类型感知代码检查
  • 从组件构建自定义工具链

使用示例#

Biome#

# 安装
npm install -D @biomejs/biome

# 格式化和检查文件
biome check src/file.ts

# 格式化和检查整个项目
biome check .

# 开发用的监视模式
biome check --watch .
bash

Oxc#

# 安装oxlint
npm install -D oxlint

# 检查文件
oxlint src/file.ts

# 并行执行检查整个项目
oxlint src/

# 类型感知检查
oxlint --tsconfig tsconfig.json src/
bash

结论#

Biome和Oxc都代表了具有高Rust性能的JavaScript/TypeScript工具的未来。Biome在具有多语言支持的一体化体验方面表现出色,而Oxc在模块化设计和最大性能方面表现出色。

两个工具之间的选择取决于您项目的具体需求:统一工具链与灵活组件、多语言支持与JS/TS重点、IDE集成与CI性能。

参考资料#