diff --git a/src/generation/generate.rs b/src/generation/generate.rs index f978a04b..52c19481 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -3949,16 +3949,16 @@ fn gen_as_jsx_expr_container(expr: Node, inner_items: PrintItems, context: &mut if surround_with_new_lines { items.push_signal(Signal::NewLine); items.push_signal(Signal::StartIndent); - } else if surround_with_space { - items.push_space(); - } - items.extend(inner_items); - if surround_with_new_lines { + items.extend(inner_items); items.extend(gen_trailing_comments_as_statements(&expr.range(), context)); items.push_signal(Signal::NewLine); items.push_signal(Signal::FinishIndent); } else if surround_with_space { - items.push_space(); + items.push_signal(Signal::SpaceOrNewLine); + items.extend(inner_items); + items.push_condition(if_false("addSpaceIfNotStartOfLine", condition_resolvers::is_start_of_line(), " ".into())); + } else { + items.extend(inner_items); } items.push_sc(sc!("}")); diff --git a/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_SpaceSurroundingExpression_True.txt b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_SpaceSurroundingExpression_True.txt index 15a7b4a9..32ba7728 100644 --- a/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_SpaceSurroundingExpression_True.txt +++ b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_SpaceSurroundingExpression_True.txt @@ -5,3 +5,193 @@ const t = ; [expect] const t = ; + +== should not add extra space before closing brace in nested JSX == + + } +/>; + +[expect] + + } +/>; + +== should not add extra space in similar nested JSX pattern == + + } + otherProp="value" +/>; + +[expect] + + } + otherProp="value" +/>; + +== should handle deeply nested JSX without extra spaces == + + } + /> + } +/>; + +[expect] + + } + /> + } +/>; + +== expression braces on the same line as a component == +{contents}; + +[expect] +{ contents }; + +== expression braces on the same line == +return ( +{contents}); + +[expect] +return ( + + { contents } + +); + +== expression braces across multiple lines == +return {
}; + +[expect] +return ( + + { +
+
+
+ } + +); + +== open braces followed by open braces == +return ( + +); + +[expect] +return ( + +); + +== open braces with compound expressions == +<> + {groups.length === 0 && ( +
+

text

+
+ )} +; + +[expect] +<> + { groups.length === 0 && ( +
+

text

+
+ ) } +; + +== open braces with arrow function == +<> + {data.map((data, index) => ( +
+ { data.label }: { data.text } +
+ ))} +; + +[expect] +<> + { data.map((data, index) => ( +
+ { data.label }: { data.text } +
+ )) } +; + +== more varied formatting == + + } + /> + +[expect] + } +/>; + +== more varied formatting across lines == + + } + /> + +[expect] + + } +/>;