From 2b7e28b591a6947f76ddf9a53f49eec8f1accc0f Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 25 Feb 2020 06:48:14 +1100 Subject: feat: Add Deno.formatDiagnostics (#4032) --- cli/ops/errors.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'cli/ops/errors.rs') diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index 4dd4f1ca1..b5cc75f7a 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -1,5 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::diagnostics::Diagnostic; use crate::fmt_errors::JSError; use crate::op_error::OpError; use crate::ops::json_op; @@ -18,6 +19,10 @@ pub fn init(i: &mut Isolate, s: &State) { "format_error", s.core_op(json_op(s.stateful_op(op_format_error))), ); + i.register_op( + "format_diagnostic", + s.core_op(json_op(s.stateful_op(op_format_diagnostic))), + ); } #[derive(Deserialize)] @@ -68,3 +73,15 @@ fn op_apply_source_map( "column": orig_column as u32, }))) } + +fn op_format_diagnostic( + _state: &State, + args: Value, + _zero_copy: Option, +) -> Result { + if let Some(diagnostic) = Diagnostic::from_json_value(&args) { + Ok(JsonOp::Sync(json!(diagnostic.to_string()))) + } else { + Err(OpError::type_error("bad diagnostic".to_string())) + } +} -- cgit v1.2.3