diff --git a/crates/oxc_prettier/src/format/array.rs b/crates/oxc_prettier/src/format/array.rs index bfdd2a019..cfdf33189 100644 --- a/crates/oxc_prettier/src/format/array.rs +++ b/crates/oxc_prettier/src/format/array.rs @@ -21,26 +21,24 @@ impl<'a, 'b> Array<'a, 'b> { } } -impl<'a> Prettier<'a> { - pub(super) fn print_array(&mut self, array: &Array<'a, '_>) -> Doc<'a> { - if array.len() == 0 { - return ss!("[]"); - } - - let mut parts = self.vec(); - parts.push(ss!("[")); - - let mut parts_inner = self.vec(); - parts_inner.push(Doc::Softline); - parts_inner.extend(print_elements(self, array)); - parts_inner.push(if_break!(self, ",")); - - parts.push(group!(self, Doc::Indent(parts_inner))); - parts.push(Doc::Softline); - parts.push(ss!("]")); - - Doc::Group(parts) +pub(super) fn print_array<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { + if array.len() == 0 { + return ss!("[]"); } + + let mut parts = p.vec(); + parts.push(ss!("[")); + + let mut parts_inner = p.vec(); + parts_inner.push(Doc::Softline); + parts_inner.extend(print_elements(p, array)); + parts_inner.push(if_break!(p, ",")); + + parts.push(group!(p, Doc::Indent(parts_inner))); + parts.push(Doc::Softline); + parts.push(ss!("]")); + + Doc::Group(parts) } fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Vec<'a, Doc<'a>> { diff --git a/crates/oxc_prettier/src/format/arrow_function.rs b/crates/oxc_prettier/src/format/arrow_function.rs index a1da17091..d74bef1d0 100644 --- a/crates/oxc_prettier/src/format/arrow_function.rs +++ b/crates/oxc_prettier/src/format/arrow_function.rs @@ -3,13 +3,14 @@ use oxc_ast::ast::*; use crate::{doc::Doc, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_arrow_function(&mut self, expr: &ArrowExpression<'a>) -> Doc<'a> { - let mut parts = self.vec(); +pub(super) fn print_arrow_function<'a>( + p: &mut Prettier<'a>, + expr: &ArrowExpression<'a>, +) -> Doc<'a> { + let mut parts = p.vec(); - parts.push(ss!("() => ")); - parts.push(expr.body.format(self)); + parts.push(ss!("() => ")); + parts.push(expr.body.format(p)); - Doc::Array(parts) - } + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/binaryish.rs b/crates/oxc_prettier/src/format/binaryish.rs index 42410db97..c1975bc1c 100644 --- a/crates/oxc_prettier/src/format/binaryish.rs +++ b/crates/oxc_prettier/src/format/binaryish.rs @@ -33,19 +33,17 @@ impl BinaryishOperator { } } -impl<'a> Prettier<'a> { - pub(super) fn print_binaryish_expression<'b>( - &mut self, - left: &BinaryishLeft<'a, 'b>, - operator: BinaryishOperator, - right: &Expression<'a>, - ) -> Doc<'a> { - let mut parts = self.vec(); - parts.push(left.format(self)); - parts.push(ss!(" ")); - parts.push(ss!(operator.as_str())); - parts.push(Doc::Line); - parts.push(right.format(self)); - Doc::Array(parts) - } +pub(super) fn print_binaryish_expression<'a>( + p: &mut Prettier<'a>, + left: &BinaryishLeft<'a, '_>, + operator: BinaryishOperator, + right: &Expression<'a>, +) -> Doc<'a> { + let mut parts = p.vec(); + parts.push(left.format(p)); + parts.push(ss!(" ")); + parts.push(ss!(operator.as_str())); + parts.push(Doc::Line); + parts.push(right.format(p)); + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/block.rs b/crates/oxc_prettier/src/format/block.rs index c5fccefe2..e5ad97b79 100644 --- a/crates/oxc_prettier/src/format/block.rs +++ b/crates/oxc_prettier/src/format/block.rs @@ -4,50 +4,50 @@ use oxc_ast::ast::*; use crate::{doc::Doc, hardline, indent, ss, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_block( - &mut self, - stmts: &Vec<'a, Statement<'a>>, - directives: Option<&Vec<'a, Directive>>, - is_static_block: bool, - ) -> Doc<'a> { - let mut parts = self.vec(); - if is_static_block { - parts.push(ss!("static ")); - } - parts.push(ss!("{")); - if let Some(doc) = self.print_block_body(stmts, directives) { - parts.push(indent![self, hardline!(), doc]); - parts.push(hardline!()); - } - parts.push(ss!("}")); - Doc::Array(parts) +use super::statement; + +pub(super) fn print_block<'a>( + p: &mut Prettier<'a>, + stmts: &Vec<'a, Statement<'a>>, + directives: Option<&Vec<'a, Directive>>, + is_static_block: bool, +) -> Doc<'a> { + let mut parts = p.vec(); + if is_static_block { + parts.push(ss!("static ")); } - - pub(super) fn print_block_body( - &mut self, - stmts: &Vec<'a, Statement<'a>>, - directives: Option<&Vec<'a, Directive>>, - ) -> Option> { - let has_directives = directives.is_some_and(|directives| !directives.is_empty()); - let has_body = stmts.iter().any(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); - - if !has_body && !has_directives { - return None; - } - - let mut parts = self.vec(); - - if has_directives { - if let Some(directives) = directives { - parts.extend(self.print_statement_sequence(directives)); - } - } - - if !stmts.is_empty() { - parts.extend(self.print_statement_sequence(stmts)); - } - - Some(Doc::Array(parts)) + parts.push(ss!("{")); + if let Some(doc) = print_block_body(p, stmts, directives) { + parts.push(indent![p, hardline!(), doc]); + parts.push(hardline!()); } + parts.push(ss!("}")); + Doc::Array(parts) +} + +pub(super) fn print_block_body<'a>( + p: &mut Prettier<'a>, + stmts: &Vec<'a, Statement<'a>>, + directives: Option<&Vec<'a, Directive>>, +) -> Option> { + let has_directives = directives.is_some_and(|directives| !directives.is_empty()); + let has_body = stmts.iter().any(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); + + if !has_body && !has_directives { + return None; + } + + let mut parts = p.vec(); + + if has_directives { + if let Some(directives) = directives { + parts.extend(statement::print_statement_sequence(p, directives)); + } + } + + if !stmts.is_empty() { + parts.extend(statement::print_statement_sequence(p, stmts)); + } + + Some(Doc::Array(parts)) } diff --git a/crates/oxc_prettier/src/format/call_expression.rs b/crates/oxc_prettier/src/format/call_expression.rs index a91fb6a6a..db83f2b10 100644 --- a/crates/oxc_prettier/src/format/call_expression.rs +++ b/crates/oxc_prettier/src/format/call_expression.rs @@ -4,19 +4,17 @@ use oxc_ast::ast::*; use crate::{doc::Doc, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_call_expression( - &mut self, - callee: &Expression<'a>, - arguments: &Vec<'a, Argument<'a>>, - optional: bool, // for optional chaining - type_parameters: &Option>>, - ) -> Doc<'a> { - let mut parts = self.vec(); - parts.push(callee.format(self)); - parts.push(ss!("(")); - parts.extend(arguments.iter().map(|arg| arg.format(self))); - parts.push(ss!(")")); - Doc::Array(parts) - } +pub(super) fn print_call_expression<'a>( + p: &mut Prettier<'a>, + callee: &Expression<'a>, + arguments: &Vec<'a, Argument<'a>>, + optional: bool, // for optional chaining + type_parameters: &Option>>, +) -> Doc<'a> { + let mut parts = p.vec(); + parts.push(callee.format(p)); + parts.push(ss!("(")); + parts.extend(arguments.iter().map(|arg| arg.format(p))); + parts.push(ss!(")")); + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/class.rs b/crates/oxc_prettier/src/format/class.rs index bd4bd1683..cb47dffab 100644 --- a/crates/oxc_prettier/src/format/class.rs +++ b/crates/oxc_prettier/src/format/class.rs @@ -3,15 +3,13 @@ use oxc_ast::ast::*; use crate::{doc::Doc, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_class(&mut self, class: &Class<'a>) -> Doc<'a> { - let mut parts = self.vec(); - parts.push(ss!("class ")); - if let Some(id) = &class.id { - parts.push(id.format(self)); - } - parts.push(ss!(" ")); - parts.push(class.body.format(self)); - Doc::Array(parts) +pub(super) fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a> { + let mut parts = p.vec(); + parts.push(ss!("class ")); + if let Some(id) = &class.id { + parts.push(id.format(p)); } + parts.push(ss!(" ")); + parts.push(class.body.format(p)); + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/function.rs b/crates/oxc_prettier/src/format/function.rs index cd0fc1944..4f257cbd2 100644 --- a/crates/oxc_prettier/src/format/function.rs +++ b/crates/oxc_prettier/src/format/function.rs @@ -3,66 +3,64 @@ use oxc_ast::ast::*; use crate::{doc::Doc, group, hardline, if_break, indent, softline, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_function(&mut self, func: &Function<'a>) -> Doc<'a> { - let mut parts = self.vec(); - if let Some(comments) = self.print_leading_comments(func.span) { - parts.push(comments); - } - if func.r#async { - parts.push(ss!("async ")); - } - if func.generator { - parts.push(ss!("function* ")); - } else { - parts.push(ss!("function ")); - } - if let Some(type_params) = &func.type_parameters { - parts.push(type_params.format(self)); - } - if let Some(id) = &func.id { - parts.push(self.str(id.name.as_str())); - } - if should_group_function_parameters(func) { - parts.push(group!(self, func.params.format(self))); - } else { - parts.push(func.params.format(self)); - } - if let Some(body) = &func.body { - parts.push(ss!(" ")); - parts.push(body.format(self)); - } - if self.options.semi && (func.is_ts_declare_function() || func.body.is_none()) { - parts.push(self.str(";")); - } - - Doc::Array(parts) +pub(super) fn print_function<'a>(p: &mut Prettier<'a>, func: &Function<'a>) -> Doc<'a> { + let mut parts = p.vec(); + if let Some(comments) = p.print_leading_comments(func.span) { + parts.push(comments); + } + if func.r#async { + parts.push(ss!("async ")); + } + if func.generator { + parts.push(ss!("function* ")); + } else { + parts.push(ss!("function ")); + } + if let Some(type_params) = &func.type_parameters { + parts.push(type_params.format(p)); + } + if let Some(id) = &func.id { + parts.push(p.str(id.name.as_str())); + } + if should_group_function_parameters(func) { + parts.push(group!(p, func.params.format(p))); + } else { + parts.push(func.params.format(p)); + } + if let Some(body) = &func.body { + parts.push(ss!(" ")); + parts.push(body.format(p)); + } + if p.options.semi && (func.is_ts_declare_function() || func.body.is_none()) { + parts.push(p.str(";")); } - pub(super) fn print_return_or_throw_argument( - &mut self, - argument: Option<&Expression<'a>>, - is_return: bool, - ) -> Doc<'a> { - let mut parts = self.vec(); + Doc::Array(parts) +} - parts.push(ss!(if is_return { "return" } else { "throw" })); +pub(super) fn print_return_or_throw_argument<'a>( + p: &mut Prettier<'a>, + argument: Option<&Expression<'a>>, + is_return: bool, +) -> Doc<'a> { + let mut parts = p.vec(); - if let Some(argument) = argument { - parts.push(ss!(" ")); - parts.push(group![ - self, - if_break!(self, "("), - indent!(self, softline!(), argument.format(self)), - softline!(), - if_break!(self, ")") - ]); - } + parts.push(ss!(if is_return { "return" } else { "throw" })); - parts.push(self.str(";")); - parts.push(hardline!()); - Doc::Array(parts) + if let Some(argument) = argument { + parts.push(ss!(" ")); + parts.push(group![ + p, + if_break!(p, "("), + indent!(p, softline!(), argument.format(p)), + softline!(), + if_break!(p, ")") + ]); } + + parts.push(p.str(";")); + parts.push(hardline!()); + Doc::Array(parts) } fn should_group_function_parameters(func: &Function) -> bool { diff --git a/crates/oxc_prettier/src/format/function_parameters.rs b/crates/oxc_prettier/src/format/function_parameters.rs index d684773cc..fcb988883 100644 --- a/crates/oxc_prettier/src/format/function_parameters.rs +++ b/crates/oxc_prettier/src/format/function_parameters.rs @@ -3,24 +3,25 @@ use oxc_ast::ast::*; use crate::{doc::Doc, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_function_parameters(&mut self, params: &FormalParameters<'a>) -> Doc<'a> { - let mut parts = self.vec(); - parts.push(ss!("(")); +pub(super) fn print_function_parameters<'a>( + p: &mut Prettier<'a>, + params: &FormalParameters<'a>, +) -> Doc<'a> { + let mut parts = p.vec(); + parts.push(ss!("(")); - for (i, param) in params.items.iter().enumerate() { - parts.push(param.format(self)); - if i < params.items.len() - 1 { - parts.push(ss!(", ")); - } - } - - if let Some(rest) = ¶ms.rest { + for (i, param) in params.items.iter().enumerate() { + parts.push(param.format(p)); + if i < params.items.len() - 1 { parts.push(ss!(", ")); - parts.push(rest.format(self)); } - - parts.push(ss!(")")); - Doc::Array(parts) } + + if let Some(rest) = ¶ms.rest { + parts.push(ss!(", ")); + parts.push(rest.format(p)); + } + + parts.push(ss!(")")); + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index cffc7d0bb..033d61e7a 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -52,7 +52,7 @@ where impl<'a> Format<'a> for Program<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_block_body(&self.body, Some(&self.directives)).unwrap_or(ss!("")) + block::print_block_body(p, &self.body, Some(&self.directives)).unwrap_or(ss!("")) } } @@ -131,7 +131,7 @@ impl<'a> Format<'a> for IfStatement<'a> { impl<'a> Format<'a> for BlockStatement<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_block(&self.body, None, false) + block::print_block(p, &self.body, None, false) } } @@ -262,7 +262,7 @@ impl<'a> Format<'a> for SwitchCase<'a> { impl<'a> Format<'a> for ReturnStatement<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_return_or_throw_argument(self.argument.as_ref(), true) + function::print_return_or_throw_argument(p, self.argument.as_ref(), true) } } @@ -311,7 +311,7 @@ impl<'a> Format<'a> for CatchClause<'a> { impl<'a> Format<'a> for ThrowStatement<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_return_or_throw_argument(Some(&self.argument), false) + function::print_return_or_throw_argument(p, Some(&self.argument), false) } } @@ -352,7 +352,7 @@ impl<'a> Format<'a> for ModuleDeclaration<'a> { if let ModuleDeclaration::ImportDeclaration(decl) = self { decl.format(p) } else { - p.print_export_declaration(self) + module::print_export_declaration(p, self) } } } @@ -460,19 +460,19 @@ impl<'a> Format<'a> for VariableDeclarator<'a> { impl<'a> Format<'a> for Function<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_function(self) + function::print_function(p, self) } } impl<'a> Format<'a> for FunctionBody<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_block(&self.statements, Some(&self.directives), false) + block::print_block(p, &self.statements, Some(&self.directives), false) } } impl<'a> Format<'a> for FormalParameters<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_function_parameters(self) + function_parameters::print_function_parameters(p, self) } } @@ -790,7 +790,13 @@ impl<'a> Format<'a> for PrivateFieldExpression<'a> { impl<'a> Format<'a> for CallExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_call_expression(&self.callee, &self.arguments, self.optional, &self.type_parameters) + call_expression::print_call_expression( + p, + &self.callee, + &self.arguments, + self.optional, + &self.type_parameters, + ) } } @@ -821,13 +827,13 @@ impl<'a> Format<'a> for SpreadElement<'a> { impl<'a> Format<'a> for ArrayExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_array(&Array::ArrayExpression(self)) + array::print_array(p, &Array::ArrayExpression(self)) } } impl<'a> Format<'a> for ObjectExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_object_properties(&self.properties) + object::print_object_properties(p, &self.properties) } } @@ -863,7 +869,7 @@ impl<'a> Format<'a> for PropertyKey<'a> { impl<'a> Format<'a> for ArrowExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_arrow_function(self) + arrow_function::print_arrow_function(p, self) } } @@ -912,7 +918,8 @@ impl<'a> Format<'a> for UnaryExpression<'a> { impl<'a> Format<'a> for BinaryExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_binaryish_expression( + binaryish::print_binaryish_expression( + p, &BinaryishLeft::Expression(&self.left), BinaryishOperator::BinaryOperator(self.operator), &self.right, @@ -922,7 +929,8 @@ impl<'a> Format<'a> for BinaryExpression<'a> { impl<'a> Format<'a> for PrivateInExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_binaryish_expression( + binaryish::print_binaryish_expression( + p, &BinaryishLeft::PrivateIdentifier(&self.left), BinaryishOperator::BinaryOperator(self.operator), &self.right, @@ -932,7 +940,8 @@ impl<'a> Format<'a> for PrivateInExpression<'a> { impl<'a> Format<'a> for LogicalExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_binaryish_expression( + binaryish::print_binaryish_expression( + p, &BinaryishLeft::Expression(&self.left), BinaryishOperator::LogicalOperator(self.operator), &self.right, @@ -942,7 +951,7 @@ impl<'a> Format<'a> for LogicalExpression<'a> { impl<'a> Format<'a> for ConditionalExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_ternary(self) + ternary::print_ternary(p, self) } } @@ -1102,7 +1111,13 @@ impl<'a> Format<'a> for ChainElement<'a> { impl<'a> Format<'a> for NewExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_call_expression(&self.callee, &self.arguments, false, &self.type_parameters) + call_expression::print_call_expression( + p, + &self.callee, + &self.arguments, + false, + &self.type_parameters, + ) } } @@ -1114,7 +1129,7 @@ impl<'a> Format<'a> for MetaProperty { impl<'a> Format<'a> for Class<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_class(self) + class::print_class(p, self) } } @@ -1274,7 +1289,7 @@ impl<'a> Format<'a> for JSXFragment<'a> { impl<'a> Format<'a> for StaticBlock<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_block(&self.body, None, false) + block::print_block(p, &self.body, None, false) } } @@ -1320,7 +1335,7 @@ impl<'a> Format<'a> for BindingPattern<'a> { impl<'a> Format<'a> for ObjectPattern<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - p.print_object_properties(&self.properties) + object::print_object_properties(p, &self.properties) } } diff --git a/crates/oxc_prettier/src/format/module.rs b/crates/oxc_prettier/src/format/module.rs index 0a1d1eba6..fa5d95c95 100644 --- a/crates/oxc_prettier/src/format/module.rs +++ b/crates/oxc_prettier/src/format/module.rs @@ -3,26 +3,27 @@ use oxc_ast::ast::*; use crate::{doc::Doc, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_export_declaration(&mut self, decl: &ModuleDeclaration<'a>) -> Doc<'a> { - debug_assert!(decl.is_export()); +pub(super) fn print_export_declaration<'a>( + p: &mut Prettier<'a>, + decl: &ModuleDeclaration<'a>, +) -> Doc<'a> { + debug_assert!(decl.is_export()); - let mut parts = self.vec(); - parts.push(ss!("export")); + let mut parts = p.vec(); + parts.push(ss!("export")); - if decl.is_default_export() { - parts.push(ss!(" default ")); - } - - parts.push(match decl { - ModuleDeclaration::ImportDeclaration(decl) => unreachable!(), - ModuleDeclaration::ExportAllDeclaration(decl) => decl.format(self), - ModuleDeclaration::ExportDefaultDeclaration(decl) => decl.format(self), - ModuleDeclaration::ExportNamedDeclaration(decl) => decl.format(self), - ModuleDeclaration::TSExportAssignment(decl) => decl.format(self), - ModuleDeclaration::TSNamespaceExportDeclaration(decl) => decl.format(self), - }); - - Doc::Array(parts) + if decl.is_default_export() { + parts.push(ss!(" default ")); } + + parts.push(match decl { + ModuleDeclaration::ImportDeclaration(decl) => unreachable!(), + ModuleDeclaration::ExportAllDeclaration(decl) => decl.format(p), + ModuleDeclaration::ExportDefaultDeclaration(decl) => decl.format(p), + ModuleDeclaration::ExportNamedDeclaration(decl) => decl.format(p), + ModuleDeclaration::TSExportAssignment(decl) => decl.format(p), + ModuleDeclaration::TSNamespaceExportDeclaration(decl) => decl.format(p), + }); + + Doc::Array(parts) } diff --git a/crates/oxc_prettier/src/format/object.rs b/crates/oxc_prettier/src/format/object.rs index ac8998bfe..111cba637 100644 --- a/crates/oxc_prettier/src/format/object.rs +++ b/crates/oxc_prettier/src/format/object.rs @@ -4,42 +4,40 @@ use crate::{doc::Doc, group, if_break, ss, Prettier}; use super::Format; -impl<'a> Prettier<'a> { - pub(super) fn print_object_properties>( - &mut self, - properties: &Vec<'a, F>, - ) -> Doc<'a> { - let mut parts = self.vec(); - parts.push(ss!("{")); +pub(super) fn print_object_properties<'a, F: Format<'a>>( + p: &mut Prettier<'a>, + properties: &Vec<'a, F>, +) -> Doc<'a> { + let mut parts = p.vec(); + parts.push(ss!("{")); - let mut indent_parts = self.vec(); + let mut indent_parts = p.vec(); - if self.options.bracket_spacing { + if p.options.bracket_spacing { + indent_parts.push(Doc::Line); + } else { + indent_parts.push(Doc::Softline); + }; + + let len = properties.len(); + properties.iter().map(|prop| prop.format(p)).enumerate().for_each(|(i, prop)| { + indent_parts.push(prop); + if i < len - 1 { + indent_parts.push(Doc::Str(",")); indent_parts.push(Doc::Line); - } else { - indent_parts.push(Doc::Softline); - }; - - let len = properties.len(); - properties.iter().map(|prop| prop.format(self)).enumerate().for_each(|(i, prop)| { - indent_parts.push(prop); - if i < len - 1 { - indent_parts.push(Doc::Str(",")); - indent_parts.push(Doc::Line); - } - }); - - parts.push(group!(self, Doc::Indent(indent_parts))); - parts.push(if_break!(self, ",")); - - if self.options.bracket_spacing { - parts.push(Doc::Line); - } else { - parts.push(Doc::Softline); } + }); - parts.push(ss!("}")); + parts.push(group!(p, Doc::Indent(indent_parts))); + parts.push(if_break!(p, ",")); - Doc::Group(parts) + if p.options.bracket_spacing { + parts.push(Doc::Line); + } else { + parts.push(Doc::Softline); } + + parts.push(ss!("}")); + + Doc::Group(parts) } diff --git a/crates/oxc_prettier/src/format/statement.rs b/crates/oxc_prettier/src/format/statement.rs index d0d4ed14d..400bbfd96 100644 --- a/crates/oxc_prettier/src/format/statement.rs +++ b/crates/oxc_prettier/src/format/statement.rs @@ -4,15 +4,13 @@ use crate::{doc::Doc, Prettier}; use super::Format; -impl<'a> Prettier<'a> { - pub(super) fn print_statement_sequence>( - &mut self, - stmts: &Vec<'a, F>, - ) -> Vec<'a, Doc<'a>> { - let mut parts = self.vec(); - for stmt in stmts { - parts.push(stmt.format(self)); - } - parts +pub(super) fn print_statement_sequence<'a, F: Format<'a>>( + p: &mut Prettier<'a>, + stmts: &Vec<'a, F>, +) -> Vec<'a, Doc<'a>> { + let mut parts = p.vec(); + for stmt in stmts { + parts.push(stmt.format(p)); } + parts } diff --git a/crates/oxc_prettier/src/format/ternary.rs b/crates/oxc_prettier/src/format/ternary.rs index e454af661..2470101b3 100644 --- a/crates/oxc_prettier/src/format/ternary.rs +++ b/crates/oxc_prettier/src/format/ternary.rs @@ -3,20 +3,18 @@ use oxc_ast::ast::*; use crate::{doc::Doc, group, indent, ss, Format, Prettier}; -impl<'a> Prettier<'a> { - pub(super) fn print_ternary(&mut self, expr: &ConditionalExpression<'a>) -> Doc<'a> { - group![ - self, - expr.test.format(self), - indent!( - self, - Doc::Line, - ss!("? "), - expr.consequent.format(self), - Doc::Line, - ss!(": "), - expr.alternate.format(self) - ) - ] - } +pub(super) fn print_ternary<'a>(p: &mut Prettier<'a>, expr: &ConditionalExpression<'a>) -> Doc<'a> { + group![ + p, + expr.test.format(p), + indent!( + p, + Doc::Line, + ss!("? "), + expr.consequent.format(p), + Doc::Line, + ss!(": "), + expr.alternate.format(p) + ) + ] }