Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): print eof legal comments #7058

Merged
merged 1 commit into from
Nov 1, 2024
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
22 changes: 20 additions & 2 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ impl<'a> Codegen<'a> {
}

fn is_legal_comment(&self, comment: &Comment) -> bool {
(self.options.comments || self.options.legal_comments.is_inline())
&& comment.is_legal(self.source_text)
if self.options.comments {
if self.options.legal_comments.is_inline() || self.options.legal_comments.is_none() {
return comment.is_legal(self.source_text);
}
} else if self.options.legal_comments.is_inline() {
return comment.is_legal(self.source_text);
}
false
}

/// Weather to keep leading comments.
Expand Down Expand Up @@ -142,6 +148,18 @@ impl<'a> Codegen<'a> {
}
}

pub(crate) fn try_print_eof_legal_comments(&mut self, comments: &[Comment]) {
if !self.options.legal_comments.is_eof() {
return;
}
for c in comments {
if c.is_legal(self.source_text) {
self.print_comment(c);
self.print_hard_newline();
}
}
}

fn print_comments(&mut self, start: u32, comments: &[Comment], unused_comments: Vec<Comment>) {
if comments.first().is_some_and(|c| c.preceded_by_newline) {
// Skip printing newline if this comment is already on a newline.
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ impl<'a> Codegen<'a> {
self
}

/// Print a [`Program`] into a string of source code. A source map will be
/// generated if [`CodegenOptions::source_map_path`] is set.
/// Print a [`Program`] into a string of source code.
///
/// A source map will be generated if [`CodegenOptions::source_map_path`] is set.
#[must_use]
pub fn build(mut self, program: &Program<'a>) -> CodegenReturn {
self.quote = if self.options.single_quote { b'\'' } else { b'"' };
Expand All @@ -196,8 +197,8 @@ impl<'a> Codegen<'a> {
if let Some(path) = &self.options.source_map_path {
self.sourcemap_builder = Some(SourcemapBuilder::new(path, program.source_text));
}

program.print(&mut self, Context::default());
self.try_print_eof_legal_comments(&program.comments);
let code = self.code.into_string();
let map = self.sourcemap_builder.map(SourcemapBuilder::into_sourcemap);
CodegenReturn { code, map }
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_codegen/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ pub enum LegalComment {
}

impl LegalComment {
/// Is None.
pub fn is_none(self) -> bool {
self == Self::None
}

/// Is inline mode.
pub fn is_inline(self) -> bool {
self == Self::Inline
}

/// Is EOF mode.
pub fn is_eof(self) -> bool {
self == Self::Eof
}
}

/// Codegen Options.
Expand Down
10 changes: 9 additions & 1 deletion crates/oxc_codegen/tests/integration/legal_comments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::snapshot;
use oxc_codegen::{CodegenOptions, LegalComment};

use crate::{snapshot, snapshot_options};

fn cases() -> Vec<&'static str> {
vec![
Expand All @@ -13,3 +15,9 @@ fn cases() -> Vec<&'static str> {
fn legal_inline_comment() {
snapshot("legal_inline_comments", &cases());
}

#[test]
fn legal_eof_comment() {
let options = CodegenOptions { legal_comments: LegalComment::Eof, ..Default::default() };
snapshot_options("legal_eof_comments", &cases(), &options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
---
########## 0
/* @license */
/* @license */
foo;bar;
----------
foo;
bar;
/* @license */
/* @license */

########## 1
/* @license */
/* @preserve */
foo;bar;
----------
foo;
bar;
/* @license */
/* @preserve */

########## 2
/* @license */
//! KEEP
foo;bar;
----------
foo;
bar;
/* @license */
//! KEEP

########## 3
/* @license */
/*! KEEP */
foo;bar;
----------
foo;
bar;
/* @license */
/*! KEEP */
Loading