Skip to content
Open
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
39 changes: 37 additions & 2 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9214,23 +9214,58 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<'
if children.is_empty() {
items.push_signal(Signal::PossibleNewLine);
} else {
let mut previous_child = None;
// `gen_jsx_text` trims boundary whitespace so multi-line JSX text doesn't keep source indentation.
// Restore meaningful same-line edge spaces here when the first or last rendered child is JSX text.
let children_len = children.len();
let mut previous_child: Option<Node<'a>> = None;
let mut before_last_child: Option<Node<'a>> = None;
for (index, (child, generated_child)) in children.into_iter().enumerate() {
if index > 0 && should_use_space(*previous_child.as_ref().unwrap(), child, context) {
items.extend(jsx_space_separator(*previous_child.as_ref().unwrap(), child, context));
} else if index == 0 && has_jsx_leading_space(child, context) {
items.push_signal(Signal::SpaceOrNewLine);
} else {
items.push_signal(Signal::PossibleNewLine);
}

items.extend(generated_child.into());

if children_len >= 2 && index == children_len - 2 {
before_last_child = Some(child);
}
previous_child = Some(child);
}
items.push_signal(Signal::PossibleNewLine);
if has_jsx_trailing_space(*previous_child.as_ref().unwrap(), before_last_child, context) {
items.push_signal(Signal::SpaceOrNewLine);
} else {
items.push_signal(Signal::PossibleNewLine);
}
}
items
}

fn has_jsx_leading_space<'a>(current_node: Node<'a>, context: &Context<'a>) -> bool {
if let Node::JSXText(text) = current_node {
let text = text.text_fast(context.program);
text.starts_with(' ') && utils::has_no_new_lines_in_leading_whitespace(text)
} else {
false
}
}

fn has_jsx_trailing_space<'a>(current_node: Node<'a>, previous_node: Option<Node<'a>>, context: &Context<'a>) -> bool {
if let Node::JSXText(text) = current_node {
if matches!(previous_node, Some(previous_node) if is_ignore_jsx_expr_container(previous_node, context)) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_ignore_jsx_expr_container

Otherwise tests/specs/issues/deno/issue026681.txt fails

return false;
}

let text = text.text_fast(context.program);
text.ends_with(' ') && utils::has_no_new_lines_in_trailing_whitespace(text)
} else {
false
}
}

fn should_use_space<'a>(previous_child: Node<'a>, current: Node<'a>, context: &mut Context<'a>) -> bool {
if has_jsx_space_between(previous_child, current, context.program) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/jsx/JsxElement/JsxElement_Spaces.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const t = <div>{t} {u} {v}</div>;
[expect]
const t = <div>{t} {u} {v}</div>;

== should remove spaces surrounding ==
== should keep spaces surrounding ==
const t = <div> test </div>;

[expect]
const t = <div>test</div>;
const t = <div> test </div>;

== should handle a space when exceeding the line width with only the parent when the parent and children are on the same line ==
const t = <div>{test} {testsdffffffffffff}</div>;
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/jsx/JsxText/JsxText_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const t = <> Testing this out </>;

[expect]
const t = <>Testing this out</>;
const t = <> Testing this out </>;

== should format when multi line ==
const t = <>
Expand Down