Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions crates/oxc_formatter/src/generated/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4610,9 +4610,19 @@ impl<'a> Format<'a> for AstNode<'a, TSImportTypeQualifiedName<'a>> {
impl<'a> Format<'a> for AstNode<'a, TSFunctionType<'a>> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let is_suppressed = f.comments().is_suppressed(self.span().start);
if !is_suppressed && format_type_cast_comment_node(self, false, f)? {
return Ok(());
}
self.format_leading_comments(f)?;
let needs_parentheses = self.needs_parentheses(f);
if needs_parentheses {
"(".fmt(f)?;
}
let result =
if is_suppressed { FormatSuppressedNode(self.span()).fmt(f) } else { self.write(f) };
if needs_parentheses {
")".fmt(f)?;
}
self.format_trailing_comments(f)?;
result
}
Expand Down
74 changes: 60 additions & 14 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{
object::format_property_key,
},
write,
write::{semicolon::OptionalSemicolon, type_parameters},
write::{
function::should_group_function_parameters, semicolon::OptionalSemicolon, type_parameters,
},
};

use super::{
Expand Down Expand Up @@ -83,6 +85,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
}
}
let value = self.value();

if value.r#async {
write!(f, ["async", space()])?;
}
Expand All @@ -95,22 +98,27 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
format_property_key(self.key(), f)?;
}

if self.optional() {
if self.optional {
write!(f, "?")?;
}
if let Some(type_parameters) = &value.type_parameters() {
write!(f, type_parameters)?;
}
// Handle comments between method name and parameters
// Example: method /* comment */ (param) {}
let comments = f.context().comments().comments_before(value.params().span.start);
if !comments.is_empty() {
write!(f, [space(), FormatTrailingComments::Comments(comments)])?;
}
write!(f, group(&value.params()))?;
if let Some(return_type) = &value.return_type() {
write!(f, return_type)?;

if value.type_parameters.is_none() {
// // Handle comments between method name and parameters
// // Example: method /* comment */ (param) {}
// let comments = f.context().comments().comments_before(value.params().span.start);
// if !comments.is_empty() {
// write!(f, [space(), FormatTrailingComments::Comments(comments)])?;
// }
}

format_grouped_parameters_with_return_type(
value.type_parameters(),
value.this_param.as_deref(),
value.params(),
value.return_type(),
f,
)?;

if let Some(body) = &value.body() {
// Handle block comments between method signature and body
// Example: method() /* comment */ {}
Expand Down Expand Up @@ -623,3 +631,41 @@ impl<'a> Format<'a> for ClassPropertySemicolon<'a, '_> {
}
}
}

pub fn format_grouped_parameters_with_return_type<'a>(
type_parameters: Option<&AstNode<'a, TSTypeParameterDeclaration<'a>>>,
this_param: Option<&TSThisParameter<'a>>,
params: &AstNode<'a, FormalParameters<'a>>,
return_type: Option<&AstNode<'a, TSTypeAnnotation<'a>>>,
f: &mut Formatter<'_, 'a>,
) -> FormatResult<()> {
write!(f, [type_parameters])?;

group(&format_once(|f| {
let mut format_parameters = params.memoized();
let mut format_return_type = return_type.memoized();

// Inspect early, in case the `return_type` is formatted before `parameters`
// in `should_group_function_parameters`.
format_parameters.inspect(f)?;

let group_parameters = should_group_function_parameters(
type_parameters.map(AsRef::as_ref),
params.items.len()
+ usize::from(params.rest.is_some())
+ usize::from(this_param.is_some()),
return_type.map(AsRef::as_ref),
&mut format_return_type,
f,
)?;

if group_parameters {
write!(f, [group(&format_parameters)])
} else {
write!(f, [format_parameters])
}?;

write!(f, [format_return_type])
}))
.fmt(f)
}
6 changes: 3 additions & 3 deletions crates/oxc_formatter/src/write/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ impl<'a> FormatWrite<'a> for FormatFunction<'a, '_> {
)?;

if group_parameters {
write!(f, [group(&format_parameters)])?;
write!(f, [group(&format_parameters)])
} else {
write!(f, [format_parameters])?;
}
write!(f, [format_parameters])
}?;

write!(f, [format_return_type])
}))]
Expand Down
142 changes: 49 additions & 93 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ use crate::{

use self::{
array_expression::FormatArrayExpression,
class::format_grouped_parameters_with_return_type,
function::should_group_function_parameters,
object_like::ObjectLike,
object_pattern_like::ObjectPatternLike,
parameters::{ParameterLayout, ParameterList},
Expand Down Expand Up @@ -1072,10 +1074,16 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSEnumMember<'a>> {

impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAnnotation<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
if matches!(self.parent, AstNodes::TSTypePredicate(_)) {
write!(f, [self.type_annotation()])
} else {
write!(f, [":", space(), self.type_annotation()])
match self.parent {
AstNodes::TSFunctionType(_) | AstNodes::TSConstructorType(_) => {
write!(f, ["=>", space(), self.type_annotation()])
}
AstNodes::TSTypePredicate(_) => {
write!(f, [self.type_annotation()])
}
_ => {
write!(f, [":", space(), self.type_annotation()])
}
}
}
}
Expand Down Expand Up @@ -1466,18 +1474,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSSignature<'a>>> {

impl<'a> FormatWrite<'a> for AstNode<'a, TSCallSignatureDeclaration<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
if let Some(type_parameters) = &self.type_parameters() {
write!(f, group(type_parameters))?;
}

if let Some(this_param) = &self.this_param() {
write!(f, [this_param, ",", soft_line_break_or_space()])?;
}
write!(f, group(&self.params()))?;
if let Some(return_type) = &self.return_type() {
write!(f, return_type)?;
}
Ok(())
write!(f, group(&format_args!(self.type_parameters(), self.params(), self.return_type())))
}
}

Expand All @@ -1502,42 +1499,21 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
if self.optional() {
write!(f, "?")?;
}
// TODO:
// if should_group_function_parameters(
// type_parameters.as_ref(),
// parameters.len(),
// return_type_annotation
// .as_ref()
// .map(|annotation| annotation.ty()),
// &mut format_return_type_annotation,
// f,
// )? {
// write!(f, [group(&parameters)])?;
// } else {
// write!(f, [parameters])?;
// }
if let Some(type_parameters) = &self.type_parameters() {
write!(f, [group(&type_parameters)])?;
}
write!(f, group(&self.params()))?;
if let Some(return_type) = &self.return_type() {
write!(f, return_type)?;
}
Ok(())

format_grouped_parameters_with_return_type(
self.type_parameters(),
self.this_param.as_deref(),
self.params(),
self.return_type(),
f,
)
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructSignatureDeclaration<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, ["new", space()])?;
if let Some(type_parameters) = &self.type_parameters() {
write!(f, group(&type_parameters))?;
}
write!(f, group(&self.params()))?;
if let Some(return_type) = self.return_type() {
write!(f, return_type)?;
}
Ok(())
write!(f, group(&format_args!(self.type_parameters(), self.params(), self.return_type())))
}
}

Expand Down Expand Up @@ -1677,45 +1653,39 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSImportTypeQualifiedName<'a>> {

impl<'a> FormatWrite<'a> for AstNode<'a, TSFunctionType<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let type_parameters = self.type_parameters();
let params = self.params();
let return_type = self.return_type();

let format_inner = format_with(|f| {
let type_parameters = self.type_parameters();
write!(f, type_parameters)?;

// TODO
// let mut format_return_type = return_type.format().memoized();
let should_group_parameters = false;
// TODO
//should_group_function_parameters(
// type_parameters.as_ref(),
// parameters.as_ref()?.items().len(),
// Some(return_type.clone()),
// &mut format_return_type,
// f,
// )?;

if should_group_parameters {
write!(f, group(&params))?;
} else {
write!(f, params)?;
}
let params = self.params();
let return_type = self.return_type();
let mut format_parameters = params.memoized();
let mut format_return_type = return_type.memoized();

// Inspect early, in case the `return_type` is formatted before `parameters`
// in `should_group_function_parameters`.
format_parameters.inspect(f)?;

let group_parameters = should_group_function_parameters(
type_parameters.map(AsRef::as_ref),
params.items.len()
+ usize::from(params.rest.is_some())
+ usize::from(self.this_param.is_some()),
Some(&self.return_type),
&mut format_return_type,
f,
)?;

let comments = f.context().comments().comments_before_character(params.span.end, b'=');
FormatTrailingComments::Comments(comments).fmt(f)?;
if group_parameters {
write!(f, [group(&format_parameters)])
} else {
write!(f, [format_parameters])
}?;

write!(f, [space(), "=>", space(), return_type.type_annotation()])
write!(f, [space(), format_return_type])
});

if self.needs_parentheses(f) {
"(".fmt(f)?;
}
write!(f, group(&format_inner))?;
if self.needs_parentheses(f) {
")".fmt(f)?;
}
Ok(())
write!(f, group(&format_inner))
}
}

Expand All @@ -1731,21 +1701,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructorType<'a>> {
}
write!(
f,
[group(&format_args!(
"new",
space(),
type_parameters,
params,
format_once(|f| {
let comments =
f.context().comments().comments_before_character(params.span.end, b'=');
FormatTrailingComments::Comments(comments).fmt(f)
}),
space(),
"=>",
space(),
return_type.type_annotation()
))]
[group(&format_args!("new", space(), type_parameters, params, space(), return_type))]
);
Ok(())
}
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_formatter/src/write/object_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
generated::ast_nodes::{AstNode, AstNodes},
options::Expand,
write,
write::parameters::should_hug_function_parameters,
write::parameters::{get_this_param, should_hug_function_parameters},
};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -39,11 +39,7 @@ impl<'a> ObjectLike<'a, '_> {
let AstNodes::FormalParameters(parameters) = &param.parent else {
unreachable!()
};
let this_param = if let AstNodes::Function(function) = parameters.parent {
function.this_param()
} else {
None
};
let this_param = get_this_param(parameters.parent);
should_hug_function_parameters(parameters, this_param, false, f)

}
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_formatter/src/write/object_pattern_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
generated::ast_nodes::{AstNode, AstNodes},
write,
write::parameters::should_hug_function_parameters,
write::parameters::{get_this_param, should_hug_function_parameters},
};

use super::{
Expand Down Expand Up @@ -117,11 +117,7 @@ impl<'a> ObjectPatternLike<'a, '_> {
matches!(self, Self::ObjectPattern(node) if {
matches!(node.parent, AstNodes::FormalParameter(param) if {
matches!(param.parent, AstNodes::FormalParameters(params) if {
let this_param = if let AstNodes::Function(function) = params.parent {
function.this_param()
} else {
None
};
let this_param = get_this_param(param.parent);
should_hug_function_parameters(params, this_param, false, f)
})
})
Expand Down
Loading
Loading