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

Issue:
Single line block with const variable has curlies removed
**Describe the bug**

dprint-plugin-typescript version: 0.84.0

**Input Code**

```ts
if (typeof err !== 'undefined') { const _err: Error = err; }
```

**Expected Output**

```ts
if (typeof err !== 'undefined') { const _err: Error = err; }
```

**Actual Output**

```ts
if (typeof err !== 'undefined') const _err: Error = err;
```

https://dprint.dev/playground/#code/JYMwBAFALgngDgUwPbgQJzWAhAXh2AcgFcA7AEwRGBITIIEowBvMAYyRIGcowB9dNAC4wAUQxJM+AQG4wAXyA/language/typescript


Pull request description:
fix: keep braces on if & while statements when block body contains single decl
Closes #510

Patch:
diff --git a/src/generation/generate.rs b/src/generation/generate.rs
index 693993c..ae5fd32 100644
--- a/src/generation/generate.rs
+++ b/src/generation/generate.rs
@@ -8435,7 +8435,9 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
 
   fn get_force_braces(body_node: Node) -> bool {
     if let Node::BlockStmt(body_node) = body_node {
-      body_node.stmts.is_empty() || body_node.stmts.iter().all(|s| s.kind() == NodeKind::EmptyStmt)
+      body_node.stmts.is_empty()
+        || body_node.stmts.iter().all(|s| s.kind() == NodeKind::EmptyStmt)
+        || (body_node.stmts.len() == 1 && matches!(body_node.stmts[0], Stmt::Decl(_)))
     } else {
       false
     }


Test patch:
diff --git a/tests/specs/issues/issue0510.txt b/tests/specs/issues/issue0510.txt
new file mode 100644
index 0000000..edf7ab8
--- /dev/null
+++ b/tests/specs/issues/issue0510.txt
@@ -0,0 +1,11 @@
+== should not remove braces in an if/while statement when the body contains a declaration ==
+if (true) { const _err = 5; }
+while (true) { const _err = 5; }
+if (true) { class Test {} }
+while (true) { class Test {} }
+
+[expect]
+if (true) { const _err = 5; }
+while (true) { const _err = 5; }
+if (true) { class Test {} }
+while (true) { class Test {} }