Add Decodal formatter
This commit is contained in:
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use decodal::{
|
||||
Data, Diagnostic, DiagnosticKind, EmptyLoader, Engine, LoadedSource, SourceId, SourceLoader,
|
||||
Span, format_diagnostic_with,
|
||||
Span, format_diagnostic_with, format_source,
|
||||
};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
@@ -11,6 +11,17 @@ pub fn evaluate(source: &str) -> String {
|
||||
encode_result(evaluate_inner(source))
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = formatSource)]
|
||||
pub fn format_source_json(source: &str) -> String {
|
||||
match format_source(source) {
|
||||
Ok(formatted) => format!("{{\"ok\":true,\"source\":{}}}", json_string(&formatted)),
|
||||
Err(error) => format!(
|
||||
"{{\"ok\":false,\"error\":{}}}",
|
||||
json_string(&format_diagnostic_with_root(&error, "playground"))
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = evaluateProject)]
|
||||
pub fn evaluate_project(entry: &str, files_json: &str) -> String {
|
||||
encode_result(evaluate_project_inner(entry, files_json))
|
||||
@@ -214,6 +225,13 @@ fn json_string(value: &str) -> String {
|
||||
mod tests {
|
||||
use super::{evaluate_project_inner, normalize_path, resolve_import};
|
||||
|
||||
#[test]
|
||||
fn formats_source_for_web() {
|
||||
let output = super::format_source_json("value={a=1;};");
|
||||
assert!(output.contains("\"ok\":true"));
|
||||
assert!(output.contains("value = {\\n a = 1;\\n};\\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_virtual_paths() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user