-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcomment.rs
More file actions
181 lines (167 loc) · 5.97 KB
/
comment.rs
File metadata and controls
181 lines (167 loc) · 5.97 KB
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
use rustc_hash::FxHashMap;
use oxc_ast::{Comment, CommentKind};
use oxc_syntax::identifier::is_line_terminator;
use crate::{Codegen, LegalComment};
pub type CommentsMap = FxHashMap</* attached_to */ u32, Vec<Comment>>;
impl Codegen<'_> {
pub(crate) fn build_comments(&mut self, comments: &[Comment]) {
if !self.options.comments
&& self.options.legal_comments.is_none()
&& !self.options.annotation_comments
{
return;
}
let move_legal_comments = {
let legal_comments = &self.options.legal_comments;
matches!(
legal_comments,
LegalComment::Eof | LegalComment::Linked(_) | LegalComment::External
)
};
for comment in comments {
// Omit pure comments because they are handled separately.
if comment.is_pure() || comment.is_no_side_effects() {
continue;
}
let mut add = false;
if comment.is_legal() {
if move_legal_comments {
self.legal_comments.push(*comment);
} else if self.options.print_legal_comment() {
add = true;
}
} else if comment.is_leading() {
if comment.is_annotation() {
if self.options.print_annotation_comment() {
add = true;
}
} else if self.options.print_normal_comment() {
add = true;
}
}
if add {
self.comments.entry(comment.attached_to).or_default().push(*comment);
}
}
}
pub(crate) fn has_comment(&self, start: u32) -> bool {
self.comments.contains_key(&start)
}
pub(crate) fn print_leading_comments(&mut self, start: u32) {
if let Some(comments) = self.comments.remove(&start) {
self.print_comments(&comments);
}
}
pub(crate) fn get_statement_comments(&mut self, start: u32) -> Option<Vec<Comment>> {
if self.comments.is_empty() {
return None;
}
self.comments.remove(&start)
}
/// A statement comment also includes legal comments
#[inline]
pub(crate) fn print_statement_comments(&mut self, start: u32) {
if let Some(comments) = self.get_statement_comments(start) {
self.print_comments(&comments);
}
}
pub(crate) fn print_expr_comments(&mut self, start: u32) -> bool {
if self.comments.is_empty() {
return false;
}
let Some(comments) = self.comments.remove(&start) else { return false };
for comment in &comments {
self.print_hard_newline();
self.print_indent();
self.print_comment(comment);
}
if comments.is_empty() {
false
} else {
self.print_hard_newline();
true
}
}
pub(crate) fn print_comments(&mut self, comments: &[Comment]) {
for (i, comment) in comments.iter().enumerate() {
if i == 0 {
if comment.preceded_by_newline {
// Skip printing newline if this comment is already on a newline.
if let Some(b) = self.last_byte() {
match b {
b'\n' => self.print_indent(),
b'\t' => { /* noop */ }
_ => {
self.print_hard_newline();
self.print_indent();
}
}
}
} else {
self.print_indent();
}
}
if i >= 1 {
if comment.preceded_by_newline {
self.print_hard_newline();
self.print_indent();
} else if comment.is_legal() {
self.print_hard_newline();
}
}
self.print_comment(comment);
if i == comments.len() - 1 {
if comment.is_line() || comment.followed_by_newline {
self.print_hard_newline();
} else {
self.print_next_indent_as_space = true;
}
}
}
}
fn print_comment(&mut self, comment: &Comment) {
let Some(source_text) = self.source_text else {
return;
};
let comment_source = comment.span.source_text(source_text);
match comment.kind {
CommentKind::Line => {
self.print_str(comment_source);
}
CommentKind::Block => {
// Print block comments with our own indentation.
let lines = comment_source.split(is_line_terminator);
for line in lines {
if !line.starts_with("/*") {
self.print_indent();
}
self.print_str(line.trim_start());
if !line.ends_with("*/") {
self.print_hard_newline();
}
}
}
}
}
pub(crate) fn try_print_eof_legal_comments(&mut self) {
match self.options.legal_comments.clone() {
LegalComment::Eof => {
let comments = self.legal_comments.drain(..).collect::<Vec<_>>();
if !comments.is_empty() {
self.print_hard_newline();
}
for c in comments {
self.print_comment(&c);
self.print_hard_newline();
}
}
LegalComment::Linked(path) => {
self.print_hard_newline();
self.print_str("/*! For license information please see ");
self.print_str(&path);
self.print_str(" */");
}
_ => {}
}
}
}