use crate::formatter::{format, FormatConfig, IndentStyle};
use wasm_bindgen::prelude::*;
#[wasm_bindgen(js_name = formatSource)]
pub fn format_source(
source: &str,
indent_width: Option<u32>,
use_tabs: Option<bool>,
preserve_comments: Option<bool>,
sort_imports: Option<bool>,
) -> Result<String, JsValue> {
let config = FormatConfig {
indent_width: indent_width.unwrap_or(4) as usize,
indent_style: if use_tabs.unwrap_or(false) {
IndentStyle::Tabs
} else {
IndentStyle::Spaces
},
preserve_comments: preserve_comments.unwrap_or(true),
sort_imports: sort_imports.unwrap_or(true),
..Default::default()
};
format(source, config).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen(js_name = checkFormat)]
pub fn check_format(
source: &str,
indent_width: Option<u32>,
use_tabs: Option<bool>,
) -> Result<bool, JsValue> {
let formatted = format_source(source, indent_width, use_tabs, Some(true), Some(true))?;
Ok(source == formatted)
}