all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Noah Peart <noah.v.peart@gmail.com>
To: 67758@debbugs.gnu.org
Subject: bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode
Date: Sun, 10 Dec 2023 15:33:03 -0800	[thread overview]
Message-ID: <CAPVBTSfg-7uC12DJhGCTKvALN-sumUFB-jimoZFSg502yq_U8Q@mail.gmail.com> (raw)


[-- 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


             reply	other threads:[~2023-12-10 23:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-10 23:33 Noah Peart [this message]
2023-12-12  1:14 ` bug#67758: [PATCH] Add indentation rules for bracketless statements in js-ts-mode Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPVBTSfg-7uC12DJhGCTKvALN-sumUFB-jimoZFSg502yq_U8Q@mail.gmail.com \
    --to=noah.v.peart@gmail.com \
    --cc=67758@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.