diff --git a/crates/oxc_prettier/src/comments/mod.rs b/crates/oxc_prettier/src/comments/mod.rs new file mode 100644 index 000000000..47358c1d4 --- /dev/null +++ b/crates/oxc_prettier/src/comments/mod.rs @@ -0,0 +1,55 @@ +mod print; + +use bitflags::bitflags; + +use oxc_ast::CommentKind; + +#[derive(Debug, Clone, Copy)] +pub struct Comment { + pub start: u32, + pub end: u32, + pub is_block: bool, + pub has_line_suffix: bool, +} + +impl Comment { + pub fn new(start: u32, end: u32, kind: CommentKind) -> Self { + // The comment span is for the comment value + // -2 for `//` and `/*` + let start = start - 2; + // +2 for `/*` + let end = if kind.is_multi_line() { end + 2 } else { end }; + Self { start, end, is_block: kind.is_multi_line(), has_line_suffix: false } + } + + pub fn with_line_suffix(mut self, yes: bool) -> Self { + self.has_line_suffix = yes; + self + } +} + +#[derive(Default)] +pub struct DanglingCommentsPrintOptions { + ident: bool, +} + +impl DanglingCommentsPrintOptions { + pub(crate) fn with_ident(mut self, ident: bool) -> Self { + self.ident = ident; + self + } +} + +bitflags! { + #[derive(Debug, Clone, Copy)] + pub struct CommentFlags: u8 { + const Leading = 1 << 0; // Check comment is a leading comment + const Trailing = 1 << 1; // Check comment is a trailing comment + const Dangling = 1 << 2; // Check comment is a dangling comment + const Block = 1 << 3; // Check comment is a block comment + const Line = 1 << 4; // Check comment is a line comment + const PrettierIgnore = 1 << 5; // Check comment is a `prettier-ignore` comment + const First = 1 << 6; // Check comment is the first attached comment + const Last = 1 << 7; // Check comment is the last attached comment + } +} diff --git a/crates/oxc_prettier/src/comment.rs b/crates/oxc_prettier/src/comments/print.rs similarity index 78% rename from crates/oxc_prettier/src/comment.rs rename to crates/oxc_prettier/src/comments/print.rs index 675b5d0e3..3b014b7f1 100644 --- a/crates/oxc_prettier/src/comment.rs +++ b/crates/oxc_prettier/src/comments/print.rs @@ -1,10 +1,4 @@ -//! Comment helpers - -#![allow(non_upper_case_globals)] - -use bitflags::bitflags; - -use oxc_ast::CommentKind; +use oxc_allocator::Vec; use oxc_span::Span; use crate::{ @@ -13,57 +7,7 @@ use crate::{ hardline, indent, line, ss, Prettier, }; -use oxc_allocator::Vec; - -bitflags! { - #[derive(Debug, Clone, Copy)] - pub struct CommentFlags: u8 { - const Leading = 1 << 0; // Check comment is a leading comment - const Trailing = 1 << 1; // Check comment is a trailing comment - const Dangling = 1 << 2; // Check comment is a dangling comment - const Block = 1 << 3; // Check comment is a block comment - const Line = 1 << 4; // Check comment is a line comment - const PrettierIgnore = 1 << 5; // Check comment is a `prettier-ignore` comment - const First = 1 << 6; // Check comment is the first attached comment - const Last = 1 << 7; // Check comment is the last attached comment - } -} - -#[derive(Default)] -pub struct DanglingCommentsPrintOptions { - ident: bool, -} - -impl DanglingCommentsPrintOptions { - pub(crate) fn with_ident(mut self, ident: bool) -> Self { - self.ident = ident; - self - } -} - -#[derive(Debug, Clone, Copy)] -struct Comment { - start: u32, - end: u32, - is_block: bool, - has_line_suffix: bool, -} - -impl Comment { - fn new(start: u32, end: u32, kind: CommentKind) -> Self { - // The comment span is for the comment value - // -2 for `//` and `/*` - let start = start - 2; - // +2 for `/*` - let end = if kind.is_multi_line() { end + 2 } else { end }; - Self { start, end, is_block: kind.is_multi_line(), has_line_suffix: false } - } - - fn with_line_suffix(mut self, yes: bool) -> Self { - self.has_line_suffix = yes; - self - } -} +use super::{Comment, CommentFlags, DanglingCommentsPrintOptions}; impl<'a> Prettier<'a> { #[must_use] diff --git a/crates/oxc_prettier/src/format/array.rs b/crates/oxc_prettier/src/format/array.rs index 8076a396d..854558825 100644 --- a/crates/oxc_prettier/src/format/array.rs +++ b/crates/oxc_prettier/src/format/array.rs @@ -4,7 +4,7 @@ use oxc_syntax::operator::UnaryOperator; use crate::{ array, - comment::DanglingCommentsPrintOptions, + comments::DanglingCommentsPrintOptions, doc::{Doc, DocBuilder, Fill, Group}, group, if_break, indent, softline, ss, Prettier, }; diff --git a/crates/oxc_prettier/src/lib.rs b/crates/oxc_prettier/src/lib.rs index 67cfff351..bba555c3f 100644 --- a/crates/oxc_prettier/src/lib.rs +++ b/crates/oxc_prettier/src/lib.rs @@ -4,7 +4,7 @@ #![allow(clippy::wildcard_imports)] -mod comment; +mod comments; mod doc; mod format; mod macros; diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index b2e311a7d..574d6067f 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -1,8 +1,4 @@ -<<<<<<< Updated upstream Compatibility: 171/591 (28.93%) -======= -Compatibility: 168/591 (28.43%) ->>>>>>> Stashed changes # Failed @@ -78,7 +74,6 @@ Compatibility: 168/591 (28.43%) * assignment-comments/call2.js * assignment-comments/function.js * assignment-comments/identifier.js -* assignment-comments/number.js * assignment-comments/string.js ### async @@ -226,7 +221,6 @@ Compatibility: 168/591 (28.43%) * comments-closure-typecast/comment-in-the-middle.js * comments-closure-typecast/comment-placement.js * comments-closure-typecast/extra-spaces-and-asterisks.js -* comments-closure-typecast/iife-issue-5850-isolated.js * comments-closure-typecast/iife.js * comments-closure-typecast/issue-4124.js * comments-closure-typecast/issue-8045.js @@ -471,7 +465,6 @@ Compatibility: 168/591 (28.43%) ### optional-chaining * optional-chaining/chaining.js * optional-chaining/comments.js -* optional-chaining/eval.js ### optional-chaining-assignment * optional-chaining-assignment/invalid-destructuring-arr.js