Code Formatting
Deno uses automated tools to ensure consistent code formatting across the entire codebase.Running Formatters
Before committing any code, you must run the formatter:Linting
All code must pass linting checks before being committed.JavaScript/TypeScript Linting
If you only changed non-Rust code:Rust Linting
If you changed Rust code:Rust Code Style
Formatting Configuration
Rust code follows the configuration in.rustfmt.toml:
- Max width: 80 characters
- Tab spaces: 2
- Edition: 2024
- Import grouping:
StdExternalCrate - Import granularity:
item
Common Patterns
Ops (Operations)
Rust functions exposed to JavaScript are called “ops” and are located inext/ directories:
Error Handling
Useeprintln! for debug output and avoid unwrap() in production code:
Resource Management
Resources are managed objects passed between Rust and JavaScript:TypeScript/JavaScript Code Style
Configuration
TypeScript and JavaScript code follows dprint configuration with Deno defaults:- Deno mode: enabled
- Consistent formatting across the codebase
Runtime Code Conventions
For JavaScript code in the runtime:File Formatting
General Rules
As specified in.editorconfig:
- End of line: LF (Unix-style)
- Insert final newline: true
- Indent style: spaces
- Indent size: 2 spaces
- Charset: UTF-8
- Trim trailing whitespace: true
Special Cases
.outfiles (test expectations) are editor-neutral and may not follow standard formatting- Node compatibility tests may have different formatting requirements
Directory Structure Conventions
Key Directories
cli/
cli/
User-facing CLI implementation, subcommands, and tools.
cli/args/flags.rs- CLI flag parsing and structurecli/tools/<tool>/- Individual tool implementationscli/main.rs- Entry point and command routing
runtime/
runtime/
JavaScript runtime assembly and integration.
runtime/worker.rs- Worker/runtime initializationruntime/permissions.rs- Permission system
ext/
ext/
Extensions providing native functionality to JavaScript.Each extension contains:
- Rust ops (operations)
- JavaScript APIs
- Extension-specific tests
tests/
tests/
Test suite organization:
tests/specs/- Integration spec teststests/unit/- Unit teststests/testdata/- Test fixtures and data filestests/wpt/- Web Platform Tests
Naming Conventions
Files and Directories
- Use snake_case for Rust files:
module_loader.rs - Use kebab-case for directories when descriptive:
worker-threads - Use lowercase for spec test directories:
tests/specs/my_feature/
Code Identifiers
Rust:- Functions and variables:
snake_case - Types and traits:
PascalCase - Constants:
SCREAMING_SNAKE_CASE
- Functions and variables:
camelCase - Classes and interfaces:
PascalCase - Constants:
SCREAMING_SNAKE_CASEorcamelCase
Comment Style
Rust Comments
TypeScript Comments
Best Practices
Keep Changes Minimal
Don’t do drive-by changes in a PR. If you need to make a change that is not directly related to the PR, create a separate PR for it.
Clear Code Organization
Follow the established directory structure. Place new features in appropriate locations based on existing patterns.
Use Examples
When adding new functionality, look at similar existing code:
- CLI flags:
cli/args/flags.rs - Ops: relevant
ext/directories - Tools:
cli/tools/
Document Patterns
Follow established patterns for:
- Ops in extensions
- Workers and execution contexts
- Resource management
- Permission checks
Pre-Commit Checklist
Additional Resources
- EditorConfig:
.editorconfigdefines cross-editor formatting rules - Dprint Config:
.dprint.jsonconfigures JavaScript/TypeScript/Markdown formatting - Rustfmt Config:
.rustfmt.tomlconfigures Rust formatting - Dlint Config:
.dlint.jsonconfigures JavaScript/TypeScript linting