Skip to content

Commit

Permalink
feat: add web fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jan 31, 2024
1 parent 05626a2 commit db8d2f5
Show file tree
Hide file tree
Showing 10 changed files with 781 additions and 42 deletions.
188 changes: 188 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ resolver = "2"


[workspace.dependencies]
biome_formatter = "0.4.0"
biome_js_formatter = "0.4.0"
biome_js_parser = "0.4.0"
biome_js_syntax = "0.4.0"
serde = { version = "1.0" }
serde_json = { version = "1.0" }
biome_fmt = { path = "crates/biome_fmt", version = "*", default-features = false }

serde = "1.0"
serde-wasm-bindgen = "0.6"
serde_json = "1.0"
wasm-bindgen = "0.2.90"

[profile.release]
Expand Down
14 changes: 9 additions & 5 deletions crates/biome_fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ publish = true
repository.workspace = true
version.workspace = true

[features]
default = ["wasm-bindgen"]
wasm-bindgen = []

[dependencies]
biome_formatter = { workspace = true }
biome_js_formatter = { workspace = true }
biome_js_parser = { workspace = true }
biome_js_syntax = { workspace = true }
biome_formatter = { version = "0.4.0" }
biome_js_formatter = { version = "0.4.0" }
biome_js_parser = { version = "0.4.0" }
biome_js_syntax = { version = "0.4.0" }
serde = { workspace = true, features = ["derive"] }
serde-wasm-bindgen = "0.6"
serde-wasm-bindgen = { workspace = true }
serde_json = { workspace = true, features = ["preserve_order"] }
wasm-bindgen = { workspace = true }

Expand Down
42 changes: 36 additions & 6 deletions crates/biome_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use biome_js_formatter::context::JsFormatOptions;
use biome_js_syntax::JsFileSource;

use serde::Deserialize;

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::wasm_bindgen;

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen(typescript_custom_section)]
const TS_Config: &'static str = r#"
export interface Config {
Expand All @@ -22,9 +25,9 @@ export interface Config {
bracket_same_line?: boolean;
}"#;

#[derive(Deserialize, Default)]
#[derive(Deserialize, Default, Clone)]
#[serde(rename_all = "snake_case")]
pub(crate) struct Config {
pub struct BiomeConfig {
/// The indent style.
indent_style: Option<IndentStyle>,

Expand Down Expand Up @@ -65,17 +68,44 @@ pub(crate) struct Config {
source_type: Option<JsFileSource>,
}

impl Config {
impl BiomeConfig {
pub fn with_source_type(mut self, source_type: JsFileSource) -> Self {
self.source_type = Some(source_type);
self
}

pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self {
self.indent_style = Some(indent_style);
self
}

pub fn with_indent_width(mut self, indent_width: u8) -> Self {
self.indent_width = Some(indent_width);
self
}

pub fn with_line_width(mut self, line_width: u16) -> Self {
self.line_width = Some(line_width);
self
}

pub fn indent_style(&self) -> Option<IndentStyle> {
self.indent_style
}

pub fn indent_width(&self) -> Option<u8> {
self.indent_width
}

pub fn line_width(&self) -> Option<u16> {
self.line_width
}
}

impl TryFrom<Config> for JsFormatOptions {
impl TryFrom<BiomeConfig> for JsFormatOptions {
type Error = String;

fn try_from(value: Config) -> Result<Self, Self::Error> {
fn try_from(value: BiomeConfig) -> Result<Self, Self::Error> {
let source_type = value.source_type.expect("source_type is required");

let mut option = JsFormatOptions::new(source_type);
Expand Down Expand Up @@ -151,7 +181,7 @@ impl TryFrom<Config> for JsFormatOptions {

#[derive(Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
enum IndentStyle {
pub enum IndentStyle {
Tab,
Space,
}
Expand Down
Loading

0 comments on commit db8d2f5

Please sign in to comment.