2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-08-28 11:08:24 -04:00
|
|
|
use super::dispatch_json::{Deserialize, Value};
|
2020-02-24 14:48:14 -05:00
|
|
|
use crate::diagnostics::Diagnostic;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::source_maps::get_orig_position;
|
|
|
|
use crate::source_maps::CachedMaps;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::ResourceTable;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::collections::HashMap;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &Rc<State>) {
|
2020-08-28 11:08:24 -04:00
|
|
|
let t = &CoreIsolate::state(i).borrow().resource_table.clone();
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_apply_source_map",
|
2020-08-28 11:08:24 -04:00
|
|
|
s.stateful_json_op_sync(t, op_apply_source_map),
|
2019-10-11 14:41:54 -04:00
|
|
|
);
|
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_format_diagnostic",
|
2020-08-28 11:08:24 -04:00
|
|
|
s.stateful_json_op_sync(t, op_format_diagnostic),
|
2020-02-24 14:48:14 -05:00
|
|
|
);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2020-04-13 10:54:16 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-08-26 08:50:21 -04:00
|
|
|
struct ApplySourceMap {
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: String,
|
|
|
|
line_number: i32,
|
|
|
|
column_number: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_apply_source_map(
|
2020-08-28 11:08:24 -04:00
|
|
|
state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: ApplySourceMap = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
let mut mappings_map: CachedMaps = HashMap::new();
|
2020-04-13 10:54:16 -04:00
|
|
|
let (orig_file_name, orig_line_number, orig_column_number) =
|
|
|
|
get_orig_position(
|
|
|
|
args.file_name,
|
|
|
|
args.line_number.into(),
|
|
|
|
args.column_number.into(),
|
|
|
|
&mut mappings_map,
|
2020-08-18 12:30:13 -04:00
|
|
|
&state.global_state.ts_compiler,
|
2020-04-13 10:54:16 -04:00
|
|
|
);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-04-13 10:54:16 -04:00
|
|
|
"fileName": orig_file_name,
|
|
|
|
"lineNumber": orig_line_number as u32,
|
|
|
|
"columnNumber": orig_column_number as u32,
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2020-02-24 14:48:14 -05:00
|
|
|
|
|
|
|
fn op_format_diagnostic(
|
2020-08-28 11:08:24 -04:00
|
|
|
_state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2020-02-24 14:48:14 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-05-05 12:23:15 -04:00
|
|
|
let diagnostic = serde_json::from_value::<Diagnostic>(args)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(diagnostic.to_string()))
|
2020-02-24 14:48:14 -05:00
|
|
|
}
|