Skip to content

Commit e0dd420

Browse files
solution
1 parent 6402971 commit e0dd420

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

src/generation/generate.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9214,23 +9214,57 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<'
92149214
if children.is_empty() {
92159215
items.push_signal(Signal::PossibleNewLine);
92169216
} else {
9217-
let mut previous_child = None;
9217+
let children_len = children.len();
9218+
let mut previous_child: Option<Node<'a>> = None;
9219+
let mut before_last_child: Option<Node<'a>> = None;
92189220
for (index, (child, generated_child)) in children.into_iter().enumerate() {
92199221
if index > 0 && should_use_space(*previous_child.as_ref().unwrap(), child, context) {
92209222
items.extend(jsx_space_separator(*previous_child.as_ref().unwrap(), child, context));
9223+
} else if index == 0 && jsx_child_has_leading_space(child, context) {
9224+
items.push_signal(Signal::SpaceOrNewLine);
92219225
} else {
92229226
items.push_signal(Signal::PossibleNewLine);
92239227
}
92249228

92259229
items.extend(generated_child.into());
92269230

9231+
if children_len >= 2 && index == children_len - 2 {
9232+
before_last_child = Some(child);
9233+
}
92279234
previous_child = Some(child);
92289235
}
9229-
items.push_signal(Signal::PossibleNewLine);
9236+
if jsx_child_has_trailing_space(*previous_child.as_ref().unwrap(), before_last_child, context) {
9237+
items.push_signal(Signal::SpaceOrNewLine);
9238+
} else {
9239+
items.push_signal(Signal::PossibleNewLine);
9240+
}
92309241
}
92319242
items
92329243
}
92339244

9245+
fn jsx_child_has_leading_space<'a>(child: Node<'a>, context: &mut Context<'a>) -> bool {
9246+
if let Node::JSXText(text) = child {
9247+
let raw = text.text_fast(context.program);
9248+
let leading_whitespace = &raw[..raw.len() - raw.trim_start().len()];
9249+
!leading_whitespace.is_empty() && !leading_whitespace.contains('\n')
9250+
} else {
9251+
false
9252+
}
9253+
}
9254+
9255+
fn jsx_child_has_trailing_space<'a>(child: Node<'a>, previous_child: Option<Node<'a>>, context: &mut Context<'a>) -> bool {
9256+
if let Node::JSXText(text) = child {
9257+
if previous_child.map_or(false, |prev| is_ignore_jsx_expr_container(prev, context)) {
9258+
return false;
9259+
}
9260+
let raw = text.text_fast(context.program);
9261+
let trailing_whitespace = &raw[raw.trim_end().len()..];
9262+
!trailing_whitespace.is_empty() && !trailing_whitespace.contains('\n')
9263+
} else {
9264+
false
9265+
}
9266+
}
9267+
92349268
fn should_use_space<'a>(previous_child: Node<'a>, current: Node<'a>, context: &mut Context<'a>) -> bool {
92359269
if has_jsx_space_between(previous_child, current, context.program) {
92369270
return true;

0 commit comments

Comments
 (0)