mirror of
https://github.com/astral-sh/ruff.git
synced 2026-10-28 10:45:37 +08:00
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com> Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Ruff WASM
⚠️ WARNING: This API is experimental and may change at any time
An extremely fast Python linter and code formatter, written in Rust.
This is a WASM version of the Ruff API which can be used to lint/format Python in a browser environment.
There are multiple versions for the different wasm-pack targets. See here for more info on targets.
Usage
This example uses the wasm-pack web target and is known to work with Vite.
import init, { Workspace, type Diagnostic, PositionEncoding } from "@astral-sh/ruff-wasm-web";
const exampleDocument = `print('hello'); print("world")`;
await init(); // Initializes WASM module
// These settings illustrate configuring Ruff
// Settings info: https://docs.astral.sh/ruff/settings
const workspace = new Workspace(
{
"line-length": 88,
"indent-width": 4,
format: {
"indent-style": "space",
"quote-style": "double",
},
lint: {
select: ["E4", "E7", "E9", "F"],
},
},
PositionEncoding.UTF16,
);
// Will contain 1 diagnostic code for E702: Multiple statements on one line
const diagnostics: Diagnostic[] = workspace.check(exampleDocument);
const formatted = workspace.format(exampleDocument);
Versioning
This crate is an internal component of Ruff. The Rust API exposed here is unstable and will have frequent breaking changes.
This version (0.17.3) is a component of Ruff 0.17.3. The source can be found here.
See Ruff's crate versioning policy for details on versioning.