refactor(prettier): move comment printing to its own directory (#1556)

This commit is contained in:
Boshen 2023-11-27 14:22:15 +08:00 committed by GitHub
parent cc382835ef
commit 6e7892fcf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 67 deletions

View file

@ -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
}
}

View file

@ -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]

View file

@ -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,
};

View file

@ -4,7 +4,7 @@
#![allow(clippy::wildcard_imports)]
mod comment;
mod comments;
mod doc;
mod format;
mod macros;

View file

@ -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