Skip to content

Commit

Permalink
feat: Add arrowFunction.noNewLineBeforeFnCall option
Browse files Browse the repository at this point in the history
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,
    })
);
```
  • Loading branch information
Ty3uK committed May 26, 2023
1 parent 31f1d03 commit 26fafef
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => 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),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
~~ arrowFunction.noNewLineBeforeFnCall: false ~~
== should not insert new line after arrow when option is true ==
[].map((it) => test({
foo: 1,
bar: 2
}));

[expect]
[].map((it) =>
test({
foo: 1,
bar: 2,
})
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
~~ arrowFunction.noNewLineBeforeFnCall: true ~~
== should not insert new line after arrow when option is true ==
[].map((it) => test({
foo: 1,
bar: 2
}));

[expect]
[].map((it) => test({
foo: 1,
bar: 2,
}));

0 comments on commit 26fafef

Please sign in to comment.