Skip to content

Commit f611613

Browse files
committed
Add arrowFunction.noNewLineBeforeFnCall option
Resolves #107. If set to true dprint will not format this: ```typescript [].map(() => test({ foo: 1, bar: 2, })); ```` to this: ```typescript [].map(() => test({ foo: 1, bar: 2, }) ); ```
1 parent 31f1d03 commit f611613

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

deployment/schema.json

+12
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,18 @@
333333
"description": "Prefers not using parentheses when possible."
334334
}]
335335
},
336+
"arrowFunction.noNewLineBeforeFnCall": {
337+
"description": "Disable inserting new line before function call in arrow function body.",
338+
"type": "boolean",
339+
"default": false,
340+
"oneOf": [{
341+
"const": true,
342+
"description": ""
343+
}, {
344+
"const": false,
345+
"description": ""
346+
}]
347+
},
336348
"binaryExpression.linePerExpression": {
337349
"description": "Whether to force a line per expression when spanning multiple lines.",
338350
"type": "boolean",

src/configuration/builder.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,13 @@ impl ConfigurationBuilder {
474474
self.insert("arrowFunction.useParentheses", value.to_string().into())
475475
}
476476

477+
/// Disable inserting new line before function call in arrow function body.
478+
///
479+
/// Default: `false`
480+
pub fn arrow_function_no_new_line_before_fn_call(&mut self, value: bool) -> &mut Self {
481+
self.insert("arrowFunction.noNewLineBeforeFnCall", value.into())
482+
}
483+
477484
/// Whether to force a line per expression when spanning multiple lines.
478485
///
479486
/// * `true` - Formats with each part on a new line.
@@ -1086,6 +1093,7 @@ mod tests {
10861093
.prefer_hanging(false)
10871094
/* situational */
10881095
.arrow_function_use_parentheses(UseParentheses::Maintain)
1096+
.arrow_function_no_new_line_before_fn_call(false)
10891097
.binary_expression_line_per_expression(false)
10901098
.conditional_expression_line_per_expression(true)
10911099
.member_expression_line_per_expression(false)
@@ -1259,7 +1267,7 @@ mod tests {
12591267
.while_statement_space_around(true);
12601268

12611269
let inner_config = config.get_inner_config();
1262-
assert_eq!(inner_config.len(), 177);
1270+
assert_eq!(inner_config.len(), 178);
12631271
let diagnostics = resolve_config(inner_config, &resolve_global_config(ConfigKeyMap::new(), &Default::default()).config).diagnostics;
12641272
assert_eq!(diagnostics.len(), 0);
12651273
}

src/configuration/resolve_config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
9494
semi_colons,
9595
/* situational */
9696
arrow_function_use_parentheses: get_value(&mut config, "arrowFunction.useParentheses", UseParentheses::Maintain, &mut diagnostics),
97+
arrow_function_no_new_line_before_fn_call: get_value(&mut config, "arrowFunction.noNewLineBeforeFnCall", false, &mut diagnostics),
9798
binary_expression_line_per_expression: get_value(&mut config, "binaryExpression.linePerExpression", false, &mut diagnostics),
9899
conditional_expression_line_per_expression: get_value(&mut config, "conditionalExpression.linePerExpression", true, &mut diagnostics),
99100
jsx_quote_style: get_value(&mut config, "jsx.quoteStyle", quote_style.to_jsx_quote_style(), &mut diagnostics),

src/configuration/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ pub struct Configuration {
293293
/* situational */
294294
#[serde(rename = "arrowFunction.useParentheses")]
295295
pub arrow_function_use_parentheses: UseParentheses,
296+
#[serde(rename = "arrowFunction.noNewLineBeforeFnCall")]
297+
pub arrow_function_no_new_line_before_fn_call: bool,
296298
#[serde(rename = "binaryExpression.linePerExpression")]
297299
pub binary_expression_line_per_expression: bool,
298300
#[serde(rename = "conditionalExpression.linePerExpression")]

src/generation/generate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr, context: &mut Context<'a>) -> Pr
17301730
BlockStmtOrExpr::BlockStmt(_) => true,
17311731
BlockStmtOrExpr::Expr(expr) => match expr {
17321732
Expr::Paren(_) | Expr::Array(_) => true,
1733+
Expr::Call(_) => context.config.arrow_function_no_new_line_before_fn_call,
17331734
Expr::Tpl(tpl) => tpl.quasis[0].raw().starts_with(|c: char| c == '\n' || c == '\r'),
17341735
_ => is_jsx_paren_expr_handled_node(expr.into(), context),
17351736
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
~~ arrowFunction.noNewLineBeforeFnCall: false ~~
2+
== should not insert new line after arrow when option is true ==
3+
[].map((it) => test({
4+
foo: 1,
5+
bar: 2
6+
}));
7+
8+
[expect]
9+
[].map((it) =>
10+
test({
11+
foo: 1,
12+
bar: 2,
13+
})
14+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
~~ arrowFunction.noNewLineBeforeFnCall: true ~~
2+
== should not insert new line after arrow when option is true ==
3+
[].map((it) => test({
4+
foo: 1,
5+
bar: 2
6+
}));
7+
8+
[expect]
9+
[].map((it) => test({
10+
foo: 1,
11+
bar: 2,
12+
}));

0 commit comments

Comments
 (0)