unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode
@ 2023-12-10 23:33 Noah Peart
  2023-12-12  1:14 ` Dmitry Gutov
  0 siblings, 1 reply; 2+ messages in thread
From: Noah Peart @ 2023-12-10 23:33 UTC (permalink / raw)
  To: 67758


[-- Attachment #1.1: Type: text/plain, Size: 1685 bytes --]

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'

[-- Attachment #1.2: Type: text/html, Size: 2023 bytes --]

[-- Attachment #2: js-ts-mode-statement-indent.patch --]
[-- Type: text/x-patch, Size: 3220 bytes --]

From 2faaff772677c44180df64b82a12b72fbd0c57cb Mon Sep 17 00:00:00 2001
From: Noah Peart <noah.v.peart@gmail.com>
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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode
  2023-12-10 23:33 bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode Noah Peart
@ 2023-12-12  1:14 ` Dmitry Gutov
  0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Gutov @ 2023-12-12  1:14 UTC (permalink / raw)
  To: Noah Peart, 67758-done

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.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-12  1:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-10 23:33 bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode Noah Peart
2023-12-12  1:14 ` Dmitry Gutov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).