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
use swc_atoms::JsWord;
use swc_common::{
    comments::{CommentKind, Comments},
    BytePos, Span,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ImportRef {
    /// path="foo"
    Path(JsWord),
    /// type="foo"
    Types(JsWord),

    /// ES6 import.
    Normal(JsWord),
}

impl ImportRef {
    pub fn to_path(self) -> JsWord {
        match self {
            ImportRef::Path(s) => format!("./{}", s).into(),
            ImportRef::Types(s) => s,
            ImportRef::Normal(s) => s,
        }
    }
}

pub fn find_imports_in_comments<C>(comments: C, span: Span) -> Vec<ImportRef>
where
    C: Comments,
{
    debug_assert_ne!(
        span.lo,
        BytePos(0),
        "Found {:?}, BytePos(0) should not have comments because it's used by DUMMY_SP",
        span
    );

    let mut deps = vec![];

    comments.with_leading(span.lo, |comments| {
        for c in comments {
            if c.kind != CommentKind::Line {
                continue;
            }
            if let Some(cmt_text) = c
                .text
                .trim()
                .strip_prefix('/')
                .map(|s| s.trim())
                .and_then(|s| s.strip_prefix("<reference"))
                .and_then(|s| s.strip_suffix("\" />"))
                .map(|s| s.trim())
            {
                if let Some(path) = cmt_text.strip_prefix("path=\"") {
                    deps.push(ImportRef::Path(path.into()));
                } else if let Some(path) = cmt_text.strip_prefix("types=\"") {
                    deps.push(ImportRef::Types(path.into()));
                } else {
                    // TODO: Handle lib, types
                }
            }
        }
    });

    deps
}