Subject: bug#78121: tsx-ts-mode: wrong indentation of body with misindented args

From: Konstantin Kharlamov <[email-masked]>
Date: Tue, 29 Apr 2025 22:21:37 +0700

I wrote a fix and added a test, please review.

I did not touch the situation discussed in P.S. (should I maybe create
a separate issue for that?), this only fixes the problem reported.
From 7912d4e176f057e48816300ad84c5dbf230b9926 Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <[email-masked]>
Date: Tue, 29 Apr 2025 21:51:18 +0700
Subject: [PATCH] typescript-ts: don't misindent body if params are unaligned
 (bug#78121)

Older code was calculating body indentation depending on function
parameters alignment.  This is incorrect, because if parameters are
misaligned, so will the function body.

lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules):
Stop depending on function parameters indentation for calculating body
content and the closing `}'.
test/lisp/progmodes/typescript-ts-mode-resources/indent.erts (Function
body with params misundented (bug#78121)): Add new test.
---
 lisp/progmodes/typescript-ts-mode.el          |  4 ++--
 .../typescript-ts-mode-resources/indent.erts  | 20 +++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index b1c2f0af6f4..44b92029837 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -121,7 +121,7 @@ typescript-ts-mode--indent-rules
   (typescript-ts-mode--check-dialect language)
   `((,language
      ((parent-is "program") column-0 0)
-     ((node-is "}") parent-bol 0)
+     ((node-is "}") standalone-parent 0)
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
      ((node-is ">") parent-bol 0)
@@ -131,7 +131,7 @@ typescript-ts-mode--indent-rules
      ((parent-is "ternary_expression") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "member_expression") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "named_imports") parent-bol typescript-ts-mode-indent-offset)
-     ((parent-is "statement_block") parent-bol typescript-ts-mode-indent-offset)
+     ((parent-is "statement_block") standalone-parent typescript-ts-mode-indent-offset)
      ((or (node-is "case")
           (node-is "default"))
       parent-bol typescript-ts-mode-indent-offset)
diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
index 1305029a9e1..d8136c24b9e 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -173,3 +173,23 @@ interface Foo {
   bar?: boolean;
 }
 =-=-=
+
+Code:
+  (lambda ()
+    (setq tsx-ts-mode-indent-offset 2)
+    (tsx-ts-mode)
+    (setq indent-tabs-mode nil)
+    (indent-region (line-beginning-position 5) (point-max)))
+
+Name: Function body with params misundented (bug#78121)
+
+=-=
+const foo = (_: string,
+        __: number) => {
+    const bar = (_: string,
+            __: number) => {
+      return;
+    }
+  return;
+}
+=-=-=
-- 
2.49.0