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
46 changes: 29 additions & 17 deletions crates/oxc_ast/src/ast/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum CommentPosition {
#[ast]
#[generate_derive(CloneIn, ContentEq)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub enum CommentAnnotation {
pub enum CommentContent {
/// No Annotation
#[default]
None = 0,
Expand All @@ -60,27 +60,31 @@ pub enum CommentAnnotation {
/// <https://jsdoc.app>
Jsdoc = 2,

/// A jsdoc containing legal annotation.
/// `/** @preserve */`
JsdocLegal = 3,

/// `/* #__PURE__ */`
/// <https://github.com/javascript-compiler-hints/compiler-notations-spec>
Pure = 3,
Pure = 4,

/// `/* #__NO_SIDE_EFFECTS__ */`
NoSideEffects = 4,
NoSideEffects = 5,

/// Webpack magic comment
/// e.g. `/* webpackChunkName */`
/// <https://webpack.js.org/api/module-methods/#magic-comments>
Webpack = 5,
Webpack = 6,

/// Vite comment
/// e.g. `/* @vite-ignore */`
/// <https://github.com/search?q=repo%3Avitejs%2Fvite%20vite-ignore&type=code>
Vite = 6,
Vite = 7,

/// Code Coverage Ignore
/// `v8 ignore`, `c8 ignore`, `node:coverage`, `istanbul ignore`
/// <https://github.com/oxc-project/oxc/issues/10091>
CoverageIgnore = 7,
CoverageIgnore = 8,
}

bitflags! {
Expand Down Expand Up @@ -144,9 +148,9 @@ pub struct Comment {
#[estree(skip)]
pub newlines: CommentNewlines,

/// Comment Annotation
/// Content of the comment
#[estree(skip)]
pub annotation: CommentAnnotation,
pub content: CommentContent,
}

impl Comment {
Expand All @@ -160,7 +164,7 @@ impl Comment {
kind,
position: CommentPosition::Trailing,
newlines: CommentNewlines::None,
annotation: CommentAnnotation::None,
content: CommentContent::None,
}
}

Expand Down Expand Up @@ -196,16 +200,23 @@ impl Comment {
self.position == CommentPosition::Trailing
}

/// Is comment without a special meaning.
#[inline]
pub fn is_normal(self) -> bool {
self.content == CommentContent::None
}

/// Is comment with special meaning.
#[inline]
pub fn is_annotation(self) -> bool {
self.annotation != CommentAnnotation::None
self.content != CommentContent::None && self.content != CommentContent::Legal
}

/// Returns `true` if this comment is a JSDoc comment. Implies `is_leading` and `is_block`.
#[inline]
pub fn is_jsdoc(self) -> bool {
self.annotation == CommentAnnotation::Jsdoc && self.is_leading()
matches!(self.content, CommentContent::Jsdoc | CommentContent::JsdocLegal)
&& self.is_leading()
}

/// Legal comments
Expand All @@ -216,37 +227,38 @@ impl Comment {
/// <https://esbuild.github.io/api/#legal-comments>
#[inline]
pub fn is_legal(self) -> bool {
self.annotation == CommentAnnotation::Legal && self.is_leading()
matches!(self.content, CommentContent::Legal | CommentContent::JsdocLegal)
&& self.is_leading()
}

/// Is `/* @__PURE__*/`.
#[inline]
pub fn is_pure(self) -> bool {
self.annotation == CommentAnnotation::Pure
self.content == CommentContent::Pure
}

/// Is `/* @__NO_SIDE_EFFECTS__*/`.
#[inline]
pub fn is_no_side_effects(self) -> bool {
self.annotation == CommentAnnotation::NoSideEffects
self.content == CommentContent::NoSideEffects
}

/// Is webpack magic comment.
#[inline]
pub fn is_webpack(self) -> bool {
self.annotation == CommentAnnotation::Webpack
self.content == CommentContent::Webpack
}

/// Is vite special comment.
#[inline]
pub fn is_vite(self) -> bool {
self.annotation == CommentAnnotation::Vite
self.content == CommentContent::Vite
}

/// Is coverage ignore comment.
#[inline]
pub fn is_coverage_ignore(self) -> bool {
self.annotation == CommentAnnotation::CoverageIgnore && self.is_leading()
self.content == CommentContent::CoverageIgnore && self.is_leading()
}

/// Returns `true` if this comment is preceded by a newline.
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,8 @@ const _: () = {
assert!(size_of::<CommentPosition>() == 1);
assert!(align_of::<CommentPosition>() == 1);

assert!(size_of::<CommentAnnotation>() == 1);
assert!(align_of::<CommentAnnotation>() == 1);
assert!(size_of::<CommentContent>() == 1);
assert!(align_of::<CommentContent>() == 1);

// Padding: 0 bytes
assert!(size_of::<CommentNewlines>() == 1);
Expand All @@ -1593,7 +1593,7 @@ const _: () = {
assert!(offset_of!(Comment, kind) == 12);
assert!(offset_of!(Comment, position) == 13);
assert!(offset_of!(Comment, newlines) == 14);
assert!(offset_of!(Comment, annotation) == 15);
assert!(offset_of!(Comment, content) == 15);
};

#[cfg(target_pointer_width = "32")]
Expand Down Expand Up @@ -3167,8 +3167,8 @@ const _: () = {
assert!(size_of::<CommentPosition>() == 1);
assert!(align_of::<CommentPosition>() == 1);

assert!(size_of::<CommentAnnotation>() == 1);
assert!(align_of::<CommentAnnotation>() == 1);
assert!(size_of::<CommentContent>() == 1);
assert!(align_of::<CommentContent>() == 1);

// Padding: 0 bytes
assert!(size_of::<CommentNewlines>() == 1);
Expand All @@ -3182,7 +3182,7 @@ const _: () = {
assert!(offset_of!(Comment, kind) == 12);
assert!(offset_of!(Comment, position) == 13);
assert!(offset_of!(Comment, newlines) == 14);
assert!(offset_of!(Comment, annotation) == 15);
assert!(offset_of!(Comment, content) == 15);
};

#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7852,8 +7852,8 @@ impl<'new_alloc> CloneIn<'new_alloc> for CommentPosition {
}
}

impl<'new_alloc> CloneIn<'new_alloc> for CommentAnnotation {
type Cloned = CommentAnnotation;
impl<'new_alloc> CloneIn<'new_alloc> for CommentContent {
type Cloned = CommentContent;

#[inline(always)]
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Expand All @@ -7876,7 +7876,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for Comment {
kind: CloneIn::clone_in(&self.kind, allocator),
position: CloneIn::clone_in(&self.position, allocator),
newlines: CloneIn::clone_in(&self.newlines, allocator),
annotation: CloneIn::clone_in(&self.annotation, allocator),
content: CloneIn::clone_in(&self.content, allocator),
}
}

Expand All @@ -7887,7 +7887,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for Comment {
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
position: CloneIn::clone_in_with_semantic_ids(&self.position, allocator),
newlines: CloneIn::clone_in_with_semantic_ids(&self.newlines, allocator),
annotation: CloneIn::clone_in_with_semantic_ids(&self.annotation, allocator),
content: CloneIn::clone_in_with_semantic_ids(&self.content, allocator),
}
}
}
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ impl ContentEq for CommentPosition {
}
}

impl ContentEq for CommentAnnotation {
impl ContentEq for CommentContent {
fn content_eq(&self, other: &Self) -> bool {
self == other
}
Expand All @@ -2488,6 +2488,6 @@ impl ContentEq for Comment {
&& ContentEq::content_eq(&self.kind, &other.kind)
&& ContentEq::content_eq(&self.position, &other.position)
&& ContentEq::content_eq(&self.newlines, &other.newlines)
&& ContentEq::content_eq(&self.annotation, &other.annotation)
&& ContentEq::content_eq(&self.content, &other.content)
}
}
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod generated {
pub use generated::{ast_builder, ast_kind};

pub use crate::{
ast::comment::{Comment, CommentAnnotation, CommentKind, CommentPosition},
ast::comment::{Comment, CommentContent, CommentKind, CommentPosition},
ast_builder::AstBuilder,
ast_builder_impl::NONE,
ast_kind::{AstKind, AstType},
Expand Down
14 changes: 6 additions & 8 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ impl Codegen<'_> {
continue;
}
let mut add = false;
if comment.is_legal() {
if self.options.print_legal_comment() {
if comment.is_leading() {
if comment.is_legal() && 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() {
if comment.is_annotation() && self.options.print_annotation_comment() {
add = true;
}
if comment.is_normal() && self.options.print_normal_comment() {
add = true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,9 @@ impl Gen for FunctionBody<'_> {
let span_end = self.span.end;
let comments_at_end = if span_end > 0 { p.get_comments(span_end - 1) } else { None };
let single_line = if self.is_empty() {
comments_at_end.as_ref().is_none_or(|comments| comments.iter().all(|c| c.is_block()))
comments_at_end.as_ref().is_none_or(|comments| {
comments.iter().all(|c| !c.preceded_by_newline() && !c.followed_by_newline())
})
} else {
false
};
Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_codegen/tests/integration/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ pub mod legal {

#[test]
fn legal_eof_comment() {
let options = CodegenOptions { legal_comments: LegalComment::Eof, ..Default::default() };
let options =
CodegenOptions { legal_comments: LegalComment::Eof, ..CodegenOptions::default() };
snapshot_options("legal_eof_comments", &cases(), &options);
}

Expand All @@ -220,7 +221,7 @@ pub mod legal {
let options = CodegenOptions {
minify: true,
legal_comments: LegalComment::Eof,
..Default::default()
..CodegenOptions::default()
};
snapshot_options("legal_eof_minify_comments", &cases(), &options);
}
Expand All @@ -229,15 +230,15 @@ pub mod legal {
fn legal_linked_comment() {
let options = CodegenOptions {
legal_comments: LegalComment::Linked(String::from("test.js")),
..Default::default()
..CodegenOptions::default()
};
snapshot_options("legal_linked_comments", &cases(), &options);
}

#[test]
fn legal_external_comment() {
let options =
CodegenOptions { legal_comments: LegalComment::External, ..Default::default() };
CodegenOptions { legal_comments: LegalComment::External, ..CodegenOptions::default() };
let code = "/* @license */\n/* @preserve */\nfoo;\n";
let ret = codegen_options(code, &options);
assert_eq!(ret.code, "foo;\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ function foo() {
----------
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
/**
* @preserve
*/

########## 8
/**
* @preserve
Expand All @@ -119,3 +123,7 @@ function foo() {
/**
* @preserve
*/

/**
* @preserve
*/
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function foo(){(()=>{
/**
* @preserve
*/
/**
* @preserve
*/

########## 8
/**
* @preserve
Expand All @@ -101,3 +105,7 @@ function foo(){(()=>{
/**
* @preserve
*/

/**
* @preserve
*/
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ function foo() {
----------
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
})();
/**
* @preserve
*/
Expand Down
Loading
Loading