bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#25904: Formatting bug with js-mode


From: Felipe Ochoa
Subject: bug#25904: Formatting bug with js-mode
Date: Mon, 20 Nov 2017 17:22:14 -0500

Hi Dmitry!

I think the code is reasonable, but the patch needs a few examples for emacs/test/manual/js*.js. Maybe add a new file, or maybe reuse an existing one.

I've added a test to js.js in the latest patch below.
It also wouldn't hurt that the existing examples are unchanged with the new code.

I checked all the existing examples manually and all were unchanged when applying indent-region on the entire buffer.

To allow comments between the opening paren and the arglist? Does anybody write them there?

Oops -- this comment was supposed to be after the arrow. I was thinking of return type annotation comments, but I just checked flow and I don't think they support this. We can just remove the comment

I imagine this (*) could be transformed into a single regexp, though it would be pretty complex (rx could help, though!).

(*) Looking at an opening paren followed by ([optional] lambda arglist and an arrow) then [optional] comment and newline.

Is there an arglist regexp already in use somewhere? I'd rather not roll my own since dealing with default argument values and spreads and such seems quite challenging.

If it's not one regexp, moving some of the new code into a helper function (with a sensible name) seems prudent.

I've done this in the latest patch.

---
lisp/progmodes/js.el     | 26 ++++++++++++++++++++++++--
test/manual/indent/js.js |  9 +++++++++
2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 1f86909..6e057b0 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -1848,7 +1848,9 @@ This performs fontification according to 
`js--class-styles'."
             (skip-chars-backward " \t")
             (or (bobp) (backward-char))
             (and (> (point) (point-min))
-                  (save-excursion (backward-char) (not (looking-at "[/*]/")))
+                  ;; Need to exclude => here since js--looking-at-operator-p 
thinks
+                  ;; it's looking at an assignment operator
+                  (save-excursion (backward-char) (not (looking-at 
"[/*]/\\|=>")))
                  (js--looking-at-operator-p)
                  (and (progn (backward-char)
                              (not (looking-at "+\\+\\|--\\|/[/*]"))))))))))
@@ -2077,6 +2079,21 @@ indentation is aligned to that column."
        (when comma-p
          (goto-char (1+ declaration-keyword-end))))))))

+(defun js--looking-at-broken-arrow-function-p ()
+  "Helper function for `js--proper-indentation'.
+Return t if point is at the start of a (possibly async) arrow
+function and the last non-comment, non-whitespace token of the
+current like is the \"=>\" token."
+  (and (looking-at "\\s-*\\(async\\s-*\\)?")
+       (save-excursion
+         (goto-char (match-end 0))
+         (cond
+          ((eq (char-after) ?\()
+           (forward-list) ; Is this better than forward-sexp?
+           (looking-at-p "\\s-*=>\\s-*\\(/[/*]\\|$\\)"))
+          (t (looking-at-p
+              (concat js--name-re "\\s-*=>\\s-*\\(/[/*]\\|$\\)")))))))
+
(defun js--proper-indentation (parse-status)
  "Return the proper indentation for the current line."
  (save-excursion
@@ -2107,7 +2124,12 @@ indentation is aligned to that column."
                 (continued-expr-p (js--continued-expression-p)))
             (goto-char (nth 1 parse-status)) ; go to the opening char
             (if (or (not js-indent-align-list-continuation)
-                     (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)"))
+                     (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
+                     (when (eq (char-after) ?\()
+                       (save-excursion
+                         (forward-char)
+                         (skip-syntax-forward " ")
+                         (js--looking-at-broken-arrow-function-p))))
                 (progn ; nothing following the opening paren/bracket
                   (skip-syntax-backward " ")
                   (when (eq (char-before) ?\)) (backward-list))
diff --git a/test/manual/indent/js.js b/test/manual/indent/js.js
index b0d8bca..2286cc1 100644
--- a/test/manual/indent/js.js
+++ b/test/manual/indent/js.js
@@ -144,6 +144,15 @@ bar(
  /abc/
)

+// #25904
+foo.bar.baz(very =>
+  very
+).biz(([baz={a: [123]}, boz]) =>
+  baz
+).snarf((snorf) =>
+  snorf
+);
+
// Local Variables:
// indent-tabs-mode: nil
// js-indent-level: 2
--
2.7.4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]