diff --git a/deployment/schema.json b/deployment/schema.json index 81d4e8db..26f456a6 100644 --- a/deployment/schema.json +++ b/deployment/schema.json @@ -367,6 +367,30 @@ "description": "" }] }, + "preferSingleLineDefaultTrue": { + "description": "If code should revert back from being on multiple lines to being on a single line when able. Defaults to the global `preferSingleLine` value when it's set and otherwise `true`.", + "type": "boolean", + "default": true, + "oneOf": [{ + "const": true, + "description": "" + }, { + "const": false, + "description": "" + }] + }, + "jsxExpressionContainer.preferSingleLine": { + "description": "Whether to keep a JSX expression container's braces beside its expression when there's a newline between the open brace and the expression. Only the newline after the open brace is checked, so when this is `false` the close brace goes on its own line even if it was beside the expression. Defaults to the global `preferSingleLine` value when it's set and otherwise `true`.", + "type": "boolean", + "default": true, + "oneOf": [{ + "const": true, + "description": "Keeps the braces beside the expression even when there's a newline after the open brace. Ex. `{value}`" + }, { + "const": false, + "description": "Puts the braces on their own lines with the expression indented between them when there's a newline after the open brace." + }] + }, "forceSingleLine": { "description": "If code should be forced to be on a single line if able.", "type": "boolean", @@ -1733,13 +1757,13 @@ "$ref": "#/definitions/preferSingleLine" }, "exportDeclaration.preferSingleLine": { - "$ref": "#/definitions/preferSingleLine" + "$ref": "#/definitions/preferSingleLineDefaultTrue" }, "forStatement.preferSingleLine": { "$ref": "#/definitions/preferSingleLine" }, "importDeclaration.preferSingleLine": { - "$ref": "#/definitions/preferSingleLine" + "$ref": "#/definitions/preferSingleLineDefaultTrue" }, "jsxAttributes.preferSingleLine": { "$ref": "#/definitions/preferSingleLine" @@ -1747,6 +1771,9 @@ "jsxElement.preferSingleLine": { "$ref": "#/definitions/preferSingleLine" }, + "jsxExpressionContainer.preferSingleLine": { + "$ref": "#/definitions/jsxExpressionContainer.preferSingleLine" + }, "mappedType.preferSingleLine": { "$ref": "#/definitions/preferSingleLine" }, diff --git a/src/configuration/builder.rs b/src/configuration/builder.rs index 403c5d80..84565375 100644 --- a/src/configuration/builder.rs +++ b/src/configuration/builder.rs @@ -1324,6 +1324,17 @@ impl ConfigurationBuilder { self.insert("jsxElement.preferSingleLine", value.into()) } + /// Whether to keep a JSX expression container's braces beside its expression + /// when there's a newline after the open brace. + /// + /// * `true` - Keeps the braces beside the expression. Ex. `{value}` + /// * `false` - Puts the braces on their own lines when there's a newline after the open brace. + /// + /// Default: The global `preferSingleLine` value when set, otherwise `true`. + pub fn jsx_expression_container_prefer_single_line(&mut self, value: bool) -> &mut Self { + self.insert("jsxExpressionContainer.preferSingleLine", value.into()) + } + pub fn mapped_type_prefer_single_line(&mut self, value: bool) -> &mut Self { self.insert("mappedType.preferSingleLine", value.into()) } @@ -1635,6 +1646,7 @@ mod tests { .import_declaration_prefer_single_line(false) .jsx_attributes_prefer_single_line(false) .jsx_element_prefer_single_line(false) + .jsx_expression_container_prefer_single_line(false) .mapped_type_prefer_single_line(false) .member_expression_prefer_single_line(false) .object_expression_prefer_single_line(false) @@ -1705,7 +1717,7 @@ mod tests { .with_statement_space_around(true); let inner_config = config.get_inner_config(); - assert_eq!(inner_config.len(), 231); + assert_eq!(inner_config.len(), 232); let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics; assert_eq!(diagnostics.len(), 0); } diff --git a/src/configuration/resolve_config.rs b/src/configuration/resolve_config.rs index 6c49b742..318a7a3e 100644 --- a/src/configuration/resolve_config.rs +++ b/src/configuration/resolve_config.rs @@ -304,6 +304,12 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration) ), jsx_attributes_prefer_single_line: get_value(&mut config, "jsxAttributes.preferSingleLine", prefer_single_line, &mut diagnostics), jsx_element_prefer_single_line: get_value(&mut config, "jsxElement.preferSingleLine", prefer_single_line, &mut diagnostics), + jsx_expression_container_prefer_single_line: get_value( + &mut config, + "jsxExpressionContainer.preferSingleLine", + prefer_single_line_nullable.unwrap_or(true), + &mut diagnostics, + ), mapped_type_prefer_single_line: get_value(&mut config, "mappedType.preferSingleLine", prefer_single_line, &mut diagnostics), member_expression_prefer_single_line: get_value(&mut config, "memberExpression.preferSingleLine", prefer_single_line, &mut diagnostics), object_expression_prefer_single_line: get_value(&mut config, "objectExpression.preferSingleLine", prefer_single_line, &mut diagnostics), @@ -825,6 +831,29 @@ mod tests { assert_eq!(result.diagnostics.len(), 0); } + #[test] + fn handle_jsx_expression_container_prefer_single_line() { + let resolve = |values: &[(&str, bool)]| { + let mut config = ConfigKeyMap::new(); + for (key, value) in values { + config.insert(key.to_string(), ConfigKeyValue::from_bool(*value)); + } + let result = resolve_config(config, &GlobalConfiguration::default()); + assert_eq!(result.diagnostics.len(), 0); + result.config.jsx_expression_container_prefer_single_line + }; + + // defaults to true when the global value isn't set + assert!(resolve(&[])); + // falls back to the global value when it's set + assert!(!resolve(&[("preferSingleLine", false)])); + assert!(resolve(&[("preferSingleLine", true)])); + // the per-node value wins over the global value + assert!(resolve(&[("preferSingleLine", false), ("jsxExpressionContainer.preferSingleLine", true)])); + assert!(!resolve(&[("preferSingleLine", true), ("jsxExpressionContainer.preferSingleLine", false)])); + assert!(!resolve(&[("jsxExpressionContainer.preferSingleLine", false)])); + } + #[test] fn handle_sort_type_only_imports_exports() { for (value, expected) in [ diff --git a/src/configuration/types.rs b/src/configuration/types.rs index 4b0e0845..fbed7bca 100644 --- a/src/configuration/types.rs +++ b/src/configuration/types.rs @@ -853,6 +853,8 @@ pub struct Configuration { pub jsx_attributes_prefer_single_line: bool, #[serde(rename = "jsxElement.preferSingleLine")] pub jsx_element_prefer_single_line: bool, + #[serde(rename = "jsxExpressionContainer.preferSingleLine")] + pub jsx_expression_container_prefer_single_line: bool, #[serde(rename = "mappedType.preferSingleLine")] pub mapped_type_prefer_single_line: bool, #[serde(rename = "memberExpression.preferSingleLine")] diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 0020f1dc..d9d6b414 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -4594,7 +4594,13 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut } } - if context.parent().is::() && context.config.jsx_multi_line_parens != JsxMultiLineParens::Always { + if let Some(container) = context.parent().to::() + && context.config.jsx_multi_line_parens != JsxMultiLineParens::Always + { + // the container already puts the jsx on its own lines + if should_surround_jsx_expr_container_expr_with_newlines(container, context) { + return inner_items; + } return surround_with_newlines_indented_if_multi_line(inner_items, context.config.indent_width); } @@ -4648,7 +4654,11 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut parent = parent.parent().unwrap(); } - parent.is::() + match parent.to::() { + // the container already puts the jsx on its own lines + Some(container) => !should_surround_jsx_expr_container_expr_with_newlines(container, context), + None => false, + } } } @@ -4800,7 +4810,7 @@ fn gen_jsx_expr_container<'a>(node: &JSXExprContainer<'a>, context: &mut Context gen_as_jsx_expr_container_with_new_lines(node.expr.range(), gen_inner, surround_with_new_lines, surround_with_space, context) } JSXExpr::Expr(expr) => { - let surround_with_new_lines = should_surround_jsx_expr_container_with_newlines(node.expr.range(), context.program); + let surround_with_new_lines = should_surround_jsx_expr_container_expr_with_newlines(node, context); // keep an object or array literal's brackets beside the braces (ex. `{{ value: true }}`) let surround_with_space = context.config.jsx_expression_container_space_surrounding_expression && !is_jsx_expr_container_object_or_array_literal(*expr, context); @@ -4868,6 +4878,39 @@ fn should_surround_jsx_expr_container_with_newlines(expr: SourceRange, program: false } +/// Gets if the braces of a container with an expression go on their own lines +/// because of comments or a newline after the open brace that's maintained. +fn should_surround_jsx_expr_container_expr_with_newlines<'a>(node: &JSXExprContainer<'a>, context: &Context<'a>) -> bool { + should_surround_jsx_expr_container_with_newlines(node.expr.range(), context.program) + || !context.config.jsx_expression_container_prefer_single_line + && has_new_line_after_jsx_expr_container_open_brace(node, context.program) + // the parens already put multi-line jsx on its own lines (ex. `{(` and `)}`) + && !is_jsx_surrounded_by_multi_line_parens(node, context) +} + +/// Gets if the container's expression starts on a line after its open brace. +fn has_new_line_after_jsx_expr_container_open_brace(node: &JSXExprContainer, program: Program) -> bool { + match node.expr { + JSXExpr::Expr(expr) => node.start_line_fast(program) != expr.start_line_fast(program), + JSXExpr::JSXEmptyExpr(_) => false, + } +} + +/// Gets if the container's expression is jsx that `jsx.multiLineParens: always` +/// surrounds with parens when it's on multiple lines. +fn is_jsx_surrounded_by_multi_line_parens<'a>(node: &JSXExprContainer<'a>, context: &Context<'a>) -> bool { + if context.config.jsx_multi_line_parens != JsxMultiLineParens::Always { + return false; + } + let JSXExpr::Expr(mut expr) = node.expr else { + return false; + }; + while let Expr::Paren(paren_expr) = expr { + expr = paren_expr.expr; + } + is_jsx_paren_expr_handled_node(expr.into(), context) +} + /// Gets if the expression will be generated as an object or array literal /// with no comments between it and the braces of its container. fn is_jsx_expr_container_object_or_array_literal<'a>(expr: Expr<'a>, context: &Context<'a>) -> bool { diff --git a/tests/specs/issues/issue0577.txt b/tests/specs/issues/issue0577.txt new file mode 100644 index 00000000..af6068ac --- /dev/null +++ b/tests/specs/issues/issue0577.txt @@ -0,0 +1,69 @@ +-- file.tsx -- +~~ indentWidth: 4, jsxExpressionContainer.preferSingleLine: false ~~ +== should keep the braces of a jsx expression container on their own lines == +function Ok() { + return ( +
+ { + isThisConditionOk + ? ( +

Yes

+ ) + : ( +

No

+ ) + } +
+ ); +} + +[expect] +function Ok() { + return ( +
+ { + isThisConditionOk + ? ( +

+ Yes +

+ ) + : ( +

+ No +

+ ) + } +
+ ); +} + +== should keep the braces of a jsx attribute value on their own lines == +function Ok() { + return ( +
+ ); +} + +[expect] +function Ok() { + return ( +
+ ); +} diff --git a/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_Default.txt b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_Default.txt new file mode 100644 index 00000000..9b3976ea --- /dev/null +++ b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_Default.txt @@ -0,0 +1,90 @@ +-- file.tsx -- +== should keep the braces beside the expression when there's a newline after the open brace == +const t =
+ { + value + } +
; +const u =
; + +[expect] +const t = ( +
+ {value} +
+); +const u = ( +
+); + +== should keep the braces beside a conditional with jsx == +const t =
+ { + isThisConditionOk + ?

Yes

+ :

No

+ } +
; + +[expect] +const t = ( +
+ {isThisConditionOk + ? ( +

+ Yes +

+ ) + : ( +

+ No +

+ )} +
+); + +== should not add blank lines around multi-line jsx after a comment == +const t =
{ + // comment + Yesyes +}
; +const u =
Yesyes +} />; + +[expect] +const t = ( +
+ { + // comment + + Yesyes + + } +
+); +const u = ( +
+ Yesyes + + } + /> +); diff --git a/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_False.txt b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_False.txt new file mode 100644 index 00000000..c8c74e52 --- /dev/null +++ b/tests/specs/jsx/JsxExpressionContainer/JsxExpressionContainer_PreferSingleLine_False.txt @@ -0,0 +1,317 @@ +-- file.tsx -- +~~ jsxExpressionContainer.preferSingleLine: false ~~ +== should put the braces on their own lines when there's a newline after the open brace == +const t =
+ { + value + } +
; + +[expect] +const t = ( +
+ { + value + } +
+); + +== should hug the braces when there's no newline after the open brace == +const t =
+ {value + } +
; +const u =
{cond + ? a + : b}
; + +[expect] +const t = ( +
+ {value} +
+); +const u = ( +
+ {cond + ? a + : b} +
+); + +== should only check the newline after the open brace == +const t =
{ + cond ? a : b}
; + +[expect] +const t = ( +
+ { + cond ? a : b + } +
+); + +== should keep a long expression on its own lines == +const t =
+ { + isThisConditionOk ?

Yes

:

No

+ } +
; + +[expect] +const t = ( +
+ { + isThisConditionOk + ? ( +

+ Yes +

+ ) + : ( +

+ No +

+ ) + } +
+); + +== should keep a logical and expression with jsx on its own lines == +const t =
+ { + cond && Yesyes + } +
; + +[expect] +const t = ( +
+ { + cond && ( + + Yesyes + + ) + } +
+); + +== should keep jsx on its own lines == +const t =
+ { + Yesyes + } +
; +const u =
+ { + Yes + } +
; +const v =
Yesyes +} />; + +[expect] +const t = ( +
+ { + + Yesyes + + } +
+); +const u = ( +
+ { + Yes + } +
+); +const v = ( +
+ Yesyes + + } + /> +); + +== should keep an attribute value on its own lines == +const t =
; + +[expect] +const t = ( +
+); + +== should keep an arrow function on its own lines == +const t =