Repository: dprint/dprint-plugin-typescript (rust)

Issue:
If body always moves to new line when condition spans multiple lines
**Describe the bug**

dprint-plugin-typescript version: 0.68.0

I set everything to "maintain", but when this if statement gets reprinted, the return moves to a new line separate from the closing `)` of the if statement condition, as though the "maintain" setting checks to see if the body is on a new line via checking the location of the keyword rather than the text that precedes it.

Config:

```json
{
  "indentWidth": 4,
  "lineWidth": 120,
  "newLineKind": "auto",
  "quoteStyle": "preferDouble",
  "useBraces": "maintain",
  "operatorPosition": "maintain",
  "nextControlFlowPosition": "maintain",
  "bracePosition": "maintain",
  "singleBodyPosition": "maintain"
}
```

**Input Code**

```ts
if (someFunction({ xyz: 1234 })) return;

if (someFunction({
    xyz: 1234
})) return;
```

**Expected Output**

```ts
if (someFunction({ xyz: 1234 })) return;

if (
    someFunction({
        xyz: 1234,
    })
) return;
```

or

```ts
if (someFunction({ xyz: 1234 })) return;

if (someFunction({
    xyz: 1234
})) return;
```

**Actual Output**

```ts
if (someFunction({ xyz: 1234 })) return;

if (
    someFunction({
        xyz: 1234,
    })
)
    return;
```


Pull request description:
Ensure if body stays on same line when condition is multiline
Fixes #359.

I'm a noob in this codebase, let me know if I've missed something obvious!

Patch:
diff --git a/src/generation/generate.rs b/src/generation/generate.rs
index 74ad5ed..0f50506 100644
--- a/src/generation/generate.rs
+++ b/src/generation/generate.rs
@@ -703,13 +703,11 @@ fn gen_catch_clause<'a>(node: &'a CatchClause, context: &mut Context<'a>) -> Pri
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.body.into(),
         use_braces: UseBraces::Always,
         brace_position: context.config.try_statement_brace_position,
         single_body_position,
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: Some((start_header_ln, start_header_lsil)),
         end_header_info: Some(end_header_ln),
       },
@@ -4455,13 +4453,11 @@ fn gen_for_stmt<'a>(node: &'a ForStmt, context: &mut Context<'a>) -> PrintItems
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.body.into(),
         use_braces: context.config.for_statement_use_braces,
         brace_position: context.config.for_statement_brace_position,
         single_body_position: Some(context.config.for_statement_single_body_position),
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: Some((start_header_ln, start_header_lsil)),
         end_header_info: Some(end_header_ln),
       },
@@ -4524,13 +4520,11 @@ fn gen_for_in_stmt<'a>(node: &'a ForInStmt, context: &mut Context<'a>) -> PrintI
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.body.into(),
         use_braces: context.config.for_in_statement_use_braces,
         brace_position: context.config.for_in_statement_brace_position,
         single_body_position: Some(context.config.for_in_statement_single_body_position),
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: Some((start_header_ln, start_header_lsil)),
         end_header_info: Some(end_header_ln),
       },
@@ -4584,13 +4578,11 @@ fn gen_for_of_stmt<'a>(node: &'a ForOfStmt, context: &mut Context<'a>) -> PrintI
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.body.into(),
         use_braces: context.config.for_of_statement_use_braces,
         brace_position: context.config.for_of_statement_brace_position,
         single_body_position: Some(context.config.for_of_statement_single_body_position),
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: Some((start_header_ln, start_header_lsil)),
         end_header_info: Some(end_header_ln),
       },
@@ -4608,7 +4600,6 @@ fn gen_if_stmt<'a>(node: &'a IfStmt, context: &mut Context<'a>) -> PrintItems {
   let cons_range = cons.range();
   let result = gen_header_with_conditional_brace_body(
     GenHeaderWithConditionalBraceBodyOptions {
-      parent: node.range(),
       body_node: cons.into(),
       generated_header: {
         let mut items = PrintItems::new();
@@ -4683,13 +4674,11 @@ fn gen_if_stmt<'a>(node: &'a IfStmt, context: &mut Context<'a>) -> PrintItems {
       items.extend(
         gen_conditional_brace_body(
           GenConditionalBraceBodyOptions {
-            parent: node.range(),
             body_node: alt.into(),
             use_braces: context.config.if_statement_use_braces,
             brace_position: context.config.if_statement_brace_position,
             single_body_position: Some(context.config.if_statement_single_body_position),
             requires_braces_condition_ref: Some(result.open_brace_condition_ref),
-            header_start_token: Some(else_keyword),
             start_header_info: Some((start_else_header_ln, start_else_header_lsil)),
             end_header_info: None,
           },
@@ -4881,13 +4870,11 @@ fn gen_try_stmt<'a>(node: &'a TryStmt, context: &mut Context<'a>) -> PrintItems
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.block.into(),
         use_braces: UseBraces::Always, // braces required
         brace_position: context.config.try_statement_brace_position,
         single_body_position: Some(SingleBodyPosition::NextLine),
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: None,
         end_header_info: None,
       },
@@ -4927,13 +4914,11 @@ fn gen_try_stmt<'a>(node: &'a TryStmt, context: &mut Context<'a>) -> PrintItems
     items.extend(
       gen_conditional_brace_body(
         GenConditionalBraceBodyOptions {
-          parent: node.range(),
           body_node: finalizer.into(),
           use_braces: UseBraces::Always, // braces required
           brace_position,
           single_body_position: Some(SingleBodyPosition::NextLine),
           requires_braces_condition_ref: None,
-          header_start_token: None,
           start_header_info: None,
           end_header_info: None,
         },
@@ -5055,13 +5040,11 @@ fn gen_while_stmt<'a>(node: &'a WhileStmt, context: &mut Context<'a>) -> PrintIt
   items.extend(
     gen_conditional_brace_body(
       GenConditionalBraceBodyOptions {
-        parent: node.range(),
         body_node: node.body.into(),
         use_braces: context.config.while_statement_use_braces,
         brace_position: context.config.while_statement_brace_position,
         single_body_position: Some(context.config.while_statement_single_body_position),
         requires_braces_condition_ref: None,
-        header_start_token: None,
         start_header_info: Some((start_header_ln, start_header_lsil)),
         end_header_info: Some(end_header_ln),
       },
@@ -7783,7 +7766,6 @@ fn gen_control_flow_separator(
 }
 
 struct GenHeaderWithConditionalBraceBodyOptions<'a> {
-  parent: SourceRange,
   body_node: Node<'a>,
   generated_header: PrintItems,
   use_braces: UseBraces,
@@ -7814,13 +7796,11 @@ fn gen_header_with_conditional_brace_body<'a>(
   items.push_info(end_header_ln);
   let result = gen_conditional_brace_body(
     GenConditionalBraceBodyOptions {
-      parent: opts.parent,
       body_node: opts.body_node,
       use_braces: opts.use_braces,
       brace_position: opts.brace_position,
       single_body_position: opts.single_body_position,
       requires_braces_condition_ref: opts.requires_braces_condition_ref,
-      header_start_token: None,
       start_header_info: Some((start_header_ln, start_header_lsil)),
       end_header_info: Some(end_header_ln),
     },
@@ -7836,13 +7816,11 @@ fn gen_header_with_conditional_brace_body<'a>(
 }
 
 struct GenConditionalBraceBodyOptions<'a> {
-  parent: SourceRange,
   body_node: Node<'a>,
   use_braces: UseBraces,
   brace_position: BracePosition,
   single_body_position: Option<SingleBodyPosition>,
   requires_braces_condition_ref: Option<ConditionReference>,
-  header_start_token: Option<&'a TokenAndSpan>,
   start_header_info: Option<(LineNumber, LineStartIndentLevel)>,
   end_header_info: Option<LineNumber>,
 }
@@ -7870,8 +7848,6 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
     opts.body_node,
     body_should_be_multi_line,
     &opts.single_body_position,
-    &opts.header_start_token,
-    &opts.parent,
     context,
   );
   let open_brace_token = get_open_brace_token(opts.body_node, context);
@@ -7903,12 +7879,12 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
       if should_use_new_line {
         return Some(true);
       }
-      let start_header_ln = condition_context.resolved_line_number(start_header_ln?)?;
-      if start_header_ln < condition_context.writer_info.line_number {
+      let end_header_ln = condition_context.resolved_line_number(end_header_ln?)?;
+      if end_header_ln < condition_context.writer_info.line_number {
         return Some(true);
       }
       let resolved_end_statements_ln = condition_context.resolved_line_number(end_statements_lc.line)?;
-      Some(resolved_end_statements_ln > start_header_ln)
+      Some(resolved_end_statements_ln > end_header_ln)
     }),
     Signal::NewLine.into(),
   );
@@ -8091,8 +8067,6 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
     body_node: Node,
     body_should_be_multi_line: bool,
     single_body_position: &Option<SingleBodyPosition>,
-    header_start_token: &Option<&'a TokenAndSpan>,
-    parent: &SourceRange,
     context: &mut Context<'a>,
   ) -> bool {
     if body_should_be_multi_line {
@@ -8100,14 +8074,14 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
     }
     if let Some(single_body_position) = single_body_position {
       return match single_body_position {
-        SingleBodyPosition::Maintain => get_body_stmt_start_line(body_node, context) > get_header_start_line(header_start_token, parent, context),
+        SingleBodyPosition::Maintain => get_body_stmt_start_line(body_node, context) > body_node.previous_token_fast(context.program).start_line_fast(context.program),
         SingleBodyPosition::NextLine => true,
         SingleBodyPosition::SameLine => {
           if let Node::BlockStmt(block_stmt) = body_node {
             if block_stmt.stmts.len() != 1 {
               return true;
             }
-            return get_body_stmt_start_line(body_node, context) > get_header_start_line(header_start_token, parent, context);
+            return get_body_stmt_start_line(body_node, context) > body_node.previous_token_fast(context.program).start_line_fast(context.program);
           }
           return false;
         }
@@ -8130,13 +8104,6 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
       }
       body_node.start_line_fast(context.program)
     }
-
-    fn get_header_start_line<'a>(header_start_token: &Option<&'a TokenAndSpan>, parent: &SourceRange, context: &mut Context<'a>) -> usize {
-      if let Some(header_start_token) = header_start_token {
-        return header_start_token.start_line_fast(context.program);
-      }
-      parent.start_line_fast(context.program)
-    }
   }
 
   fn get_body_should_be_multi_line<'a>(body_node: Node<'a>, header_trailing_comments: &[&'a Comment], context: &mut Context<'a>) -> bool {


Test patch:
diff --git a/tests/specs/issues/issue0359.txt b/tests/specs/issues/issue0359.txt
new file mode 100644
index 0000000..82b3f5a
--- /dev/null
+++ b/tests/specs/issues/issue0359.txt
@@ -0,0 +1,54 @@
+~~ singleBodyPosition: maintain, useBraces: maintain ~~
+== should not add newline for single line body and multi line condition ==
+if (someFunction({
+    xyz: 1234
+})) return;
+
+for (let x = 0; someFunction({
+    xyz: 1234
+}); x++) return;
+
+for (const x in someFunction({
+    xyz: 1234
+})) return;
+
+for (const x of someFunction({
+    xyz: 1234
+})) return;
+
+while (someFunction({
+    xyz: 1234
+})) return;
+
+[expect]
+if (
+    someFunction({
+        xyz: 1234,
+    })
+) return;
+
+for (
+    let x = 0;
+    someFunction({
+        xyz: 1234,
+    });
+    x++
+) return;
+
+for (
+    const x in someFunction({
+        xyz: 1234,
+    })
+) return;
+
+for (
+    const x of someFunction({
+        xyz: 1234,
+    })
+) return;
+
+while (
+    someFunction({
+        xyz: 1234,
+    })
+) return;