forked from denoland/deno_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_map.rs
168 lines (150 loc) · 4.75 KB
/
source_map.rs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
//! This mod provides functions to remap a `JsError` based on a source map.
use crate::resolve_url;
pub use sourcemap::SourceMap;
use std::collections::HashMap;
use std::rc::Rc;
use std::str;
pub trait SourceMapGetter {
/// Returns the raw source map file.
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>>;
fn get_source_line(
&self,
file_name: &str,
line_number: usize,
) -> Option<String>;
}
impl<T> SourceMapGetter for Rc<T>
where
T: SourceMapGetter + ?Sized,
{
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
(**self).get_source_map(file_name)
}
fn get_source_line(
&self,
file_name: &str,
line_number: usize,
) -> Option<String> {
(**self).get_source_line(file_name, line_number)
}
}
pub enum SourceMapApplication {
/// No mapping was applied, the location is unchanged.
Unchanged,
/// Line and column were mapped to a new location.
LineAndColumn {
line_number: u32,
column_number: u32,
},
/// Line, column and file name were mapped to a new location.
LineAndColumnAndFileName {
file_name: String,
line_number: u32,
column_number: u32,
},
}
pub struct SourceMapper<G: SourceMapGetter> {
maps: HashMap<String, Option<SourceMap>>,
source_lines: HashMap<(String, i64), Option<String>>,
getter: Option<G>,
pub(crate) ext_source_maps: HashMap<String, Vec<u8>>,
// This is not the right place for this, but it's the easiest way to make
// op_apply_source_map a fast op. This stashing should happen in #[op2].
pub(crate) stashed_file_name: Option<String>,
}
impl<G: SourceMapGetter> SourceMapper<G> {
pub fn new(getter: Option<G>) -> Self {
Self {
maps: Default::default(),
source_lines: Default::default(),
ext_source_maps: Default::default(),
getter,
stashed_file_name: Default::default(),
}
}
pub fn has_user_sources(&self) -> bool {
self.getter.is_some()
}
/// Apply a source map to the passed location. If there is no source map for
/// this location, or if the location remains unchanged after mapping, the
/// changed values are returned.
///
/// Line and column numbers are 1-based.
pub fn apply_source_map(
&mut self,
file_name: &str,
line_number: u32,
column_number: u32,
) -> SourceMapApplication {
// Lookup expects 0-based line and column numbers, but ours are 1-based.
let line_number = line_number - 1;
let column_number = column_number - 1;
let getter = self.getter.as_ref();
let maybe_source_map =
self.maps.entry(file_name.to_owned()).or_insert_with(|| {
None
.or_else(|| {
SourceMap::from_slice(self.ext_source_maps.get(file_name)?).ok()
})
.or_else(|| {
SourceMap::from_slice(&getter?.get_source_map(file_name)?).ok()
})
});
let Some(source_map) = maybe_source_map.as_ref() else {
return SourceMapApplication::Unchanged;
};
let Some(token) = source_map.lookup_token(line_number, column_number)
else {
return SourceMapApplication::Unchanged;
};
let new_line_number = token.get_src_line() + 1;
let new_column_number = token.get_src_col() + 1;
let new_file_name = match token.get_source() {
Some(source_file_name) => {
if source_file_name == file_name {
None
} else {
// The `source_file_name` written by tsc in the source map is
// sometimes only the basename of the URL, or has unwanted `<`/`>`
// around it. Use the `file_name` we get from V8 if
// `source_file_name` does not parse as a URL.
match resolve_url(source_file_name) {
Ok(m) if m.scheme() == "blob" => None,
Ok(m) => Some(m.to_string()),
Err(_) => None,
}
}
}
None => None,
};
match new_file_name {
None => SourceMapApplication::LineAndColumn {
line_number: new_line_number,
column_number: new_column_number,
},
Some(file_name) => SourceMapApplication::LineAndColumnAndFileName {
file_name,
line_number: new_line_number,
column_number: new_column_number,
},
}
}
const MAX_SOURCE_LINE_LENGTH: usize = 150;
pub fn get_source_line(
&mut self,
file_name: &str,
line_number: i64,
) -> Option<String> {
let getter = self.getter.as_ref()?;
self
.source_lines
.entry((file_name.to_string(), line_number))
.or_insert_with(|| {
// Source lookup expects a 0-based line number, ours are 1-based.
let s = getter.get_source_line(file_name, (line_number - 1) as usize);
s.filter(|s| s.len() <= Self::MAX_SOURCE_LINE_LENGTH)
})
.clone()
}
}