swc_ecma_codegen/
comments.rs

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
use swc_common::comments::Comment;

use super::*;

macro_rules! write_comments {
    ($e:expr, $prefix_space:expr, $cmts:expr) => {{
        let cmts = match $cmts {
            Some(v) => v,
            None => return Ok(()),
        };

        for cmt in cmts.iter() {
            match cmt.kind {
                CommentKind::Line => {
                    if $prefix_space && !$e.cfg.minify {
                        $e.wr.write_comment(" ")?;
                    }

                    srcmap!($e, cmt, true);

                    $e.wr.write_comment("//")?;
                    $e.wr.write_comment(&cmt.text)?;

                    srcmap!($e, cmt, false);

                    $e.wr.write_line()?;
                }
                CommentKind::Block => {
                    if $prefix_space && !$e.cfg.minify {
                        $e.wr.write_comment(" ")?;
                    }

                    srcmap!($e, cmt, true);

                    $e.wr.write_comment("/*")?;
                    $e.wr.write_comment(&cmt.text)?;

                    {
                        let hi = cmt.span_hi();
                        if !hi.is_dummy() && hi.0 > 2 {
                            $e.wr.add_srcmap(hi - swc_common::BytePos(2))?;
                        }
                    }
                    $e.wr.write_comment("*/")?;

                    if !$e.cfg.minify {
                        $e.wr.write_space()?;
                    }
                }
            }
        }

        return Ok(());
    }};
}

impl<W, S: SourceMapper> Emitter<'_, W, S>
where
    W: WriteJs,
    S: SourceMapperExt,
{
    pub(super) fn emit_trailing_comments_of_pos(
        &mut self,
        pos: BytePos,
        prefix_space: bool,
        _is_hi: bool,
    ) -> Result {
        if pos.is_dummy() {
            return Ok(());
        }

        let comments = match self.comments {
            Some(ref comments) => comments,
            None => return Ok(()),
        };

        let cmts = comments.take_trailing(pos);

        write_comments!(self, prefix_space, &cmts)
    }

    pub(super) fn emit_leading_comments(&mut self, mut pos: BytePos, is_hi: bool) -> Result {
        if pos.is_dummy() {
            return Ok(());
        }

        if pos.is_pure() {
            write_comments!(
                self,
                false,
                Some(vec![Comment {
                    kind: CommentKind::Block,
                    span: DUMMY_SP,
                    text: "#__PURE__".into(),
                }])
            );
        }

        let comments = match self.comments {
            Some(ref comments) => comments,
            None => return Ok(()),
        };

        if is_hi {
            pos = pos - BytePos(1)
        }

        write_comments!(self, false, comments.take_leading(pos))
    }

    #[inline(always)]
    pub(super) fn emit_leading_comments_of_span(&mut self, span: Span, is_hi: bool) -> Result {
        let pos = if is_hi { span.hi } else { span.lo };
        self.emit_leading_comments(pos, is_hi)
    }
}