2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2020-09-12 19:53:57 +10:00
|
|
|
use crate::diagnostics::Diagnostics;
|
2021-09-18 14:40:04 +01:00
|
|
|
use crate::fmt_errors::format_file_name;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2019-08-14 17:03:02 +02:00
|
|
|
use crate::source_maps::get_orig_position;
|
|
|
|
use crate::source_maps::CachedMaps;
|
2020-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::AnyError;
|
2021-12-29 14:30:08 +01:00
|
|
|
use deno_core::op_sync;
|
2020-09-21 18:36:37 +02:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2021-12-29 14:30:08 +01:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-10-05 22:38:27 +02:00
|
|
|
use serde::Serialize;
|
2019-08-14 17:03:02 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-12-29 14:30:08 +01:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
("op_apply_source_map", op_sync(op_apply_source_map)),
|
|
|
|
("op_format_diagnostic", op_sync(op_format_diagnostic)),
|
|
|
|
("op_format_file_name", op_sync(op_format_file_name)),
|
|
|
|
])
|
|
|
|
.build()
|
2019-10-11 11:41:54 -07:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:50:21 +02:00
|
|
|
#[derive(Deserialize)]
|
2020-04-13 15:54:16 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-08-26 14:50:21 +02:00
|
|
|
struct ApplySourceMap {
|
2020-04-13 15:54:16 +01:00
|
|
|
file_name: String,
|
|
|
|
line_number: i32,
|
|
|
|
column_number: i32,
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:38:27 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct AppliedSourceMap {
|
|
|
|
file_name: String,
|
|
|
|
line_number: u32,
|
|
|
|
column_number: u32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 11:41:54 -07:00
|
|
|
fn op_apply_source_map(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-10-05 22:38:27 +02:00
|
|
|
args: ApplySourceMap,
|
2021-05-08 14:37:42 +02:00
|
|
|
_: (),
|
2021-10-05 22:38:27 +02:00
|
|
|
) -> Result<AppliedSourceMap, AnyError> {
|
2019-08-14 17:03:02 +02:00
|
|
|
let mut mappings_map: CachedMaps = HashMap::new();
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = state.borrow::<ProcState>().clone();
|
2020-12-11 18:49:26 +01:00
|
|
|
|
2021-01-06 00:10:36 +01:00
|
|
|
let (orig_file_name, orig_line_number, orig_column_number, _) =
|
2020-04-13 15:54:16 +01:00
|
|
|
get_orig_position(
|
|
|
|
args.file_name,
|
|
|
|
args.line_number.into(),
|
|
|
|
args.column_number.into(),
|
|
|
|
&mut mappings_map,
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-04-13 15:54:16 +01:00
|
|
|
);
|
2019-08-14 17:03:02 +02:00
|
|
|
|
2021-10-05 22:38:27 +02:00
|
|
|
Ok(AppliedSourceMap {
|
|
|
|
file_name: orig_file_name,
|
|
|
|
line_number: orig_line_number as u32,
|
|
|
|
column_number: orig_column_number as u32,
|
|
|
|
})
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
2020-02-25 06:48:14 +11:00
|
|
|
|
|
|
|
fn op_format_diagnostic(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2020-02-25 06:48:14 +11:00
|
|
|
args: Value,
|
2021-05-08 14:37:42 +02:00
|
|
|
_: (),
|
2020-09-14 18:48:57 +02:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-12 19:53:57 +10:00
|
|
|
let diagnostic: Diagnostics = serde_json::from_value(args)?;
|
2020-08-28 17:08:24 +02:00
|
|
|
Ok(json!(diagnostic.to_string()))
|
2020-02-25 06:48:14 +11:00
|
|
|
}
|
2021-09-18 14:40:04 +01:00
|
|
|
|
|
|
|
fn op_format_file_name(
|
|
|
|
_state: &mut OpState,
|
|
|
|
file_name: String,
|
|
|
|
_: (),
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
Ok(format_file_name(&file_name))
|
|
|
|
}
|