1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::sync::Arc;

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use sourcemap::{vlq::parse_vlq_segment, RawToken, SourceMap};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SourceMapContent {
    Json(String),
    #[serde(rename_all = "camelCase")]
    Parsed {
        #[serde(default)]
        sources: Vec<Arc<str>>,
        #[serde(default)]
        names: Vec<Arc<str>>,
        #[serde(default)]
        mappings: String,
        #[serde(default)]
        range_mappings: String,
        #[serde(default)]
        file: Option<Arc<str>>,
        #[serde(default)]
        source_root: Option<String>,
        #[serde(default)]
        sources_content: Option<Vec<Option<Arc<str>>>>,
    },
}

impl SourceMapContent {
    pub fn to_sourcemap(&self) -> Result<SourceMap> {
        match self {
            SourceMapContent::Json(s) => {
                SourceMap::from_slice(s.as_bytes()).context("failed to parse sourcemap")
            }
            SourceMapContent::Parsed {
                sources,
                names,
                mappings,
                range_mappings: _,
                file,
                source_root,
                sources_content,
            } => {
                let mut dst_col;
                let mut src_id = 0;
                let mut src_line = 0;
                let mut src_col = 0;
                let mut name_id = 0;

                let allocation_size = mappings.matches(&[',', ';'][..]).count() + 10;
                let mut tokens = Vec::with_capacity(allocation_size);

                let mut nums = Vec::with_capacity(6);

                for (dst_line, line) in mappings.split(';').enumerate() {
                    if line.is_empty() {
                        continue;
                    }

                    dst_col = 0;

                    for segment in line.split(',') {
                        if segment.is_empty() {
                            continue;
                        }

                        nums.clear();
                        nums = parse_vlq_segment(segment)?;
                        dst_col = (i64::from(dst_col) + nums[0]) as u32;

                        let mut src = !0;
                        let mut name = !0;

                        if nums.len() > 1 {
                            if nums.len() != 4 && nums.len() != 5 {
                                bail!(
                                    "invalid vlq segment size; expected 4 or 5, got {}",
                                    nums.len()
                                );
                            }
                            src_id = (i64::from(src_id) + nums[1]) as u32;
                            if src_id >= sources.len() as u32 {
                                bail!("invalid source reference: {}", src_id);
                            }

                            src = src_id;
                            src_line = (i64::from(src_line) + nums[2]) as u32;
                            src_col = (i64::from(src_col) + nums[3]) as u32;

                            if nums.len() > 4 {
                                name_id = (i64::from(name_id) + nums[4]) as u32;
                                if name_id >= names.len() as u32 {
                                    bail!("invalid name reference: {}", name_id);
                                }
                                name = name_id;
                            }
                        }

                        tokens.push(RawToken {
                            dst_line: dst_line as u32,
                            dst_col,
                            src_line,
                            src_col,
                            src_id: src,
                            name_id: name,
                            is_range: false,
                        });
                    }
                }

                let mut map = SourceMap::new(
                    file.clone(),
                    tokens,
                    names.clone(),
                    sources.clone(),
                    sources_content.clone(),
                );
                map.set_source_root(source_root.clone());
                Ok(map)
            }
        }
    }
}