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

From: Konstantin Kharlamov <[email-masked]>
Date: Thu, 01 May 2025 14:55:18 +0700

On Thu, 2025-05-01 at 00:40 -0700, Yuan Fu wrote:
> 
> 
> > On Apr 29, 2025, at 11:10 AM, Konstantin Kharlamov
> > <[email-masked]> wrote:
> > 
> > On Tue, 2025-04-29 at 22:38 +0700, Konstantin Kharlamov wrote:
> > > On Tue, 2025-04-29 at 22:21 +0700, Konstantin Kharlamov wrote:
> > > > 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.
> > > 
> > > v2: improved the test to cover potential edge case, clarified
> > > description.
> > 
> > v3: typo in test name
> > <1.patch>
> 
> Thank you very much! I see your point. But could you make the test
> simpler, so it’s easier to see the intent of the test? 

Sure, which part do you find worth improving?  I did a few amendments
(attached), which might make the test prettier, will be glad to hear if
I can improve it further.  That aside, I am hopeful the test
description provides the information about test purpose.

v4: 1. made parameter names alphabetic (and thus equally sized), 2.
aligned every 2nd param to the 1st one. Initially I avoided this on
purpose, but while writing an email I figured my reasoning missed a
point, aligning them is fine.
From 53614ed4b4cb130c433555719f1057a440a60254 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 misaligned
 (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.  Instead, use offset of the
previous standalone parent.

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 misindented (bug#78121)): Add new test.
---
 lisp/progmodes/typescript-ts-mode.el          |  4 +--
 .../typescript-ts-mode-resources/indent.erts  | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index e0458b3192e..d9bb1482d33 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -155,7 +155,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)
@@ -165,7 +165,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..42aeb3c539b 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -173,3 +173,28 @@ 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 7) (point-max)))
+
+Name: Function body with params misindented (bug#78121)
+
+=-=
+const f1 = (a1: string,
+            a2: number) => {
+    const f2 = (a1: string,
+                a2: number) => {
+      const f3 = (a1: string,
+                  a2: number) =>
+        {
+          return;
+        }
+      return;
+    }
+  return;
+}
+=-=-=
-- 
2.49.0