Subject: bug#67758: closed (Re: bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 12 Dec 2023 01:16:01 +0000

Your bug report

#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
67758: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=67758
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode
From: Dmitry Gutov <[email-masked]>
To: Noah Peart <[email-masked]>, [email-masked]
Date: Tue, 12 Dec 2023 03:14:59 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Version: 29.2

On 11/12/2023 01:33, Noah Peart wrote:
> Tags: patch
> 
> 
> * Bug: `js-ts-mode` is missing indentation rules for bracketless
> statements.
> 
> These missing rules are the same as those that were previously missing
> from typescript-ts-mode (bug#67031).
> 
> Recipe to reproduce:
> 
> Using the following function to configure js-ts-mode and indent the
> buffer:
> 
>      (defun try-indent ()
>        (interactive)
>        (setq-local indent-tabs-mode nil)
>        (setq-local js-indent-level 2)
>        (js-ts-mode)
>        (indent-region (point-min) (point-max)))
> 
> Add the following example to a buffer and call `try-indent`.
> 
>      function bracketless_statements(x) {
>      if (x == 0)
>      console.log("if_statement");
>      else if (x == 1)
>      console.log("if_statement");
>      else
>      console.log("else_clause");
>      for (let i = 0; i < 1; i++)
>      console.log("for_statement");
>      for (let _ of [true])
>      console.log("for_in_statement");
>      while (x-- > 0)
>      console.log("while_statement");
>      do
>      console.log("do_statement");
>      while (false)
>      };
> 
> Afterwards, you should see none of the statement bodies were indented.

Thanks! Installed.

Subject: [PATCH] Add indentation rules for bracketless statements in js-ts-mode
From: Noah Peart <[email-masked]>
To: [email-masked]
Date: Sun, 10 Dec 2023 15:33:03 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

* Bug: `js-ts-mode` is missing indentation rules for bracketless
statements.

These missing rules are the same as those that were previously missing
from typescript-ts-mode (bug#67031).

Recipe to reproduce:

Using the following function to configure js-ts-mode and indent the
buffer:

    (defun try-indent ()
      (interactive)
      (setq-local indent-tabs-mode nil)
      (setq-local js-indent-level 2)
      (js-ts-mode)
      (indent-region (point-min) (point-max)))

Add the following example to a buffer and call `try-indent`.

    function bracketless_statements(x) {
    if (x == 0)
    console.log("if_statement");
    else if (x == 1)
    console.log("if_statement");
    else
    console.log("else_clause");
    for (let i = 0; i < 1; i++)
    console.log("for_statement");
    for (let _ of [true])
    console.log("for_in_statement");
    while (x-- > 0)
    console.log("while_statement");
    do
    console.log("do_statement");
    while (false)
    };

Afterwards, you should see none of the statement bodies were indented.

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.33, cairo version 1.16.0) of 2023-12-10 built on noah-X580VD
Repository revision: 0da2a4650cdac008ac9a50ec8a7729093632a6a8
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.3 LTS

Configured using:
 'configure --prefix=/usr/local --with-modules --with-tree-sitter
--with-threads --with-x-toolkit=gtk3 --with-xwidgets --with-gnutls
--with-json --with-mailutils --with-jpeg --with-png --with-rsvg
--with-tiff --with-xml2 --with-xpm --with-imagemagick CC=gcc-12
CXX=gcc-12'
From 2faaff772677c44180df64b82a12b72fbd0c57cb Mon Sep 17 00:00:00 2001
From: Noah Peart <[email-masked]>
Date: Sun, 10 Dec 2023 14:58:31 -0800
Subject: Add indentation rules for bracketless statements in js-ts-mode

* lisp/progmodes/js.el (js--treesit-indent-rules): Add indentation
rules to handle bracketless statements.
* test/lisp/progmodes/js-tests.el (js-ts-mode-test-indentation):
New test for js-ts-mode indentation.
* test/lisp/progmodes/js-resources/js-ts-indents.erts: New file
with indentation tests for js-ts-mode.
---
 lisp/progmodes/js.el                          |  5 +++
 .../progmodes/js-resources/js-ts-indents.erts | 44 +++++++++++++++++++
 test/lisp/progmodes/js-tests.el               |  6 +++
 3 files changed, 55 insertions(+)
 create mode 100644 test/lisp/progmodes/js-resources/js-ts-indents.erts

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 44eda03f7ce..cf6401d9e38 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3463,6 +3463,11 @@ js--treesit-indent-rules
        ((parent-is "class_body") parent-bol js-indent-level)
        ((parent-is ,switch-case) parent-bol js-indent-level)
        ((parent-is "statement_block") parent-bol js-indent-level)
+       ((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 js-indent-level)
 
        ;; JSX
        ,@(js-jsx--treesit-indent-compatibility-bb1f97b)
diff --git a/test/lisp/progmodes/js-resources/js-ts-indents.erts b/test/lisp/progmodes/js-resources/js-ts-indents.erts
new file mode 100644
index 00000000000..2e34b23acef
--- /dev/null
+++ b/test/lisp/progmodes/js-resources/js-ts-indents.erts
@@ -0,0 +1,44 @@
+Code:
+  (lambda ()
+    (setq indent-tabs-mode nil)
+    (setq js-indent-level 2)
+    (js-ts-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: Basic indentation
+
+=-=
+const foo = () => {
+  console.log("bar");
+  if (x) {
+    return y;
+  } else if (y) {
+    return u;
+  }
+  return baz.x()
+    ? true
+    : false;
+}
+=-=-=
+
+Name: Statement indentation without braces
+
+=-=
+function bracketless_statements(x) {
+  if (x == 0)
+    console.log("if_statement");
+  else if (x == 1)
+    console.log("if_statement");
+  else
+    console.log("else_clause");
+  for (let i = 0; i < 1; i++)
+    console.log("for_statement");
+  for (let _ of [true])
+    console.log("for_in_statement");
+  while (x-- > 0)
+    console.log("while_statement");
+  do
+    console.log("do_statement");
+  while (false)
+};
+=-=-=
diff --git a/test/lisp/progmodes/js-tests.el b/test/lisp/progmodes/js-tests.el
index 5db92b08f8a..827d7bb8a99 100644
--- a/test/lisp/progmodes/js-tests.el
+++ b/test/lisp/progmodes/js-tests.el
@@ -288,6 +288,12 @@ js-mode-end-of-defun
     ;; end-of-defun should move point to eob.
     (should (eobp))))
 
+;;;; Tree-sitter tests.
+
+(ert-deftest js-ts-mode-test-indentation ()
+  (skip-unless (treesit-ready-p 'javascript))
+  (ert-test-erts-file (ert-resource-file "js-ts-indents.erts")))
+
 (provide 'js-tests)
 
 ;;; js-tests.el ends here
-- 
2.34.1