Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arrowFunction.noNewLineBeforeFnCall option #528

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(_) | 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),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
`;
Original file line number Diff line number Diff line change
@@ -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;
`;