Skip to content

Commit

Permalink
fix(js_formatter): allow JSX expressions to nestle on arrow chains (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
faultyserver authored Dec 4, 2023
1 parent 3317660 commit 290c67b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ impl Format<JsFormatContext> for ArrowChain {
AnyJsExpression::JsObjectExpression(_)
| AnyJsExpression::JsArrayExpression(_)
| AnyJsExpression::JsSequenceExpression(_)
| AnyJsExpression::JsxTagExpression(_)
)
);

Expand Down Expand Up @@ -672,13 +673,25 @@ impl Format<JsFormatContext> for ArrowChain {
});

let format_tail_body = format_with(|f| {
// if it's inside a JSXExpression (e.g. an attribute) we should align the expression's closing } with the line with the opening {.
let should_add_soft_line = matches!(
head_parent.kind(),
Some(
JsSyntaxKind::JSX_EXPRESSION_CHILD
| JsSyntaxKind::JSX_EXPRESSION_ATTRIBUTE_VALUE
)
);

if body_on_separate_line {
write!(
f,
[indent(&format_args![
soft_line_break_or_space(),
format_tail_body_inner
])]
[
indent(&format_args![
soft_line_break_or_space(),
format_tail_body_inner
]),
should_add_soft_line.then_some(soft_line_break())
]
)
} else {
write!(f, [space(), format_tail_body_inner])
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ mod language {
include!("language.rs");
}

#[ignore]
// #[ignore]
#[test]
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
export default foo as bar;
((C) => (props) => <C {...props} />);
(({C}) => (props) => <C {...props} />);
"#;
let source_type = JsFileSource::tsx();
let tree = parse(
Expand Down
11 changes: 11 additions & 0 deletions crates/biome_js_formatter/tests/specs/jsx/arrow_function.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ function ArrowBodyIsJsxWithComment({ action }) {
<li/>
);
}



function ArrowCurryWithPlainParameters() {
return (C) => (props) => <C {...props} />;
}

function ArrowCurryWithDestructuringParameters() {
return ({ C }) =>
(props) => <C {...props} />;
}
20 changes: 20 additions & 0 deletions crates/biome_js_formatter/tests/specs/jsx/arrow_function.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ function ArrowBodyIsJsxWithComment({ action }) {
);
}
function ArrowCurryWithPlainParameters() {
return (C) => (props) => <C {...props} />;
}
function ArrowCurryWithDestructuringParameters() {
return ({ C }) =>
(props) => <C {...props} />;
}
```


Expand Down Expand Up @@ -92,6 +103,15 @@ function ArrowBodyIsJsxWithComment({ action }) {
<li />
);
}
function ArrowCurryWithPlainParameters() {
return (C) => (props) => <C {...props} />;
}
function ArrowCurryWithDestructuringParameters() {
return ({ C }) =>
(props) => <C {...props} />;
}
```


183 changes: 0 additions & 183 deletions crates/biome_js_formatter/tests/specs/prettier/jsx/jsx/arrow.js.snap

This file was deleted.

0 comments on commit 290c67b

Please sign in to comment.