Skip to content

Commit 558810d

Browse files
committed
refactor(codegen)!: remove CommentOptions API
1 parent 06ea121 commit 558810d

File tree

14 files changed

+99
-147
lines changed

14 files changed

+99
-147
lines changed

crates/oxc/src/compiler.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{mem, ops::ControlFlow, path::Path};
22

33
use oxc_allocator::Allocator;
44
use oxc_ast::ast::Program;
5-
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn, CommentOptions};
5+
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn};
66
use oxc_diagnostics::OxcDiagnostic;
77
use oxc_isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions};
88
use oxc_mangler::{MangleOptions, Mangler};
@@ -283,11 +283,7 @@ pub trait CompilerInterface {
283283
mangler: Option<Mangler>,
284284
options: CodegenOptions,
285285
) -> CodegenReturn {
286-
let comment_options = CommentOptions { preserve_annotate_comments: true };
287-
let mut codegen = CodeGenerator::new()
288-
.with_options(options)
289-
.with_mangler(mangler)
290-
.enable_comment(program, comment_options);
286+
let mut codegen = CodeGenerator::new().with_options(options).with_mangler(mangler);
291287
if self.enable_sourcemap() {
292288
codegen = codegen
293289
.enable_source_map(source_path.to_string_lossy().as_ref(), program.source_text);

crates/oxc_codegen/examples/codegen.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::{env, path::Path};
33

44
use oxc_allocator::Allocator;
5-
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
5+
use oxc_codegen::{CodeGenerator, CodegenOptions};
66
use oxc_parser::{Parser, ParserReturn};
77
use oxc_span::SourceType;
88
use pico_args::Arguments;
@@ -61,7 +61,6 @@ fn parse<'a>(
6161

6262
fn codegen(ret: &ParserReturn<'_>, minify: bool) -> String {
6363
CodeGenerator::new()
64-
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: true })
6564
.with_options(CodegenOptions { minify, ..CodegenOptions::default() })
6665
.build(&ret.program)
6766
.code

crates/oxc_codegen/src/comment.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ static ANNOTATION_MATCHER: Lazy<DoubleArrayAhoCorasick<usize>> = Lazy::new(|| {
1616
pub(crate) type CommentsMap = FxHashMap</* attached_to */ u32, Vec<Comment>>;
1717

1818
impl<'a> Codegen<'a> {
19-
pub(crate) fn preserve_annotate_comments(&self) -> bool {
20-
self.comment_options.preserve_annotate_comments && !self.options.minify
21-
}
22-
2319
pub(crate) fn build_comments(&mut self, comments: &[Comment]) {
2420
for comment in comments {
2521
self.comments.entry(comment.attached_to).or_default().push(*comment);
@@ -31,35 +27,33 @@ impl<'a> Codegen<'a> {
3127
}
3228

3329
pub(crate) fn has_annotation_comment(&self, start: u32) -> bool {
34-
if !self.preserve_annotate_comments() {
30+
if !self.options.print_annotation_comments() {
3531
return false;
3632
}
37-
let Some(source_text) = self.source_text else { return false };
3833
self.comments.get(&start).is_some_and(|comments| {
39-
comments.iter().any(|comment| Self::is_annotation_comment(comment, source_text))
34+
comments.iter().any(|comment| self.is_annotation_comment(comment))
4035
})
4136
}
4237

4338
pub(crate) fn has_non_annotation_comment(&self, start: u32) -> bool {
44-
if !self.preserve_annotate_comments() {
39+
if !self.options.print_annotation_comments() {
4540
return self.has_comment(start);
4641
}
47-
let Some(source_text) = self.source_text else { return false };
4842
self.comments.get(&start).is_some_and(|comments| {
49-
comments.iter().any(|comment| !Self::is_annotation_comment(comment, source_text))
43+
comments.iter().any(|comment| !self.is_annotation_comment(comment))
5044
})
5145
}
5246

5347
/// Weather to keep leading comments.
54-
fn is_leading_comments(comment: &Comment, source_text: &str) -> bool {
55-
(comment.is_jsdoc(source_text) || (comment.is_line() && Self::is_annotation_comment(comment, source_text)))
48+
fn is_leading_comments(&self, comment: &Comment) -> bool {
49+
(comment.is_jsdoc(self.source_text) || (comment.is_line() && self.is_annotation_comment(comment)))
5650
&& comment.preceded_by_newline
5751
// webpack comment `/*****/`
58-
&& !comment.span.source_text(source_text).chars().all(|c| c == '*')
52+
&& !comment.span.source_text(self.source_text).chars().all(|c| c == '*')
5953
}
6054

61-
fn print_comment(&mut self, comment: &Comment, source_text: &str) {
62-
let comment_source = comment.real_span().source_text(source_text);
55+
fn print_comment(&mut self, comment: &Comment) {
56+
let comment_source = comment.real_span().source_text(self.source_text);
6357
match comment.kind {
6458
CommentKind::Line => {
6559
self.print_str(comment_source);
@@ -84,14 +78,12 @@ impl<'a> Codegen<'a> {
8478
if self.options.minify {
8579
return;
8680
}
87-
let Some(source_text) = self.source_text else { return };
8881
let Some(comments) = self.comments.remove(&start) else {
8982
return;
9083
};
9184

92-
let (comments, unused_comments): (Vec<_>, Vec<_>) = comments
93-
.into_iter()
94-
.partition(|comment| Self::is_leading_comments(comment, source_text));
85+
let (comments, unused_comments): (Vec<_>, Vec<_>) =
86+
comments.into_iter().partition(|comment| self.is_leading_comments(comment));
9587

9688
if comments.first().is_some_and(|c| c.preceded_by_newline) {
9789
// Skip printing newline if this comment is already on a newline.
@@ -107,7 +99,7 @@ impl<'a> Codegen<'a> {
10799
self.print_indent();
108100
}
109101

110-
self.print_comment(comment, source_text);
102+
self.print_comment(comment);
111103
}
112104

113105
if comments.last().is_some_and(|c| c.is_line() || c.followed_by_newline) {
@@ -120,32 +112,31 @@ impl<'a> Codegen<'a> {
120112
}
121113
}
122114

123-
fn is_annotation_comment(comment: &Comment, source_text: &str) -> bool {
124-
let comment_content = comment.span.source_text(source_text);
115+
fn is_annotation_comment(&self, comment: &Comment) -> bool {
116+
let comment_content = comment.span.source_text(self.source_text);
125117
ANNOTATION_MATCHER.find_iter(comment_content).count() != 0
126118
}
127119

128120
pub(crate) fn print_annotation_comments(&mut self, node_start: u32) {
129-
if !self.preserve_annotate_comments() {
121+
if !self.options.print_annotation_comments() {
130122
return;
131123
}
132124

133125
// If there is has annotation comments awaiting move to here, print them.
134126
let start = self.start_of_annotation_comment.take().unwrap_or(node_start);
135127

136-
let Some(source_text) = self.source_text else { return };
137128
let Some(comments) = self.comments.remove(&start) else { return };
138129

139130
for comment in comments {
140-
if !Self::is_annotation_comment(&comment, source_text) {
131+
if !self.is_annotation_comment(&comment) {
141132
continue;
142133
}
143134
if comment.is_line() {
144135
self.print_str("/*");
145-
self.print_str(comment.span.source_text(source_text));
136+
self.print_str(comment.span.source_text(self.source_text));
146137
self.print_str("*/");
147138
} else {
148-
self.print_str(comment.real_span().source_text(source_text));
139+
self.print_str(comment.real_span().source_text(self.source_text));
149140
}
150141
self.print_hard_space();
151142
}
@@ -155,12 +146,10 @@ impl<'a> Codegen<'a> {
155146
if self.options.minify {
156147
return false;
157148
}
158-
let Some(source_text) = self.source_text else { return false };
159149
let Some(comments) = self.comments.remove(&start) else { return false };
160150

161-
let (annotation_comments, comments): (Vec<_>, Vec<_>) = comments
162-
.into_iter()
163-
.partition(|comment| Self::is_annotation_comment(comment, source_text));
151+
let (annotation_comments, comments): (Vec<_>, Vec<_>) =
152+
comments.into_iter().partition(|comment| self.is_annotation_comment(comment));
164153

165154
if !annotation_comments.is_empty() {
166155
self.comments.insert(start, annotation_comments);
@@ -169,7 +158,7 @@ impl<'a> Codegen<'a> {
169158
for comment in &comments {
170159
self.print_hard_newline();
171160
self.print_indent();
172-
self.print_comment(comment, source_text);
161+
self.print_comment(comment);
173162
}
174163

175164
if comments.is_empty() {

crates/oxc_codegen/src/gen.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, ops::Not};
1+
use std::ops::Not;
22

33
use cow_utils::CowUtils;
44
#[allow(clippy::wildcard_imports)]
@@ -558,7 +558,7 @@ impl<'a> Gen for VariableDeclaration<'a> {
558558
p.print_str("declare ");
559559
}
560560

561-
if p.preserve_annotate_comments()
561+
if p.options.print_annotation_comments()
562562
&& p.start_of_annotation_comment.is_none()
563563
&& matches!(self.kind, VariableDeclarationKind::Const)
564564
&& matches!(self.declarations.first(), Some(VariableDeclarator { init: Some(init), .. }) if init.is_function())
@@ -825,7 +825,7 @@ impl<'a> Gen for ExportNamedDeclaration<'a> {
825825
p.add_source_mapping(self.span.start);
826826
p.print_indent();
827827

828-
if p.preserve_annotate_comments() {
828+
if p.options.print_annotation_comments() {
829829
match &self.declaration {
830830
Some(Declaration::FunctionDeclaration(_)) => {
831831
p.print_annotation_comments(self.span.start);
@@ -1195,10 +1195,7 @@ impl<'a> Gen for RegExpLiteral<'a> {
11951195
fn gen(&self, p: &mut Codegen, _ctx: Context) {
11961196
p.add_source_mapping(self.span.start);
11971197
let last = p.peek_nth(0);
1198-
let pattern_text = p.source_text.map_or_else(
1199-
|| Cow::Owned(self.regex.pattern.to_string()),
1200-
|src| self.regex.pattern.source_text(src),
1201-
);
1198+
let pattern_text = self.regex.pattern.source_text(p.source_text);
12021199
// Avoid forming a single-line comment or "</script" sequence
12031200
if Some('/') == last
12041201
|| (Some('<') == last && pattern_text.cow_to_lowercase().starts_with("script"))

0 commit comments

Comments
 (0)