Skip to content

Commit bf953b8

Browse files
committed
fix(formatter): should group nested test for TSConditionalType (#14661)
Align Prettier's behavior
1 parent 63dc57b commit bf953b8

File tree

3 files changed

+77
-46
lines changed

3 files changed

+77
-46
lines changed

crates/oxc_formatter/src/utils/conditional.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use oxc_ast::ast::*;
44
use oxc_span::{GetSpan, Span};
55

66
use crate::{
7-
Format, FormatResult, FormatWrite,
7+
Format, FormatResult, FormatWrite, format_args,
88
formatter::{Formatter, prelude::*, trivia::FormatTrailingComments},
99
generated::ast_nodes::{AstNode, AstNodes},
1010
utils::format_node_without_trailing_comments::FormatNodeWithoutTrailingComments,
@@ -373,41 +373,34 @@ impl<'a> FormatConditionalLike<'a, '_> {
373373
f: &mut Formatter<'f, 'a>,
374374
layout: ConditionalLayout,
375375
) -> FormatResult<()> {
376-
let format_inner = format_with(|f| match self.conditional {
377-
ConditionalLike::ConditionalExpression(conditional) => {
378-
write!(f, FormatNodeWithoutTrailingComments(conditional.test()))?;
379-
format_trailing_comments(
380-
conditional.test.span().end,
381-
conditional.consequent.span().start,
382-
b'?',
383-
f,
384-
)
385-
}
386-
ConditionalLike::TSConditionalType(conditional) => {
387-
write!(
388-
f,
389-
[
390-
conditional.check_type(),
391-
space(),
392-
"extends",
393-
space(),
394-
FormatNodeWithoutTrailingComments(conditional.extends_type())
395-
]
396-
)?;
376+
let format_inner = format_with(|f| {
377+
let (start, end) = match self.conditional {
378+
ConditionalLike::ConditionalExpression(conditional) => {
379+
write!(f, FormatNodeWithoutTrailingComments(conditional.test()))?;
380+
(conditional.test.span().end, conditional.consequent.span().start)
381+
}
382+
ConditionalLike::TSConditionalType(conditional) => {
383+
write!(
384+
f,
385+
[
386+
conditional.check_type(),
387+
space(),
388+
"extends",
389+
space(),
390+
FormatNodeWithoutTrailingComments(conditional.extends_type())
391+
]
392+
)?;
393+
(conditional.extends_type.span().end, conditional.true_type.span().start)
394+
}
395+
};
397396

398-
format_trailing_comments(
399-
conditional.extends_type.span().end,
400-
conditional.true_type.span().start,
401-
b'?',
402-
f,
403-
)
404-
}
397+
format_trailing_comments(start, end, b'?', f)
405398
});
406399

407400
if layout.is_nested_alternate() {
408401
write!(f, [align(2, &format_inner)])
409402
} else {
410-
format_inner.fmt(f)
403+
write!(f, format_inner)
411404
}
412405
}
413406

@@ -420,27 +413,19 @@ impl<'a> FormatConditionalLike<'a, '_> {
420413
write!(f, [soft_line_break_or_space(), "?", space()])?;
421414

422415
let format_consequent = format_with(|f| {
423-
let format_consequent_with_trailing_comments =
424-
format_once(|f| match self.conditional {
416+
let format_consequent_with_trailing_comments = format_once(|f| {
417+
let (start, end) = match self.conditional {
425418
ConditionalLike::ConditionalExpression(conditional) => {
426419
write!(f, FormatNodeWithoutTrailingComments(conditional.consequent()))?;
427-
format_trailing_comments(
428-
conditional.consequent.span().end,
429-
conditional.alternate.span().start,
430-
b':',
431-
f,
432-
)
420+
(conditional.consequent.span().end, conditional.alternate.span().start)
433421
}
434422
ConditionalLike::TSConditionalType(conditional) => {
435423
write!(f, FormatNodeWithoutTrailingComments(conditional.true_type()))?;
436-
format_trailing_comments(
437-
conditional.true_type.span().end,
438-
conditional.false_type.span().start,
439-
b':',
440-
f,
441-
)
424+
(conditional.true_type.span().end, conditional.false_type.span().start)
442425
}
443-
});
426+
};
427+
format_trailing_comments(start, end, b':', f)
428+
});
444429

445430
let format_consequent_with_proper_indentation = format_with(|f| {
446431
if f.options().indent_style.is_space() {
@@ -606,7 +591,11 @@ impl<'a> Format<'a> for FormatConditionalLike<'a, '_> {
606591
});
607592

608593
let grouped = format_with(|f| {
609-
if layout.is_root() { write!(f, [group(&format_inner)]) } else { format_inner.fmt(f) }
594+
if layout.is_root() || layout.is_nested_test() {
595+
write!(f, [group(&format_inner)])
596+
} else {
597+
format_inner.fmt(f)
598+
}
610599
});
611600

612601
if layout.is_nested_test() || should_extra_indent {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type IsUnion<T> = (
2+
Testtttttttttttttttttttttttttttttttttt extends any ? false : never
3+
) extends false
4+
? false
5+
: true
6+
7+
8+
9+
export const IsUnionType = (
10+
Testtttttttttttttttttttttttttttttttttt ? false : never
11+
) ? false
12+
: true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
type IsUnion<T> = (
6+
Testtttttttttttttttttttttttttttttttttt extends any ? false : never
7+
) extends false
8+
? false
9+
: true
10+
11+
12+
13+
export const IsUnionType = (
14+
Testtttttttttttttttttttttttttttttttttt ? false : never
15+
) ? false
16+
: true
17+
==================== Output ====================
18+
type IsUnion<T> = (
19+
Testtttttttttttttttttttttttttttttttttt extends any ? false : never
20+
) extends false
21+
? false
22+
: true;
23+
24+
export const IsUnionType = (
25+
Testtttttttttttttttttttttttttttttttttt ? false : never
26+
)
27+
? false
28+
: true;
29+
30+
===================== End =====================

0 commit comments

Comments
 (0)