swc_sourcemap 10.0.0

Forked from https://github.com/getsentry/rust-sourcemap
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! This is _lazy_ because we skip deserializing all the fields that we don't
//! need. (unlike the original crate)

use std::{
    borrow::Cow,
    collections::{BTreeSet, HashMap},
};

use bitvec::{order::Lsb0, vec::BitVec, view::BitView};
use bytes_str::BytesStr;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::RawValue;

use crate::{
    decoder::{decode_rmi, strip_junk_header},
    encoder::{encode_rmi, encode_vlq_diff},
    types::adjust_mappings,
    vlq::parse_vlq_segment_into,
    Error, RawToken, Result,
};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RawSourceMap<'a> {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) version: Option<u32>,
    #[serde(default, borrow, skip_serializing_if = "Option::is_none")]
    pub(crate) file: Option<MaybeRawValue<'a, Str>>,
    #[serde(borrow)]
    pub(crate) sources: MaybeRawValue<'a, Vec<StrValue<'a>>>,
    #[serde(default, borrow, skip_serializing_if = "Option::is_none")]
    pub(crate) source_root: Option<StrValue<'a>>,
    #[serde(default, borrow, skip_serializing_if = "MaybeRawValue::is_empty")]
    pub(crate) sources_content: MaybeRawValue<'a, Vec<Option<StrValue<'a>>>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) sections: Option<Vec<RawSection<'a>>>,
    #[serde(default, borrow)]
    pub(crate) names: MaybeRawValue<'a, Vec<StrValue<'a>>>,
    #[serde(default, borrow, skip_serializing_if = "Option::is_none")]
    pub(crate) scopes: Option<StrValue<'a>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) range_mappings: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) mappings: Option<String>,
    #[serde(default, borrow, skip_serializing_if = "Option::is_none")]
    pub(crate) ignore_list: Option<MaybeRawValue<'a, BTreeSet<u32>>>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
pub struct RawSectionOffset {
    pub line: u32,
    pub column: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RawSection<'a> {
    pub offset: RawSectionOffset,
    #[serde(borrow)]
    pub url: Option<&'a RawValue>,
    #[serde(borrow)]
    pub map: Option<&'a RawValue>,
}

#[derive(Debug)]
pub enum DecodedMap<'a> {
    Regular(SourceMap<'a>),
    Index(SourceMapIndex<'a>),
}

impl<'a> DecodedMap<'a> {
    pub fn into_source_map(self) -> Result<SourceMap<'a>> {
        match self {
            DecodedMap::Regular(source_map) => Ok(source_map),
            DecodedMap::Index(source_map_index) => source_map_index.flatten(),
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(untagged)]
pub(crate) enum MaybeRawValue<'a, T> {
    RawValue(#[serde(borrow)] &'a RawValue),
    Data(T),
}

impl<T> MaybeRawValue<'_, Vec<T>> {
    pub fn is_empty(&self) -> bool {
        match self {
            MaybeRawValue::Data(vec) => vec.is_empty(),
            MaybeRawValue::RawValue(_) => false,
        }
    }
}

impl<'a, 'de, T> Deserialize<'de> for MaybeRawValue<'a, T>
where
    'de: 'a,
    T: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw: &'de RawValue = Deserialize::deserialize(deserializer)?;
        Ok(MaybeRawValue::RawValue(raw))
    }
}

impl<'a, T> MaybeRawValue<'a, T>
where
    T: Deserialize<'a>,
{
    pub fn into_data(self) -> T {
        match self {
            MaybeRawValue::RawValue(s) => {
                serde_json::from_str(s.get()).expect("Failed to convert RawValue to Data")
            }
            MaybeRawValue::Data(data) => data,
        }
    }

    fn assert_raw_value(self) -> &'a RawValue {
        match self {
            MaybeRawValue::RawValue(s) => s,
            MaybeRawValue::Data(_) => unreachable!("Expected RawValue, got Data"),
        }
    }
}

impl<T> Default for MaybeRawValue<'_, T>
where
    T: Default,
{
    fn default() -> Self {
        MaybeRawValue::Data(T::default())
    }
}

impl<'a, T> MaybeRawValue<'a, T>
where
    T: Deserialize<'a>,
    T: Default,
{
    pub fn as_data(&mut self) -> &mut T {
        match self {
            MaybeRawValue::RawValue(s) => {
                *self = MaybeRawValue::Data(
                    serde_json::from_str(s.get()).expect("Failed to convert RawValue to Data"),
                );
                if let MaybeRawValue::Data(data) = self {
                    data
                } else {
                    unreachable!()
                }
            }
            MaybeRawValue::Data(data) => data,
        }
    }
}

type Str = BytesStr;

type StrValue<'a> = MaybeRawValue<'a, Str>;

#[derive(Debug)]
pub struct SourceMap<'a> {
    pub(crate) file: Option<StrValue<'a>>,
    pub(crate) tokens: Vec<RawToken>,
    pub(crate) names: MaybeRawValue<'a, Vec<StrValue<'a>>>,
    pub(crate) scopes: Option<StrValue<'a>>,
    pub(crate) source_root: Option<StrValue<'a>>,
    pub(crate) sources: MaybeRawValue<'a, Vec<StrValue<'a>>>,
    pub(crate) sources_content: MaybeRawValue<'a, Vec<Option<StrValue<'a>>>>,
    pub(crate) ignore_list: Option<MaybeRawValue<'a, BTreeSet<u32>>>,
}

#[derive(Debug)]
pub(crate) struct SourceMapBuilder<'a> {
    file: Option<StrValue<'a>>,
    name_map: HashMap<&'a str, u32>,
    names: Vec<StrValue<'a>>,
    tokens: Vec<RawToken>,
    source_map: HashMap<&'a str, u32>,
    sources: Vec<StrValue<'a>>,
    source_contents: Vec<Option<StrValue<'a>>>,
    source_root: Option<StrValue<'a>>,
    ignore_list: Option<BTreeSet<u32>>,
}

impl<'a> SourceMapBuilder<'a> {
    pub fn new(file: Option<StrValue<'a>>) -> Self {
        SourceMapBuilder {
            file,
            name_map: HashMap::new(),
            names: Vec::new(),
            tokens: Vec::new(),
            source_map: HashMap::new(),
            sources: Vec::new(),
            source_contents: Vec::new(),
            source_root: None,
            ignore_list: None,
        }
    }

    pub fn add_source(&mut self, src_raw: &'a RawValue) -> u32 {
        let src_str = src_raw.get(); // RawValue provides get() -> &str
        let count = self.sources.len() as u32;
        let id = *self.source_map.entry(src_str).or_insert(count);
        if id == count {
            // New source
            self.sources.push(MaybeRawValue::RawValue(src_raw));
            // Ensure source_contents has a corresponding entry, defaulting to None.
            // This logic ensures source_contents is always same length as sources if new
            // one added.
            self.source_contents.resize(self.sources.len(), None);
        }
        id
    }

    pub fn add_name(&mut self, name_raw: &'a RawValue) -> u32 {
        let name_str = name_raw.get();
        let count = self.names.len() as u32;
        let id = *self.name_map.entry(name_str).or_insert(count);
        if id == count {
            // New name
            self.names.push(MaybeRawValue::RawValue(name_raw));
        }
        id
    }

    pub fn set_source_contents(&mut self, src_id: u32, contents: Option<&'a RawValue>) {
        // Ensure source_contents is large enough. src_id is 0-indexed.
        if (src_id as usize) >= self.source_contents.len() {
            self.source_contents.resize(src_id as usize + 1, None);
        }
        self.source_contents[src_id as usize] = contents.map(MaybeRawValue::RawValue);
    }

    pub fn add_to_ignore_list(&mut self, src_id: u32) {
        self.ignore_list
            .get_or_insert_with(BTreeSet::new)
            .insert(src_id);
    }

    pub fn into_sourcemap(self) -> SourceMap<'a> {
        SourceMap {
            file: self.file,
            tokens: self.tokens,
            names: MaybeRawValue::Data(self.names),
            scopes: None,
            source_root: self.source_root,
            sources: MaybeRawValue::Data(self.sources),
            sources_content: MaybeRawValue::Data(self.source_contents),
            ignore_list: self.ignore_list.map(MaybeRawValue::Data),
        }
    }

    /// Adds a new mapping to the builder.
    #[allow(clippy::too_many_arguments)]
    pub fn add_raw(
        &mut self,
        dst_line: u32,
        dst_col: u32,
        src_line: u32,
        src_col: u32,
        source: Option<u32>,
        name: Option<u32>,
        is_range: bool,
    ) -> RawToken {
        let src_id = source.unwrap_or(!0);
        let name_id = name.unwrap_or(!0);
        let raw = RawToken {
            dst_line,
            dst_col,
            src_line,
            src_col,
            src_id,
            name_id,
            is_range,
        };
        self.tokens.push(raw);
        raw
    }
}

#[derive(Debug)]
pub(crate) struct SourceMapSection<'a> {
    offset: (u32, u32),
    url: Option<MaybeRawValue<'a, String>>,
    map: Option<Box<MaybeRawValue<'a, RawSourceMap<'a>>>>,
}

impl<'a> SourceMapSection<'a> {
    /// Create a new sourcemap index section
    ///
    /// - `offset`: offset as line and column
    /// - `url`: optional URL of where the sourcemap is located
    /// - `map`: an optional already resolved internal sourcemap
    pub fn new(
        offset: (u32, u32),
        url: Option<MaybeRawValue<'a, String>>,
        map: Option<MaybeRawValue<'a, RawSourceMap<'a>>>,
    ) -> SourceMapSection<'a> {
        SourceMapSection {
            offset,
            url,
            map: map.map(Box::new),
        }
    }

    /// Returns the offset as tuple
    pub fn get_offset(&self) -> (u32, u32) {
        self.offset
    }
}

#[derive(Debug)]
pub struct SourceMapIndex<'a> {
    file: Option<MaybeRawValue<'a, Str>>,
    sections: Vec<SourceMapSection<'a>>,
}

pub fn decode(slice: &[u8]) -> Result<DecodedMap<'_>> {
    let content = strip_junk_header(slice)?;
    let rsm: RawSourceMap = serde_json::from_slice(content)?;

    decode_common(rsm)
}

fn decode_common(rsm: RawSourceMap) -> Result<DecodedMap> {
    if rsm.sections.is_some() {
        decode_index(rsm).map(DecodedMap::Index)
    } else {
        decode_regular(rsm).map(DecodedMap::Regular)
    }
}

fn decode_index(rsm: RawSourceMap) -> Result<SourceMapIndex> {
    let mut sections = vec![];

    for raw_section in rsm.sections.unwrap_or_default() {
        sections.push(SourceMapSection::new(
            (raw_section.offset.line, raw_section.offset.column),
            raw_section.url.map(MaybeRawValue::RawValue),
            raw_section.map.map(MaybeRawValue::RawValue),
        ));
    }

    sections.sort_by_key(SourceMapSection::get_offset);

    // file sometimes is not a string for unexplicable reasons
    let file = rsm.file;

    Ok(SourceMapIndex { file, sections })
}

pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {
    let mut dst_col;

    // Source IDs, lines, columns, and names are "running" values.
    // Each token (except the first) contains the delta from the previous value.
    let mut running_src_id = 0;
    let mut running_src_line = 0;
    let mut running_src_col = 0;
    let mut running_name_id = 0;

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

    let mut nums = Vec::with_capacity(6);
    let mut rmi = BitVec::new();

    for (dst_line, (line, rmi_str)) in mappings
        .split(';')
        .zip(range_mappings.split(';').chain(std::iter::repeat("")))
        .enumerate()
    {
        if line.is_empty() {
            continue;
        }

        dst_col = 0;

        decode_rmi(rmi_str, &mut rmi)?;

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

            nums.clear();
            parse_vlq_segment_into(segment, &mut nums)?;
            match nums.len() {
                1 | 4 | 5 => {}
                _ => return Err(Error::BadSegmentSize(nums.len() as u32)),
            }

            dst_col = (i64::from(dst_col) + nums[0]) as u32;

            // The source file , source line, source column, and name
            // may not be present in the current token. We use `u32::MAX`
            // as the placeholder for missing values.
            let mut current_src_id = !0;
            let mut current_src_line = !0;
            let mut current_src_col = !0;
            let mut current_name_id = !0;

            if nums.len() > 1 {
                running_src_id = (i64::from(running_src_id) + nums[1]) as u32;

                running_src_line = (i64::from(running_src_line) + nums[2]) as u32;
                running_src_col = (i64::from(running_src_col) + nums[3]) as u32;

                current_src_id = running_src_id;
                current_src_line = running_src_line;
                current_src_col = running_src_col;

                if nums.len() > 4 {
                    running_name_id = (i64::from(running_name_id) + nums[4]) as u32;
                    current_name_id = running_name_id;
                }
            }

            let is_range = rmi.get(line_index).map(|v| *v).unwrap_or_default();

            tokens.push(RawToken {
                dst_line: dst_line as u32,
                dst_col,
                src_line: current_src_line,
                src_col: current_src_col,
                src_id: current_src_id,
                name_id: current_name_id,
                is_range,
            });
        }
    }

    let sm = SourceMap {
        file: rsm.file,
        tokens,
        names: rsm.names,
        scopes: rsm.scopes,
        source_root: rsm.source_root,
        sources: rsm.sources,
        sources_content: rsm.sources_content,
        ignore_list: rsm.ignore_list,
    };

    Ok(sm)
}

impl<'a> SourceMap<'a> {
    /// Refer to [crate::SourceMap::adjust_mappings] for more details.
    pub fn adjust_mappings(&mut self, adjustment: crate::SourceMap) {
        self.tokens = adjust_mappings(
            std::mem::take(&mut self.tokens),
            Cow::Owned(adjustment.tokens),
        );
        self.scopes = None;
    }

    pub fn into_raw_sourcemap(self) -> RawSourceMap<'a> {
        RawSourceMap {
            version: Some(3),
            range_mappings: serialize_range_mappings(&self),
            mappings: Some(serialize_mappings(&self)),
            file: self.file,
            sources: self.sources,
            source_root: self.source_root,
            sources_content: self.sources_content,
            sections: None,
            names: self.names,
            scopes: self.scopes,
            ignore_list: self.ignore_list,
        }
    }

    pub fn file(&mut self) -> Option<&BytesStr> {
        self.file.as_mut().map(|f| &*f.as_data())
    }

    pub fn sources(&mut self) -> impl Iterator<Item = &'_ BytesStr> + use<'_, 'a> {
        self.sources.as_data().iter_mut().map(|d| &*d.as_data())
    }

    pub fn get_source(&mut self, src_id: u32) -> Option<&BytesStr> {
        self.sources
            .as_data()
            .get_mut(src_id as usize)
            .map(|d| &*d.as_data())
    }

    pub fn get_name(&mut self, src_id: u32) -> Option<&BytesStr> {
        self.names
            .as_data()
            .get_mut(src_id as usize)
            .map(|d| &*d.as_data())
    }

    pub fn get_source_contents(&mut self, src_id: u32) -> Option<&BytesStr> {
        self.sources_content
            .as_data()
            .get_mut(src_id as usize)
            .and_then(|d| d.as_mut().map(|d| &*d.as_data()))
    }
}

impl<'a> SourceMapIndex<'a> {
    pub fn flatten(self) -> Result<SourceMap<'a>> {
        let mut builder = SourceMapBuilder::new(self.file);

        for section in self.sections {
            let (off_line, off_col) = section.get_offset();

            let map = match section.map {
                Some(map) => match decode_common(map.into_data())? {
                    DecodedMap::Regular(sm) => sm,
                    DecodedMap::Index(idx) => idx.flatten()?,
                },
                None => {
                    return Err(Error::CannotFlatten(format!(
                        "Section has an unresolved sourcemap: {}",
                        section
                            .url
                            .map(|v| v.into_data())
                            .as_deref()
                            .unwrap_or("<unknown url>")
                    )));
                }
            };

            let sources = map.sources.into_data();
            let source_contents = map.sources_content.into_data();
            let ignore_list = map.ignore_list.unwrap_or_default().into_data();

            let mut src_id_map = Vec::<u32>::with_capacity(sources.len());

            for (original_id, (source, contents)) in
                sources.into_iter().zip(source_contents).enumerate()
            {
                debug_assert_eq!(original_id, src_id_map.len());
                let src_id = builder.add_source(source.assert_raw_value());

                src_id_map.push(src_id);

                if let Some(contents) = contents {
                    builder.set_source_contents(src_id, Some(contents.assert_raw_value()));
                }
            }

            let names = map.names.into_data();
            let mut name_id_map = Vec::<u32>::with_capacity(names.len());

            for (original_id, name) in names.into_iter().enumerate() {
                debug_assert_eq!(original_id, name_id_map.len());
                let name_id = builder.add_name(name.assert_raw_value());
                name_id_map.push(name_id);
            }

            for token in map.tokens {
                let dst_col = if token.dst_line == 0 {
                    token.dst_col + off_col
                } else {
                    token.dst_col
                };

                // Use u32 -> u32 map instead of using the hash map in SourceMapBuilder for
                // better performance
                let original_src_id = token.src_id;
                let src_id = if original_src_id == !0 {
                    None
                } else {
                    src_id_map.get(original_src_id as usize).copied()
                };

                let original_name_id = token.name_id;
                let name_id = if original_name_id == !0 {
                    None
                } else {
                    name_id_map.get(original_name_id as usize).copied()
                };

                let raw = builder.add_raw(
                    token.dst_line + off_line,
                    dst_col,
                    token.src_line,
                    token.src_col,
                    src_id,
                    name_id,
                    token.is_range,
                );

                if ignore_list.contains(&token.src_id) {
                    builder.add_to_ignore_list(raw.src_id);
                }
            }
        }

        Ok(builder.into_sourcemap())
    }
}

fn serialize_range_mappings(sm: &SourceMap) -> Option<String> {
    let mut buf = Vec::new();
    let mut prev_line = 0;
    let mut had_rmi = false;
    let mut empty = true;

    let mut idx_of_first_in_line = 0;

    let mut rmi_data = Vec::<u8>::new();

    for (idx, token) in sm.tokens.iter().enumerate() {
        if token.is_range {
            had_rmi = true;
            empty = false;

            let num = idx - idx_of_first_in_line;

            rmi_data.resize(rmi_data.len() + 2, 0);

            let rmi_bits = rmi_data.view_bits_mut::<Lsb0>();
            rmi_bits.set(num, true);
        }

        while token.dst_line != prev_line {
            if had_rmi {
                encode_rmi(&mut buf, &rmi_data);
                rmi_data.clear();
            }

            buf.push(b';');
            prev_line += 1;
            had_rmi = false;
            idx_of_first_in_line = idx;
        }
    }
    if empty {
        return None;
    }

    if had_rmi {
        encode_rmi(&mut buf, &rmi_data);
    }

    Some(String::from_utf8(buf).expect("invalid utf8"))
}

fn serialize_mappings(sm: &SourceMap) -> String {
    let mut rv = String::new();
    // dst == minified == generated
    let mut prev_dst_line = 0;
    let mut prev_dst_col = 0;
    let mut prev_src_line = 0;
    let mut prev_src_col = 0;
    let mut prev_name_id = 0;
    let mut prev_src_id = 0;

    for (idx, token) in sm.tokens.iter().enumerate() {
        if token.dst_line != prev_dst_line {
            prev_dst_col = 0;
            while token.dst_line != prev_dst_line {
                rv.push(';');
                prev_dst_line += 1;
            }
        } else if idx > 0 {
            if Some(&token) == sm.tokens.get(idx - 1).as_ref() {
                continue;
            }
            rv.push(',');
        }

        encode_vlq_diff(&mut rv, token.dst_col, prev_dst_col);
        prev_dst_col = token.dst_col;

        if token.src_id != !0 {
            encode_vlq_diff(&mut rv, token.src_id, prev_src_id);
            prev_src_id = token.src_id;
            encode_vlq_diff(&mut rv, token.src_line, prev_src_line);
            prev_src_line = token.src_line;
            encode_vlq_diff(&mut rv, token.src_col, prev_src_col);
            prev_src_col = token.src_col;
            if token.name_id != !0 {
                encode_vlq_diff(&mut rv, token.name_id, prev_name_id);
                prev_name_id = token.name_id;
            }
        }
    }

    rv
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_scopes_roundtrip() {
        let map = decode(
            br#"{
                "version": 3,
                "sources": ["coolstuff.js"],
                "names": [],
                "mappings": "AAAA",
                "scopes": "B,A,A,C,A,A"
            }"#,
        )
        .unwrap()
        .into_source_map()
        .unwrap();

        let raw = map.into_raw_sourcemap();
        let scopes = raw.scopes.map(|v| v.into_data().to_string());
        assert_eq!(scopes.as_deref(), Some("B,A,A,C,A,A"));
    }

    #[test]
    fn test_adjust_mappings_drops_scopes() {
        let mut map = decode(
            br#"{
                "version": 3,
                "sources": ["coolstuff.js"],
                "names": [],
                "mappings": "AAAA",
                "scopes": "B,A,A,C,A,A"
            }"#,
        )
        .unwrap()
        .into_source_map()
        .unwrap();

        let adjustment = crate::SourceMap::from_slice(
            br#"{
                "version": 3,
                "sources": ["coolstuff.js"],
                "names": [],
                "mappings": "AAAA"
            }"#,
        )
        .unwrap();

        map.adjust_mappings(adjustment);

        let raw = map.into_raw_sourcemap();
        assert!(raw.scopes.is_none());
    }
}