Skip to content

Commit

Permalink
refactor(prettier): Make hardline! to return Doc (#8379)
Browse files Browse the repository at this point in the history
For cleaner Doc construction.
  • Loading branch information
leaysgur authored Jan 9, 2025
1 parent 0903501 commit c1c0d71
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 84 deletions.
31 changes: 11 additions & 20 deletions crates/oxc_prettier/src/format/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a> Format<'a> for Program<'a> {

if let Some(body_doc) = block::print_block_body(p, &self.body, Some(&self.directives)) {
parts.push(body_doc);
parts.extend(hardline!());
parts.push(hardline!(p));
}

array!(p, parts)
Expand All @@ -46,10 +46,10 @@ impl<'a> Format<'a> for Hashbang<'a> {
let mut parts = Vec::new_in(p.allocator);

parts.push(dynamic_text!(p, self.span.source_text(p.source_text)));
parts.extend(hardline!());
parts.push(hardline!(p));
// Preserve original newline
if p.is_next_line_empty(self.span) {
parts.extend(hardline!());
parts.push(hardline!(p));
}

array!(p, parts)
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> Format<'a> for IfStatement<'a> {
if else_on_same_line {
parts.push(text!(" "));
} else {
parts.extend(hardline!());
parts.push(hardline!(p));
}
parts.push(text!("else"));
let alternate_doc = alternate.format(p);
Expand Down Expand Up @@ -296,7 +296,7 @@ impl<'a> Format<'a> for DoWhileStatement<'a> {
if matches!(self.body, Statement::BlockStatement(_)) {
parts.push(text!(" "));
} else {
parts.extend(hardline!());
parts.push(hardline!(p));
}

parts.push(text!("while ("));
Expand Down Expand Up @@ -370,19 +370,14 @@ impl<'a> Format<'a> for SwitchStatement<'a> {
let mut cases_parts = Vec::new_in(p.allocator);
let len = self.cases.len();
for (i, case) in self.cases.iter().enumerate() {
cases_parts.push({
let mut parts = Vec::new_in(p.allocator);
parts.extend(hardline!());
parts.push(case.format(p));
indent!(p, parts)
});
cases_parts.push(indent!(p, [hardline!(p), case.format(p)]));
if i != len - 1 && p.is_next_line_empty(case.span) {
cases_parts.extend(hardline!());
cases_parts.push(hardline!(p));
}
}
parts.extend(cases_parts);

parts.extend(hardline!());
parts.push(hardline!(p));
parts.push(text!("}"));

array!(p, parts)
Expand Down Expand Up @@ -417,14 +412,14 @@ impl<'a> Format<'a> for SwitchCase<'a> {
if i != 0 && matches!(stmt, Statement::BreakStatement(_)) {
let last_stmt = &consequent[i - 1];
if p.is_next_line_empty(last_stmt.span()) {
consequent_parts.extend(hardline!());
consequent_parts.push(hardline!(p));
}
}

if is_only_one_block_statement {
consequent_parts.push(text!(" "));
} else {
consequent_parts.extend(hardline!());
consequent_parts.push(hardline!(p));
}
consequent_parts.push(stmt.format(p));
}
Expand Down Expand Up @@ -601,11 +596,7 @@ impl<'a> Format<'a> for VariableDeclaration<'a> {
let mut d_parts = Vec::new_in(p.allocator);
if i != 0 {
d_parts.push(text!(","));
if is_hardline {
d_parts.extend(hardline!());
} else {
d_parts.push(line!());
}
d_parts.push(if is_hardline { hardline!(p) } else { line!() });
}
d_parts.push(decl.format(p));
indent!(p, d_parts)
Expand Down
9 changes: 2 additions & 7 deletions crates/oxc_prettier/src/format/print/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,11 @@ where

if !is_last {
if is_line_after_element_empty(p, element.span().end) {
let mut space_parts = Vec::new_in(p.allocator);
space_parts.extend(hardline!());
space_parts.extend(hardline!());
parts.push(array!(p, space_parts));
parts.push(array!(p, [hardline!(p), hardline!(p)]));
} else if arr.elements.get(i + 1).is_some_and(|next| {
p.has_comment(next.span(), CommentFlags::Leading | CommentFlags::Line)
}) {
let mut space_parts = Vec::new_in(p.allocator);
space_parts.extend(hardline!());
parts.push(array!(p, space_parts));
parts.push(array!(p, [hardline!(p)]));
} else {
parts.push(line!());
}
Expand Down
20 changes: 8 additions & 12 deletions crates/oxc_prettier/src/format/print/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ pub fn print_block<'a>(

parts.push(text!("{"));
if let Some(doc) = print_block_body(p, stmts, directives) {
parts.push({
let mut parts = Vec::new_in(p.allocator);
parts.extend(hardline!());
parts.push(doc);
indent!(p, parts)
});
parts.extend(hardline!());
parts.push(indent!(p, [hardline!(p), doc]));
parts.push(hardline!(p));
} else {
let parent = p.parent_kind();
let parent_parent = p.parent_parent_kind();
Expand All @@ -40,7 +35,7 @@ pub fn print_block<'a>(
&& !matches!(p.parent_parent_kind(), Some(AstKind::TryStatement(stmt)) if stmt.finalizer.is_some()))
|| matches!(p.current_kind(), AstKind::StaticBlock(_)))
{
parts.extend(hardline!());
parts.push(hardline!(p));
}
}
parts.push(text!("}"));
Expand All @@ -65,23 +60,24 @@ pub fn print_block_body<'a>(

if has_directives {
if let Some(directives) = directives {
// `statement::print_statement_sequence()` equivalent for directives
let mut last_directive = &directives[0];
for (idx, directive) in directives.iter().enumerate() {
parts.push(directive.format(p));
if idx != directives.len() - 1 {
parts.extend(hardline!());
parts.push(hardline!(p));
if p.is_next_line_empty(directive.span) {
parts.extend(hardline!());
parts.push(hardline!(p));
}
}

last_directive = directive;
}

if has_body {
parts.extend(hardline!());
parts.push(hardline!(p));
if p.is_next_line_empty(last_directive.span) {
parts.extend(hardline!());
parts.push(hardline!(p));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_prettier/src/format/print/call_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub fn print_call_arguments<'a>(
if i < len - 1 {
arg.push(text!(","));
if p.is_next_line_empty(element.span()) {
arg.extend(hardline!());
arg.extend(hardline!());
arg.push(hardline!(p));
arg.push(hardline!(p));
} else {
arg.push(line!());
}
Expand Down
24 changes: 9 additions & 15 deletions crates/oxc_prettier/src/format/print/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a> {
for decorator in &class.decorators {
parts.push(text!("@"));
parts.push(decorator.expression.format(p));
parts.extend(hardline!());
parts.push(hardline!(p));
}

if class.declare {
Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a> {
parts.push(printend_parts_group);

if !class.body.body.is_empty() && has_multiple_heritage(class) {
parts.extend(hardline!());
parts.push(hardline!(p));
}
} else {
parts.push(array!(p, [array!(p, group_parts), array!(p, heritage_clauses_parts)]));
Expand All @@ -106,10 +106,10 @@ pub fn print_class_body<'a>(p: &mut Prettier<'a>, class_body: &ClassBody<'a>) ->
}

if i < class_body.body.len() - 1 {
parts_inner.extend(hardline!());
parts_inner.push(hardline!(p));

if p.is_next_line_empty(node.span()) {
parts_inner.extend(hardline!());
parts_inner.push(hardline!(p));
}
}
}
Expand All @@ -119,14 +119,8 @@ pub fn print_class_body<'a>(p: &mut Prettier<'a>, class_body: &ClassBody<'a>) ->
let mut parts = Vec::new_in(p.allocator);
parts.push(text!("{"));
if !parts_inner.is_empty() {
let indent = {
let mut parts = Vec::new_in(p.allocator);
parts.extend(hardline!());
parts.push(array!(p, parts_inner));
indent!(p, parts)
};
parts.push(array!(p, [indent]));
parts.extend(hardline!());
parts.push(array!(p, [indent!(p, [hardline!(p), array!(p, parts_inner)])]));
parts.push(hardline!(p));
}

parts.push(text!("}"));
Expand Down Expand Up @@ -250,7 +244,7 @@ pub fn print_class_property<'a>(p: &mut Prettier<'a>, node: &ClassMemberish<'a,
for decorator in decarators {
parts.push(text!("@"));
parts.push(decorator.expression.format(p));
parts.extend(hardline!());
parts.push(hardline!(p));
}
}

Expand Down Expand Up @@ -387,7 +381,7 @@ fn print_heritage_clauses_implements<'a>(p: &mut Prettier<'a>, class: &Class<'a>
if should_indent_only_heritage_clauses(class) {
parts.push(if_break!(p, line!()));
} else if class.super_class.is_some() {
parts.extend(hardline!());
parts.push(hardline!(p));
} else {
parts.push(softline!());
}
Expand All @@ -398,7 +392,7 @@ fn print_heritage_clauses_implements<'a>(p: &mut Prettier<'a>, class: &Class<'a>

parts.push(indent!(
p,
[group!(p, [softline!(), join!(p, JoinSeparator::CommaLine, implements_docs),])]
[group!(p, [softline!(), join!(p, JoinSeparator::CommaLine, implements_docs)])]
));
parts.push(text!(" "));

Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_prettier/src/format/print/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub fn print_function_parameters<'a>(
if should_hug_the_only_function_parameter {
printed.push(text!(" "));
} else if p.is_next_line_empty(this_param.span) {
printed.extend(hardline!());
printed.extend(hardline!());
printed.push(hardline!(p));
printed.push(hardline!(p));
} else {
printed.push(line!());
}
Expand Down Expand Up @@ -116,8 +116,8 @@ pub fn print_function_parameters<'a>(
if should_hug_the_only_function_parameter {
printed.push(text!(" "));
} else if p.is_next_line_empty(param.span) {
printed.extend(hardline!());
printed.extend(hardline!());
printed.push(hardline!(p));
printed.push(hardline!(p));
} else {
printed.push(line!());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_prettier/src/format/print/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub fn print_statement_sequence<'a>(
parts.push(stmt.format(p));

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

if p.is_next_line_empty(stmt.span()) {
parts.extend(hardline!());
parts.push(hardline!(p));
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions crates/oxc_prettier/src/format/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,15 @@ impl<'a> Format<'a> for TSInterfaceDeclaration<'a> {
if self.body.body.len() > 0 {
let mut indent_parts = Vec::new_in(p.allocator);
for sig in &self.body.body {
indent_parts.extend(hardline!());
indent_parts.push(hardline!(p));
indent_parts.push(sig.format(p));

if let Some(semi) = p.semi() {
indent_parts.push(semi);
}
}
parts.push(indent!(p, indent_parts));
parts.extend(hardline!());
parts.push(hardline!(p));
}
parts.push(text!("}"));
array!(p, parts)
Expand All @@ -612,11 +612,11 @@ impl<'a> Format<'a> for TSEnumDeclaration<'a> {
if self.members.len() > 0 {
let mut indent_parts = Vec::new_in(p.allocator);
for member in &self.members {
indent_parts.extend(hardline!());
indent_parts.push(hardline!(p));
indent_parts.push(member.format(p));
}
parts.push(indent!(p, indent_parts));
parts.extend(hardline!());
parts.push(hardline!(p));
}
parts.push(text!("}"));

Expand Down Expand Up @@ -664,13 +664,8 @@ impl<'a> Format<'a> for TSModuleDeclaration<'a> {

if let Some(body) = &self.body {
if !body.is_empty() {
let mut indent_parts = Vec::new_in(p.allocator);

indent_parts.extend(hardline!());
indent_parts.push(body.format(p));

parts.push(indent!(p, indent_parts));
parts.extend(hardline!());
parts.push(indent!(p, [hardline!(p), body.format(p)]));
parts.push(hardline!(p));
}
}

Expand Down
26 changes: 15 additions & 11 deletions crates/oxc_prettier/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ macro_rules! join {
if i != 0 {
match $sep {
$crate::ir::JoinSeparator::Softline => parts.push($crate::softline!()),
$crate::ir::JoinSeparator::Hardline => parts.extend($crate::hardline!()),
$crate::ir::JoinSeparator::Hardline => parts.push($crate::hardline!($p)),
$crate::ir::JoinSeparator::CommaLine => {
parts.extend([$crate::text!(","), $crate::line!()]);
}
$crate::ir::JoinSeparator::Literalline => parts.extend($crate::literalline!()),
$crate::ir::JoinSeparator::Literalline => parts.push($crate::literalline!($p)),
}
}
parts.push(doc);
Expand Down Expand Up @@ -249,13 +249,15 @@ macro_rules! softline {
/// Specify a line break that is always included in the output, no matter if the expression fits on one line or not.
/// <https://github.com/prettier/prettier/blob/3.4.2/commands.md#hardline>
/// ```
/// hardline!();
/// hardline!(p);
/// ```
#[macro_export]
macro_rules! hardline {
() => {{
let hardline = $crate::ir::Doc::Line($crate::ir::Line { hard: true, ..Default::default() });
[hardline, $crate::ir::Doc::BreakParent]
($p:ident) => {{
let mut temp_vec = oxc_allocator::Vec::new_in($p.allocator);
temp_vec.push($crate::ir::Doc::Line($crate::ir::Line { hard: true, ..Default::default() }));
temp_vec.push($crate::ir::Doc::BreakParent);
$crate::ir::Doc::Array(temp_vec)
}};
}

Expand All @@ -264,17 +266,19 @@ macro_rules! hardline {
/// This is used for template literals.
/// <https://github.com/prettier/prettier/blob/3.4.2/commands.md#literalline>
/// ```
/// literalline!();
/// literalline!(p);
/// ```
#[macro_export]
macro_rules! literalline {
() => {{
let literalline = $crate::ir::Doc::Line($crate::ir::Line {
($p:ident) => {{
let mut temp_vec = oxc_allocator::Vec::new_in($p.allocator);
temp_vec.push($crate::ir::Doc::Line($crate::ir::Line {
hard: true,
literal: true,
..Default::default()
});
[literalline, $crate::ir::Doc::BreakParent]
}));
temp_vec.push($crate::ir::Doc::BreakParent);
$crate::ir::Doc::Array(temp_vec)
}};
}

Expand Down

0 comments on commit c1c0d71

Please sign in to comment.