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

refactor(prettier): Align exported function names to Prettier #7827

Merged
merged 9 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Clean up print_block
leaysgur committed Dec 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 13384afc358254d887b1a8058d7f61164f3f1e5a
11 changes: 2 additions & 9 deletions crates/oxc_prettier/src/format/block.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ pub(super) fn print_block<'a>(
let mut parts = Vec::new_in(p.allocator);

parts.push(text!("{"));
if let Some(doc) = print_block_body(p, stmts, directives, true, false) {
if let Some(doc) = print_block_body(p, stmts, directives) {
parts.push({
let mut parts = Vec::new_in(p.allocator);
parts.extend(hardline!());
@@ -52,8 +52,6 @@ pub(super) fn print_block_body<'a>(
p: &mut Prettier<'a>,
stmts: &[Statement<'a>],
directives: Option<&[Directive<'a>]>,
remove_last_statement_hardline: bool,
is_root: bool,
) -> Option<Doc<'a>> {
let has_directives = directives.is_some_and(|directives| !directives.is_empty());
let has_body = stmts.iter().any(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
@@ -71,12 +69,7 @@ pub(super) fn print_block_body<'a>(
}

if has_body {
parts.extend(statement::print_statement_sequence(
p,
stmts,
remove_last_statement_hardline,
!is_root,
));
parts.extend(statement::print_statement_sequence(p, stmts));
}

Some(array!(p, parts))
13 changes: 5 additions & 8 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -57,18 +57,15 @@ impl<'a> Format<'a> for Program<'a> {
wrap!(p, self, Program, {
let mut parts = Vec::new_in(p.allocator);

// In Prettier, this is treated as a comment
if let Some(hashbang) = &self.hashbang {
parts.push(hashbang.format(p));
}

if let Some(doc) = block::print_block_body(
p,
&self.body,
Some(&self.directives),
false,
/* is_root */ true,
) {
parts.push(doc);
if let Some(body_doc) = block::print_block_body(p, &self.body, Some(&self.directives)) {
parts.push(body_doc);
// XXX: Prettier seems to add this, but test results don't match
// parts.extend(hardline!());
}

array!(p, parts)
3 changes: 1 addition & 2 deletions crates/oxc_prettier/src/format/statement.rs
Original file line number Diff line number Diff line change
@@ -7,8 +7,6 @@ use crate::{hardline, ir::Doc, Format, Prettier};
pub(super) fn print_statement_sequence<'a>(
p: &mut Prettier<'a>,
stmts: &[Statement<'a>],
remove_last_statement_hardline: bool,
skip_empty_statement: bool,
) -> Vec<'a, Doc<'a>> {
let mut parts = Vec::new_in(p.allocator);

@@ -24,6 +22,7 @@ pub(super) fn print_statement_sequence<'a>(

if Some(stmt.span()) != last_statement_span {
parts.extend(hardline!());

if p.is_next_line_empty(stmt.span()) {
parts.extend(hardline!());
}