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
21 changes: 5 additions & 16 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
write!(f, "?")?;
}

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(),
Expand Down Expand Up @@ -639,30 +630,28 @@ pub fn format_grouped_parameters_with_return_type<'a>(
return_type: Option<&AstNode<'a, TSTypeAnnotation<'a>>>,
f: &mut Formatter<'_, 'a>,
) -> FormatResult<()> {
write!(f, [type_parameters])?;

group(&format_once(|f| {
let mut format_type_parameters = type_parameters.memoized();
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_type_parameters.inspect(f)?;
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()),
params.parameters_count() + 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)])
write!(f, [group(&format_args!(format_type_parameters, format_parameters))])
} else {
write!(f, [format_parameters])
write!(f, [format_type_parameters, format_parameters])
}?;

write!(f, [format_return_type])
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_formatter/src/write/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ impl<'a> FormatWrite<'a> for FormatFunction<'a, '_> {

let group_parameters = should_group_function_parameters(
self.type_parameters.as_deref(),
params.items.len()
+ usize::from(params.rest.is_some())
+ usize::from(self.this_param.is_some()),
params.parameters_count() + usize::from(self.this_param.is_some()),
self.return_type.as_deref(),
&mut format_return_type,
f,
Expand Down
39 changes: 26 additions & 13 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ObjectProperty<'a>> {
};

if self.method || is_accessor {
let AstNodes::Function(func) = self.value().as_ast_nodes() else {
let AstNodes::Function(value) = self.value().as_ast_nodes() else {
unreachable!(
"The `value` always be a function node if `method` or `accessor` is true"
)
};

if func.r#async() {
if value.r#async() {
write!(f, ["async", space()])?;
}
if func.generator() {
if value.generator() {
write!(f, "*")?;
}
if self.computed {
Expand All @@ -191,14 +191,15 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ObjectProperty<'a>> {
format_property_key(self.key(), f)?;
}

if let Some(type_parameters) = &func.type_parameters() {
write!(f, type_parameters)?;
}
write!(f, group(&func.params()))?;
if let Some(return_type) = &func.return_type() {
write!(f, return_type)?;
}
if let Some(body) = &func.body() {
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() {
write!(f, [space(), body])?;
}

Expand Down Expand Up @@ -1561,7 +1562,13 @@ 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<()> {
write!(f, group(&format_args!(self.type_parameters(), self.params(), self.return_type())))
format_grouped_parameters_with_return_type(
self.type_parameters(),
None,
self.params(),
self.return_type(),
f,
)
}
}

Expand Down Expand Up @@ -1600,7 +1607,13 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructSignatureDeclaration<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, ["new", space()])?;
write!(f, group(&format_args!(self.type_parameters(), self.params(), self.return_type())))
format_grouped_parameters_with_return_type(
self.type_parameters(),
None,
self.params(),
self.return_type(),
f,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

type A = {
new(...args): T<{
A
}
>
};


type A1 = {
(...args): T<{
A
}
>
};

type A2 = {
bar(
...args
): T<{
A
}>
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================

type A = {
new(...args): T<{
A
}
>
};


type A1 = {
(...args): T<{
A
}
>
};

type A2 = {
bar(
...args
): T<{
A
}>
}


==================== Output ====================
type A = {
new (...args): T<{
A;
}>;
};

type A1 = {
(...args): T<{
A;
}>;
};

type A2 = {
bar(...args): T<{
A;
}>;
};

===================== End =====================
6 changes: 6 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/objects/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const builder = {
build(environment: BuildEnvironment): Promise<
RollupOutput | RollupOutput[]
> {
},
}
19 changes: 19 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/objects/methods.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
const builder = {
build(environment: BuildEnvironment): Promise<
RollupOutput | RollupOutput[]
> {
},
}

==================== Output ====================
const builder = {
build(
environment: BuildEnvironment,
): Promise<RollupOutput | RollupOutput[]> {},
};

===================== End =====================
Loading