use rhaiper_map::vlq;
use crate::emit::Mapping;
use crate::stream::LineIndex;
#[derive(Debug, Clone)]
pub struct MapOptions {
pub file: Option<String>,
pub source_name: String,
pub include_sources_content: bool,
}
impl Default for MapOptions {
fn default() -> Self {
Self {
file: None,
source_name: "input.rhai".into(),
include_sources_content: true,
}
}
}
fn encode_mappings(src: &str, minified: &str, mappings: &[Mapping]) -> String {
let src_lines = LineIndex::new(src);
let gen_lines = LineIndex::new(minified);
let mut out = String::new();
let (mut prev_gen_col, mut prev_src_line, mut prev_src_col) = (0i64, 0i64, 0i64);
let mut line_no = 0u32;
let mut first_on_line = true;
for m in mappings {
while line_no < m.gen_line {
out.push(';');
line_no += 1;
prev_gen_col = 0;
first_on_line = true;
}
if !first_on_line {
out.push(',');
}
first_on_line = false;
let gen_col = i64::from(gen_lines.utf16_col(minified, m.gen_line, m.gen_col));
let src_col = i64::from(src_lines.utf16_col(src, m.src_line, m.src_col));
let src_line = i64::from(m.src_line);
vlq::encode(gen_col - prev_gen_col, &mut out);
vlq::encode(0, &mut out); vlq::encode(src_line - prev_src_line, &mut out);
vlq::encode(src_col - prev_src_col, &mut out);
prev_gen_col = gen_col;
prev_src_line = src_line;
prev_src_col = src_col;
}
out
}
pub fn to_json(src: &str, minified: &str, mappings: &[Mapping], opts: &MapOptions) -> String {
let mappings = encode_mappings(src, minified, mappings);
let mut doc = serde_json::Map::new();
doc.insert("version".into(), 3.into());
if let Some(file) = &opts.file {
doc.insert("file".into(), file.clone().into());
}
doc.insert(
"sources".into(),
serde_json::Value::Array(vec![opts.source_name.clone().into()]),
);
if opts.include_sources_content {
doc.insert(
"sourcesContent".into(),
serde_json::Value::Array(vec![src.into()]),
);
}
doc.insert("names".into(), serde_json::Value::Array(Vec::new()));
doc.insert("mappings".into(), mappings.into());
serde_json::Value::Object(doc).to_string()
}
#[derive(Debug, Clone)]
pub struct SourceMap {
pub sources: Vec<String>,
pub sources_content: Vec<Option<String>>,
pub mappings: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Original {
pub source: String,
pub line: u32,
pub column: u32,
}
impl SourceMap {
pub fn from_json(json: &str) -> Result<Self, String> {
let doc: serde_json::Value =
serde_json::from_str(json).map_err(|e| format!("malformed source map: {e}"))?;
let strings = |key: &str| -> Vec<Option<String>> {
doc.get(key)
.and_then(serde_json::Value::as_array)
.map(|a| a.iter().map(|v| v.as_str().map(str::to_owned)).collect())
.unwrap_or_default()
};
let sources = strings("sources").into_iter().flatten().collect();
let mappings = doc
.get("mappings")
.and_then(serde_json::Value::as_str)
.ok_or("source map has no `mappings` field")?
.to_owned();
Ok(Self {
sources,
sources_content: strings("sourcesContent"),
mappings,
})
}
pub fn resolve(&self, minified: &str, line: u32, column: u32) -> Option<Original> {
let gen_line = line.checked_sub(1)?;
let gen_char_col = column.saturating_sub(1);
let gen_col = LineIndex::new(minified).utf16_col(minified, gen_line, gen_char_col);
let found = rhaiper_map::resolve(&self.mappings, gen_line, gen_col)?;
let source = self.sources.get(found.source as usize)?.clone();
let column = match self
.sources_content
.get(found.source as usize)
.and_then(Option::as_ref)
{
Some(text) => LineIndex::new(text).char_col(text, found.line, found.col) + 1,
None => found.col + 1,
};
Some(Original {
source,
line: found.line + 1,
column,
})
}
}