diff --git a/deployment/schema.json b/deployment/schema.json index 1d410a78..31fe3cfa 100644 --- a/deployment/schema.json +++ b/deployment/schema.json @@ -333,6 +333,18 @@ "description": "Prefers not using parentheses when possible." }] }, + "arrowFunction.noNewLineBeforeFnCall": { + "description": "Disable inserting new line before function call in arrow function body.", + "type": "boolean", + "default": false, + "oneOf": [{ + "const": true, + "description": "" + }, { + "const": false, + "description": "" + }] + }, "binaryExpression.linePerExpression": { "description": "Whether to force a line per expression when spanning multiple lines.", "type": "boolean", diff --git a/src/configuration/builder.rs b/src/configuration/builder.rs index b6aa475d..5fe4cae5 100644 --- a/src/configuration/builder.rs +++ b/src/configuration/builder.rs @@ -474,6 +474,13 @@ impl ConfigurationBuilder { self.insert("arrowFunction.useParentheses", value.to_string().into()) } + /// Disable inserting new line before function call in arrow function body. + /// + /// Default: `false` + pub fn arrow_function_no_new_line_before_fn_call(&mut self, value: bool) -> &mut Self { + self.insert("arrowFunction.noNewLineBeforeFnCall", value.into()) + } + /// Whether to force a line per expression when spanning multiple lines. /// /// * `true` - Formats with each part on a new line. @@ -1086,6 +1093,7 @@ mod tests { .prefer_hanging(false) /* situational */ .arrow_function_use_parentheses(UseParentheses::Maintain) + .arrow_function_no_new_line_before_fn_call(false) .binary_expression_line_per_expression(false) .conditional_expression_line_per_expression(true) .member_expression_line_per_expression(false) @@ -1259,7 +1267,7 @@ mod tests { .while_statement_space_around(true); let inner_config = config.get_inner_config(); - assert_eq!(inner_config.len(), 177); + assert_eq!(inner_config.len(), 178); let diagnostics = resolve_config(inner_config, &resolve_global_config(ConfigKeyMap::new(), &Default::default()).config).diagnostics; assert_eq!(diagnostics.len(), 0); } diff --git a/src/configuration/resolve_config.rs b/src/configuration/resolve_config.rs index 67ac3c59..3fa8c662 100644 --- a/src/configuration/resolve_config.rs +++ b/src/configuration/resolve_config.rs @@ -94,6 +94,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration) semi_colons, /* situational */ arrow_function_use_parentheses: get_value(&mut config, "arrowFunction.useParentheses", UseParentheses::Maintain, &mut diagnostics), + arrow_function_no_new_line_before_fn_call: get_value(&mut config, "arrowFunction.noNewLineBeforeFnCall", false, &mut diagnostics), binary_expression_line_per_expression: get_value(&mut config, "binaryExpression.linePerExpression", false, &mut diagnostics), conditional_expression_line_per_expression: get_value(&mut config, "conditionalExpression.linePerExpression", true, &mut diagnostics), jsx_quote_style: get_value(&mut config, "jsx.quoteStyle", quote_style.to_jsx_quote_style(), &mut diagnostics), diff --git a/src/configuration/types.rs b/src/configuration/types.rs index 4d1aa8fc..6b7bd9ce 100644 --- a/src/configuration/types.rs +++ b/src/configuration/types.rs @@ -293,6 +293,8 @@ pub struct Configuration { /* situational */ #[serde(rename = "arrowFunction.useParentheses")] pub arrow_function_use_parentheses: UseParentheses, + #[serde(rename = "arrowFunction.noNewLineBeforeFnCall")] + pub arrow_function_no_new_line_before_fn_call: bool, #[serde(rename = "binaryExpression.linePerExpression")] pub binary_expression_line_per_expression: bool, #[serde(rename = "conditionalExpression.linePerExpression")] diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 33023626..be657e99 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -1730,6 +1730,7 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr, context: &mut Context<'a>) -> Pr BlockStmtOrExpr::BlockStmt(_) => true, BlockStmtOrExpr::Expr(expr) => match expr { Expr::Paren(_) | Expr::Array(_) => true, + Expr::Call(_) | Expr::TaggedTpl(_) => context.config.arrow_function_no_new_line_before_fn_call, Expr::Tpl(tpl) => tpl.quasis[0].raw().starts_with(|c: char| c == '\n' || c == '\r'), _ => is_jsx_paren_expr_handled_node(expr.into(), context), }, diff --git a/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_False.txt b/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_False.txt new file mode 100644 index 00000000..b824692b --- /dev/null +++ b/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_False.txt @@ -0,0 +1,25 @@ +~~ arrowFunction.noNewLineBeforeFnCall: false ~~ +== should insert new line after arrow but before fn call when option is false == +[].map((it) => test({ + foo: 1, + bar: 2 +})); + +[expect] +[].map((it) => + test({ + foo: 1, + bar: 2, + }) +); + +== should insert new line after arrow but before tagged template when option is false == +const getStyles = () => css` + display: flex; +`; + +[expect] +const getStyles = () => + css` + display: flex; +`; diff --git a/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_True.txt b/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_True.txt new file mode 100644 index 00000000..013ad646 --- /dev/null +++ b/tests/specs/expressions/ArrowFunctionExpression/ArrowFunctionExpression_NoNewLineBeforeFnCall_True.txt @@ -0,0 +1,22 @@ +~~ arrowFunction.noNewLineBeforeFnCall: true ~~ +== should not insert new line after arrow but before fn call when option is true == +[].map((it) => test({ + foo: 1, + bar: 2 +})); + +[expect] +[].map((it) => test({ + foo: 1, + bar: 2, +})); + +== should not insert new line after arrow but before tagged template when option is true == +const getStyles = () => css` + display: flex; +`; + +[expect] +const getStyles = () => css` + display: flex; +`;