Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): Allow JSX expressions to nestle on arrow chains #1033

Merged
merged 2 commits into from
Dec 4, 2023
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
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.