|
| 1 | +# contributing |
| 2 | + |
| 3 | +## prerequisites |
| 4 | + |
| 5 | +- **Rust** (stable toolchain) |
| 6 | +- **Python 3.10+** (for running transpiled output) |
| 7 | +- **uv** (recommended for Python dependency management) |
| 8 | + |
| 9 | +## building |
| 10 | + |
| 11 | +basedpython is a Rust project managed with Cargo: |
| 12 | + |
| 13 | +```sh |
| 14 | +cargo build |
| 15 | +``` |
| 16 | + |
| 17 | +the binary is built to `target/debug/by`. for a release build: |
| 18 | + |
| 19 | +```sh |
| 20 | +cargo build --release |
| 21 | +``` |
| 22 | + |
| 23 | +## running tests |
| 24 | + |
| 25 | +```sh |
| 26 | +cargo test |
| 27 | +``` |
| 28 | + |
| 29 | +- **inline tests** — most transform files in `src/transforms/` have `#[cfg(test)]` modules at the bottom with unit tests that call `transpile()` on a snippet and assert the output |
| 30 | +- **end-to-end tests** — `tests/e2e.rs` runs larger integration scenarios |
| 31 | + |
| 32 | +## project structure |
| 33 | + |
| 34 | +``` |
| 35 | +src/ |
| 36 | +├── main.rs # CLI entry point (clap argument parsing) |
| 37 | +├── lib.rs # transpile() — orchestrates the full pipeline |
| 38 | +├── args.rs # CLI argument definitions |
| 39 | +├── config.rs # Config struct and PythonVersion enum |
| 40 | +├── source_map.rs # source map generation |
| 41 | +├── symbol_table.rs # module-level symbol collection |
| 42 | +├── transforms/ # forward transforms (basedpython → Python) |
| 43 | +│ ├── mod.rs |
| 44 | +│ ├── subscript.rs |
| 45 | +│ ├── mutable_defaults.rs |
| 46 | +│ ├── generics.rs |
| 47 | +│ ├── modifiers.rs |
| 48 | +│ ├── coalesce.rs |
| 49 | +│ ├── none_chain.rs |
| 50 | +│ └── ... |
| 51 | +└── reverse_transforms/ # reverse transforms (Python → basedpython) |
| 52 | + ├── mod.rs |
| 53 | + ├── empty_class.rs |
| 54 | + ├── literal_types.rs |
| 55 | + └── subscript.rs |
| 56 | +crates/ # forked ruff/ty crates |
| 57 | +docs/ # documentation pages |
| 58 | +tests/ # end-to-end tests |
| 59 | +``` |
| 60 | + |
| 61 | +## adding a new transform |
| 62 | + |
| 63 | +1. **create the transform file** in `src/transforms/`, e.g. `my_feature.rs` |
| 64 | + |
| 65 | +2. **define a struct** that holds the source text and a `Vec<(TextRange, String)>` for edits: |
| 66 | + |
| 67 | + ```rust |
| 68 | + pub struct MyFeature<'src> { |
| 69 | + source: &'src str, |
| 70 | + pub edits: Vec<(ruff_text_size::TextRange, String)>, |
| 71 | + } |
| 72 | + ``` |
| 73 | + |
| 74 | +3. **implement `Visitor`** from `ruff_python_ast::visitor` — walk the AST nodes you care about and push edits: |
| 75 | + |
| 76 | + ```rust |
| 77 | + impl Visitor<'_> for MyFeature<'_> { |
| 78 | + fn visit_stmt(&mut self, stmt: &Stmt) { |
| 79 | + // detect your pattern, push to self.edits |
| 80 | + walk_stmt(self, stmt); |
| 81 | + } |
| 82 | + } |
| 83 | + ``` |
| 84 | + |
| 85 | +4. **register the transform** in `src/transforms/mod.rs` and wire it into the pipeline in `src/lib.rs`: |
| 86 | + - instantiate it alongside the other transforms |
| 87 | + - call `visitor.visit_stmt(stmt)` in the main loop |
| 88 | + - extend the edits vec with your transform's edits |
| 89 | + - if your transform needs generated imports (e.g. `from typing import TypeVar`), integrate with the **preamble system** — after all edits are applied, `lib.rs` collects import requests from transforms and prepends them to the output file. see how `generics.needed_imports` or `literal_types.needs_literal_import` for examples |
| 90 | + |
| 91 | +5. **add tests** in a `#[cfg(test)]` module at the bottom of your file: |
| 92 | + |
| 93 | + ```rust |
| 94 | + #[cfg(test)] |
| 95 | + mod tests { |
| 96 | + use crate::{Config, transpile}; |
| 97 | + |
| 98 | + fn t(input: &str) -> String { |
| 99 | + transpile(input, &Config::default()).unwrap() |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn basic() { |
| 104 | + assert_eq!(t("input code"), "expected output"); |
| 105 | + } |
| 106 | + } |
| 107 | + ``` |
| 108 | + |
| 109 | +6. **add a doc page** in `docs/` describing the syntax and showing examples |
| 110 | + |
| 111 | +## running the docs locally |
| 112 | + |
| 113 | +the documentation site is built with [zensical](https://github.com/kotlinisland/zensical). to view it locally, use: |
| 114 | + |
| 115 | +```sh |
| 116 | +uv run zensical serve |
| 117 | +``` |
| 118 | + |
| 119 | +this starts a local dev server (usually at `http://localhost:8000`) with live reload. pages are in `docs/` and the site configuration is in `zensical.toml` |
| 120 | + |
| 121 | +## adding a reverse transform |
| 122 | + |
| 123 | +reverse transforms live in `src/reverse_transforms/` and follow the same pattern. they detect standard Python patterns that correspond to basedpython idioms and rewrite them back. register new reverse transforms in `src/reverse_transforms/mod.rs` |
0 commit comments