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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::rc::Rc;

use crate::swc::common::FileName;
use crate::swc::common::SourceFile;

use crate::ModuleSpecifier;

pub trait IntoSwcFileName {
  fn into_file_name(self) -> FileName;
}

impl IntoSwcFileName for ModuleSpecifier {
  fn into_file_name(self) -> FileName {
    FileName::Url(self)
  }
}

impl IntoSwcFileName for String {
  fn into_file_name(self) -> FileName {
    FileName::Custom(self)
  }
}

impl IntoSwcFileName for &str {
  fn into_file_name(self) -> FileName {
    FileName::Custom(self.to_owned())
  }
}

#[derive(Clone, Default)]
pub struct SourceMap {
  inner: Rc<crate::swc::common::SourceMap>,
}

impl SourceMap {
  pub fn single(file_name: impl IntoSwcFileName, source: String) -> Self {
    let map = Self::default();
    map
      .inner
      .new_source_file(file_name.into_file_name(), source);
    map
  }

  pub fn inner(&self) -> &Rc<crate::swc::common::SourceMap> {
    &self.inner
  }

  pub fn new_source_file(
    &self,
    file_name: impl IntoSwcFileName,
    source: String,
  ) -> Rc<SourceFile> {
    self
      .inner
      .new_source_file(file_name.into_file_name(), source)
  }
}