Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 53 additions & 21 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
use std::borrow::Cow;

use oxc_ast::{Comment, CommentKind};
use oxc_ast::{Comment, CommentKind, ast::Program};
use oxc_syntax::identifier::is_line_terminator;

use crate::{Codegen, LegalComment};
Expand All @@ -15,23 +16,14 @@ impl Codegen<'_> {
{
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() {
if self.options.print_legal_comment() {
add = true;
}
} else if comment.is_leading() {
Expand Down Expand Up @@ -142,8 +134,7 @@ impl Codegen<'_> {
}
CommentKind::Block => {
// Print block comments with our own indentation.
let lines = comment_source.split(is_line_terminator);
for line in lines {
for line in comment_source.split(is_line_terminator) {
if !line.starts_with("/*") {
self.print_indent();
}
Expand All @@ -156,25 +147,66 @@ impl Codegen<'_> {
}
}

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();
/// Handle Eof / Linked / External Comments.
/// Return a list of comments of linked or external.
pub(crate) fn handle_eof_linked_or_external_comments(
&mut self,
program: &Program<'_>,
) -> Vec<Comment> {
let legal_comments = &self.options.legal_comments;
if matches!(legal_comments, LegalComment::None | LegalComment::Inline) {
return vec![];
}

// Dedupe legal comments for smaller output size.
let mut set = FxHashSet::default();
let mut comments = vec![];

let source_text = program.source_text;
for comment in program.comments.iter().filter(|c| c.is_legal()) {
let mut text = Cow::Borrowed(comment.span.source_text(source_text));
if comment.is_block() && text.contains(is_line_terminator) {
let mut buffer = String::with_capacity(text.len());
// Print block comments with our own indentation.
for line in text.split(is_line_terminator) {
if !line.starts_with("/*") {
buffer.push('\t');
}
buffer.push_str(line.trim_start());
if !line.ends_with("*/") {
buffer.push('\n');
}
}
text = Cow::Owned(buffer);
}
if set.insert(text) {
comments.push(*comment);
}
}

if comments.is_empty() {
return vec![];
}

match legal_comments {
LegalComment::Eof => {
self.print_hard_newline();
for c in comments {
self.print_comment(&c);
self.print_hard_newline();
}
vec![]
}
LegalComment::Linked(path) => {
let path = path.clone();
self.print_hard_newline();
self.print_str("/*! For license information please see ");
self.print_str(&path);
self.print_str(" */");
comments
}
_ => {}
LegalComment::External => comments,
LegalComment::None | LegalComment::Inline => unreachable!(),
}
}
}
7 changes: 2 additions & 5 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ pub struct Codegen<'a> {
// Builders
comments: CommentsMap,

legal_comments: Vec<Comment>,

sourcemap_builder: Option<SourcemapBuilder>,
}

Expand Down Expand Up @@ -157,7 +155,6 @@ impl<'a> Codegen<'a> {
indent: 0,
quote: Quote::Double,
comments: CommentsMap::default(),
legal_comments: vec![],
sourcemap_builder: None,
}
}
Expand Down Expand Up @@ -199,10 +196,10 @@ impl<'a> Codegen<'a> {
self.sourcemap_builder = Some(SourcemapBuilder::new(path, program.source_text));
}
program.print(&mut self, Context::default());
self.try_print_eof_legal_comments();
let legal_comments = self.handle_eof_linked_or_external_comments(program);
let code = self.code.into_string();
let map = self.sourcemap_builder.map(SourcemapBuilder::into_sourcemap);
CodegenReturn { code, map, legal_comments: self.legal_comments }
CodegenReturn { code, map, legal_comments }
}

/// Turn what's been built so far into a string. Like [`build`],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ foo;bar;
foo;
bar;

/* @license */
/* @license */

########## 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ foo;bar;
----------
foo;bar;
/* @license */
/* @license */

########## 1
/* @license */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ function foo() {
/**
* @preserve
*/
/*! For license information please see test.js */
########## 8
/**
* @preserve
Expand All @@ -106,5 +105,3 @@ function foo() {
/**
* @preserve
*/

/*! For license information please see test.js */
Loading