]> git.eshelyaron.com Git - emacs.git/commitdiff
typescript-ts-mode: Improve function body indentation (bug#78121)
authorKonstantin Kharlamov <Hi-Angel@yandex.ru>
Tue, 29 Apr 2025 14:51:18 +0000 (21:51 +0700)
committerEshel Yaron <me@eshelyaron.com>
Sat, 7 Jun 2025 19:52:52 +0000 (21:52 +0200)
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.

(cherry picked from commit 1d2ae31b8bcca5f00c3c707cc7af3a347749c332)

lisp/progmodes/typescript-ts-mode.el
test/lisp/progmodes/typescript-ts-mode-resources/indent.erts

index 525bda2a33f60e1a0aeb890dc89d051067b03f65..19cbbdf8d61f387bcf2f441a24bcbd12a0d78c79 100644 (file)
@@ -155,7 +155,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
   (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 @@ Argument LANGUAGE is either `typescript' or `tsx'."
      ((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") 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)
index e8b1d57f1327aba448f0bf9a5f364ac8dcf1e052..210bfcabd411e49c862f1d221d8b173bbce3d2b6 100644 (file)
@@ -182,3 +182,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;
+}
+=-=-=