Skip to content

Commit 70c51e4

Browse files
authored
feat: Support debug ids (#66)
1 parent 81cde2f commit 70c51e4

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Various fixes & improvements
6+
7+
- feat: Sourcemaps now support debug ids (#66) by @loewenheim
8+
39
## 6.2.3
410

511
### Various fixes & improvements

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ unicode-id = "0.3"
3131
if_chain = "1.0.0"
3232
scroll = { version = "0.10.1", features = ["derive"], optional = true }
3333
data-encoding = "2.3.3"
34+
debugid = {version = "0.8.0", features = ["serde"] }
3435

3536
[build-dependencies]
3637
rustc_version = "0.2.3"

src/builder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fs;
77
use std::io::Read;
88
use std::path::{Path, PathBuf};
99

10+
use debugid::DebugId;
1011
use url::Url;
1112

1213
use crate::errors::Result;
@@ -27,6 +28,7 @@ pub struct SourceMapBuilder {
2728
sources: Vec<String>,
2829
source_contents: Vec<Option<String>>,
2930
sources_mapping: Vec<u32>,
31+
debug_id: Option<DebugId>,
3032
}
3133

3234
#[cfg(any(unix, windows, target_os = "redox"))]
@@ -59,9 +61,15 @@ impl SourceMapBuilder {
5961
sources: vec![],
6062
source_contents: vec![],
6163
sources_mapping: vec![],
64+
debug_id: None,
6265
}
6366
}
6467

68+
/// Sets the debug id for the sourcemap (optional)
69+
pub fn set_debug_id(&mut self, debug_id: Option<DebugId>) {
70+
self.debug_id = debug_id;
71+
}
72+
6573
/// Sets the file for the sourcemap (optional)
6674
pub fn set_file<T: Into<String>>(&mut self, value: Option<T>) {
6775
self.file = value.map(Into::into);
@@ -285,6 +293,7 @@ impl SourceMapBuilder {
285293

286294
let mut sm = SourceMap::new(self.file, self.tokens, self.names, self.sources, contents);
287295
sm.set_source_root(self.source_root);
296+
sm.set_debug_id(self.debug_id);
288297

289298
sm
290299
}

src/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {
234234

235235
let mut sm = SourceMap::new(file, tokens, names, sources, rsm.sources_content);
236236
sm.set_source_root(rsm.source_root);
237+
sm.set_debug_id(rsm.debug_id);
237238

238239
Ok(sm)
239240
}

src/encoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl Encodable for SourceMap {
9393
x_facebook_offsets: None,
9494
x_metro_module_paths: None,
9595
x_facebook_sources: None,
96+
debug_id: self.get_debug_id(),
9697
}
9798
}
9899
}
@@ -124,6 +125,7 @@ impl Encodable for SourceMapIndex {
124125
x_facebook_offsets: None,
125126
x_metro_module_paths: None,
126127
x_facebook_sources: None,
128+
debug_id: None,
127129
}
128130
}
129131
}

src/jsontypes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use debugid::DebugId;
12
use serde::de::IgnoredAny;
23
use serde::{Deserialize, Serialize};
34
use serde_json::Value;
@@ -49,6 +50,8 @@ pub struct RawSourceMap {
4950
pub x_metro_module_paths: Option<Vec<String>>,
5051
#[serde(skip_serializing_if = "Option::is_none")]
5152
pub x_facebook_sources: FacebookSources,
53+
#[serde(skip_serializing_if = "Option::is_none")]
54+
pub debug_id: Option<DebugId>,
5255
}
5356

5457
#[derive(Deserialize)]

src/types.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ use crate::hermes::SourceMapHermes;
1212
use crate::sourceview::SourceView;
1313
use crate::utils::{find_common_prefix, greatest_lower_bound};
1414

15+
use debugid::DebugId;
16+
1517
/// Controls the `SourceMap::rewrite` behavior
1618
///
1719
/// Default configuration:
1820
///
1921
/// * `with_names`: true
2022
/// * `with_source_contents`: true
2123
/// * `load_local_source_contents`: false
24+
#[derive(Debug, Clone)]
2225
pub struct RewriteOptions<'a> {
2326
/// If enabled, names are kept in the rewritten sourcemap.
2427
pub with_names: bool,
@@ -461,6 +464,7 @@ pub struct SourceMap {
461464
source_root: Option<String>,
462465
sources: Vec<String>,
463466
sources_content: Vec<Option<SourceView<'static>>>,
467+
debug_id: Option<DebugId>,
464468
}
465469

466470
impl SourceMap {
@@ -569,9 +573,20 @@ impl SourceMap {
569573
.into_iter()
570574
.map(|opt| opt.map(SourceView::from_string))
571575
.collect(),
576+
debug_id: None,
572577
}
573578
}
574579

580+
/// Returns the embedded debug id.
581+
pub fn get_debug_id(&self) -> Option<DebugId> {
582+
self.debug_id
583+
}
584+
585+
/// Sets a new value for the debug id.
586+
pub fn set_debug_id(&mut self, debug_id: Option<DebugId>) {
587+
self.debug_id = debug_id
588+
}
589+
575590
/// Returns the embedded filename in case there is one.
576591
pub fn get_file(&self) -> Option<&str> {
577592
self.file.as_deref()
@@ -766,6 +781,7 @@ impl SourceMap {
766781
options: &RewriteOptions<'_>,
767782
) -> Result<(SourceMap, Vec<u32>)> {
768783
let mut builder = SourceMapBuilder::new(self.get_file());
784+
builder.set_debug_id(self.debug_id);
769785

770786
for token in self.tokens() {
771787
let raw = builder.add_token(&token, options.with_names);
@@ -1065,3 +1081,33 @@ impl SourceMapSection {
10651081
self.map = sm.map(Box::new);
10661082
}
10671083
}
1084+
1085+
#[cfg(test)]
1086+
mod tests {
1087+
use super::{RewriteOptions, SourceMap};
1088+
use debugid::DebugId;
1089+
1090+
#[test]
1091+
fn test_rewrite_debugid() {
1092+
let input: &[_] = br#"{
1093+
"version":3,
1094+
"sources":["coolstuff.js"],
1095+
"names":["x","alert"],
1096+
"mappings":"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM",
1097+
"debug_id":"00000000-0000-0000-0000-000000000000"
1098+
}"#;
1099+
1100+
let sm = SourceMap::from_slice(input).unwrap();
1101+
1102+
assert_eq!(sm.debug_id, Some(DebugId::default()));
1103+
1104+
let new_sm = sm
1105+
.rewrite(&RewriteOptions {
1106+
with_names: false,
1107+
..Default::default()
1108+
})
1109+
.unwrap();
1110+
1111+
assert_eq!(new_sm.debug_id, Some(DebugId::default()));
1112+
}
1113+
}

0 commit comments

Comments
 (0)