Subject: bug#77901: tsx-ts-mode: ternary chains are indented as a tree

From: Konstantin Kharlamov <[email-masked]>
Date: Thu, 01 May 2025 20:39:02 +0700

After poking around I wrote a patch (attached). Worth noting though, for
some reason it doesn't work for ternary without starting colon, e.g.:

    const a = cond1 ? 1 :
      cond2 ? 2 :
      cond3 ? 3 : 4;

I'm unclear why, per my understanding the "standalone-parent" of the
whole ternary chain (disregarding where the point is) is the `const a
=` declaration.

Such bug also persists in c++-ts-mode (as opposed to non-ts plain c++-
mode). I'm not sure if it's me misunderstanding something or not…
From 0aff1cf59eff8f94c72bc0c383d6089185357e66 Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <[email-masked]>
Date: Thu, 1 May 2025 20:28:29 +0700
Subject: [PATCH] typescript-ts: align ternary-chain branches (bug#78187)

lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules):
make sure each new ternary branch is aligned with the previous one.
test/lisp/progmodes/typescript-ts-mode-resources/indent.erts (Chained
ternary expressions): New test.
---
 lisp/progmodes/typescript-ts-mode.el                     | 2 +-
 .../progmodes/typescript-ts-mode-resources/indent.erts   | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index d9bb1482d33..89f943d4a8c 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -162,7 +162,7 @@ typescript-ts-mode--indent-rules
      ((and (parent-is "comment") c-ts-common-looking-at-star)
       c-ts-common-comment-start-after-first-star -1)
      ((parent-is "comment") prev-adaptive-prefix 0)
-     ((parent-is "ternary_expression") parent-bol typescript-ts-mode-indent-offset)
+     ((parent-is "ternary_expression") standalone-parent 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") standalone-parent 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 42aeb3c539b..210bfcabd41 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -93,6 +93,15 @@ const foo = () => {
 };
 =-=-=
 
+Name: Chained ternary expressions
+
+=-=
+const a = cond1 ? 1
+  : cond2 ? 2
+  : cond3 ? 3
+  : 4;
+=-=-=
+
 Code:
   (lambda ()
     (setq tsx-ts-mode-indent-offset 2)
-- 
2.49.0