forked from getsentry/rust-sourcemap
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
191 lines (177 loc) · 5.37 KB
/
main.rs
File metadata and controls
191 lines (177 loc) · 5.37 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::fs;
use std::path::PathBuf;
use std::process;
use argh::FromArgs;
use swc_sourcemap::{DecodedMap, SourceView, Token};
/// Utility for working with source maps.
#[derive(FromArgs, Debug)]
pub struct Cli {
/// the source map to process
#[argh(option, short = 's')]
sourcemap: Option<PathBuf>,
/// the minified input file
#[argh(option, short = 'm')]
minified_file: Option<PathBuf>,
/// the 0 indexed line number
#[argh(option, short = 'L')]
line0: Option<u32>,
/// the 1 indexed line number
#[argh(option, short = 'l')]
line: Option<u32>,
/// the 0 indexed column number
#[argh(option, short = 'C')]
column0: Option<u32>,
/// the 1 indexed column number
#[argh(option, short = 'c')]
column: Option<u32>,
/// the function name that should be mapped
#[argh(option, short = 'f')]
function: Option<String>,
/// dumps all tokens
#[argh(switch)]
dump: bool,
}
impl Cli {
/// Retrurns the zero indexed line
fn lookup_pos(&self) -> Option<(u32, u32)> {
Some((
self.line0.unwrap_or_else(|| self.line.map_or(0, |x| x - 1)),
self.column0.or_else(|| self.column.map(|x| x - 1))?,
))
}
}
fn bail(msg: &str) -> ! {
eprintln!("error: {}", msg);
process::exit(1);
}
fn print_token(
sm: &DecodedMap,
token: &Token<'_>,
line: u32,
column: u32,
func: Option<&str>,
sv: Option<&SourceView>,
) {
if let Some(name) = token.get_name() {
println!(" name: {:?}", name);
} else {
println!(" name: not found");
}
if let Some(source) = token.get_source() {
println!(" source file: {:?}", source);
} else {
println!(" source file: not found");
}
println!(" source line: {}", token.get_src_line());
println!(" source column: {}", token.get_src_col());
println!(" minified line: {}", token.get_dst_line());
println!(" minified column: {}", token.get_dst_col());
if let Some(name) = sm.get_original_function_name(line, column, func, sv) {
println!(" original function: {:?}", name);
} else {
println!(" original function: not found");
}
if let Some(line) = token
.get_source_view()
.and_then(|sv| sv.get_line(token.get_src_line()))
{
println!(" source line:");
println!(" {}", line.trim());
} else if token.get_source_view().is_none() {
println!(" cannot find source");
} else {
println!(" cannot find source line");
}
}
fn main() {
if let Err(err) = execute() {
eprintln!("error: {}", err);
}
}
fn execute() -> Result<(), Box<dyn std::error::Error>> {
let args: Cli = argh::from_env();
let sv = if let Some(ref path) = args.minified_file {
Some(SourceView::from_string(fs::read_to_string(&path)?))
} else {
None
};
let (sm, sourcemap_path) = match (&args.sourcemap, &sv) {
(&Some(ref path), _) => (None, Some(path.to_path_buf())),
(&None, &Some(ref sv)) => {
if let Some(smref) = sv.sourcemap_reference()? {
if let Some(sm) = smref.get_embedded_sourcemap()? {
(Some(sm), None)
} else {
(
None,
smref.resolve_path(args.minified_file.as_ref().unwrap()),
)
}
} else {
bail("missing sourcemap reference");
}
}
_ => bail("sourcemap not provided"),
};
let sm = if let Some(sm) = sm {
sm
} else {
sourcemap::decode_slice(&fs::read(&sourcemap_path.as_ref().unwrap())?)?
};
let ty = match sm {
DecodedMap::Regular(..) => "regular",
DecodedMap::Index(..) => "indexed",
DecodedMap::Hermes(..) => "hermes",
};
if let Some(ref path) = sourcemap_path {
println!("source map path: {:?}", path);
} else {
println!("embedded source map");
}
println!("source map type: {}", ty);
// perform a lookup
if let Some((line, column)) = args.lookup_pos() {
println!("lookup line: {}, column: {}:", line, column);
if let Some(token) = sm.lookup_token(line, column) {
print_token(
&sm,
&token,
line,
column,
args.function.as_deref(),
sv.as_ref(),
);
} else {
println!(" - no match");
}
}
// dump all tokens
if args.dump {
println!("tokens:");
let converted_map;
let token_iter = match sm {
DecodedMap::Regular(ref sm) => sm.tokens(),
DecodedMap::Index(ref sm) => {
converted_map = sm.flatten()?;
converted_map.tokens()
}
DecodedMap::Hermes(ref sm) => sm.tokens(),
};
for token in token_iter {
println!(
" {}:{} -> {}:{}:{}{}",
token.get_dst_line(),
token.get_dst_col(),
token.get_source().unwrap_or(""),
token.get_src_line(),
token.get_src_col(),
if let Some(name) = token.get_name() {
format!(" [{}]", name)
} else {
"".to_string()
},
);
}
}
Ok(())
}