Subject: bug#67031: [PATCH] Fix typescript-ts-mode indentation in unbracketed statements

From: Noah Peart <[email-masked]>
Date: Mon, 20 Nov 2023 10:13:23 -0800

Sorry, ignore the previous patch.  A couple additional rules were needed to
handle
"while" and "else".

On Mon, Nov 20, 2023 at 10:07 AM Noah Peart <[email-masked]> wrote:

> Here is an updated patch with the added "else_clause" and indentation test
> for
> statements without braces.
>
> On Mon, Nov 20, 2023 at 4:55 AM Noah Peart <[email-masked]> wrote:
>
>> Yea, I'm using the same.  But, would it be useful to have a way to set
>> that when
>> running tests without needing to temporarily add set
>> `treesit-extra-load-path` in
>> the test file?
>>
>> On Sun, Nov 19, 2023 at 5:38 AM Dmitry Gutov <[email-masked]> wrote:
>>
>>> On 19/11/2023 08:08, Eli Zaretskii wrote:
>>> >> Date: Sun, 19 Nov 2023 03:24:49 +0200
>>> >> Cc:[email-masked],[email-masked]
>>> >> From: Dmitry Gutov<[email-masked]>
>>> >>
>>> >> On 19/11/2023 02:12, Noah Peart wrote:
>>> >>> Yea, I can do that.
>>> >>>
>>> >>> Is there a recipe for running the tests with a tree-sitter library
>>> path?
>>> >> You mean with the default tree-sitter librayr path overridden? I'm
>>> not sure.
>>> > Why would an ERT test need that?  We are supposed to test Emacs with
>>> > the otherwise installed system features.  As tree-sitter grammars are
>>> > not part of Emacs, there should be no need to tell Emacs to use a
>>> > grammar library other than the one installed on the system.  Right?
>>>
>>> We have a way to override that with the variable
>>> treesit-extra-load-path, so it follows that some users will take
>>> advantage of it to add extra directories.
>>>
>>> I'm using ~/.emacs.d/tree-sitter/ myself.
>>>
>>
From 2628e92f6c55fb69d9f504caee1e1b105e30e01a Mon Sep 17 00:00:00 2001
From: nverno <[email-masked]>
Date: Mon, 20 Nov 2023 10:04:51 -0800
Subject: [PATCH] Fix typescript-ts-mode indentation in statements without
 braces

---
 lisp/progmodes/typescript-ts-mode.el          |  5 +++++
 .../typescript-ts-mode-resources/indent.erts  | 22 +++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index b976145dbf3..b6d5495adbb 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -124,6 +124,11 @@ typescript-ts-mode--indent-rules
      ((parent-is "arrow_function") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "parenthesized_expression") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "binary_expression") parent-bol typescript-ts-mode-indent-offset)
+     ((match "while" "do_statement") parent-bol 0)
+     ((match "else" "if_statement") parent-bol 0)
+     ((parent-is ,(rx (or (seq (or "if" "for" "for_in" "while" "do") "_statement")
+                          "else_clause")))
+      parent-bol typescript-ts-mode-indent-offset)
 
      ,@(when (eq language 'tsx)
 	 (append (tsx-ts-mode--indent-compatibility-b893426)
diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
index 146ee76574e..20f423259b4 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -23,6 +23,28 @@ const foo = () => {
 }
 =-=-=
 
+Name: Statement indentation without braces
+
+=-=
+const foo = () => {
+  if (true)
+    console.log("if_statement");
+  else if (false)
+    console.log("if_statement");
+  else
+    console.log("else_clause");
+  for (let i = 0; i < 1; i++)
+    console.log("for_statement");
+  for (let i of [true])
+    console.log("for_in_statement");
+  while (false)
+    console.log("while_statement");
+  do
+    console.log("do_statement");
+  while (false)
+};
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
-- 
2.34.1