5

When I comment out (M-x comment-dwim) some section of JSX code with the new js-mode in Emacs 27, it gives:

// <Grid rows={rows} columns={columns}>
//   <Table />
//   <TableHeaderRow />
// </Grid>

Is there any way to make js-mode behave like it does with rjsx-mode:

{/* <Grid rows={rows} columns={columns}> */}
{/*   <Table /> */}
{/*   <TableHeaderRow /> */}
{/* </Grid> */}

Supposing that this is not currently possible, and that js-mode is really the way to go forward for JS/JSX code (from the older rjsx-mode and js2-mode, as I vaguely understand), where would I go to study the code and possibly suggest a pull request to make it work?

Drew
  • 75,699
  • 9
  • 109
  • 225
cjauvin
  • 594
  • 4
  • 15

1 Answers1

1

It's fixed in emacs 28.
For emacs 27 you can copy function

(defun js-jsx--comment-region (beg end &optional arg)
  (if (or (js-jsx--context)
          (save-excursion
            (skip-chars-forward " \t")
            (js-jsx--looking-at-start-tag-p)))
      (let ((comment-start "{/* ")
            (comment-end " */}"))
        (comment-region-default beg end arg))
    (comment-region-default beg end arg)))

And use it like so

(add-hook 'js-mode-hook (lambda ()
                      (when (eq js-jsx-syntax t)
                        (setq-local comment-region-function #'js-jsx--comment-region))
                      ))
albert200000
  • 153
  • 4
  • I'm now using Emacs 28.1 and I have two problems with the new `js-mode` when I work on a JSX file: (1) since this code https://github.com/emacs-mirror/emacs/blob/bd67ffa1790b620b2beebdc32080d70b76e71029/lisp/progmodes/js.el#L3521 never seems to run (even though I see a `[JSX]` suffix in my modeline), the special JSX region commenting function is not used (i.e. it sill comments using `//` in a JSX section). – cjauvin Apr 14 '22 at 17:11
  • Second problem (2): when I activate the `js-jsx-mode` manually, the JSX region commenting is used, but then calling it through `comment-dwim` does not work with uncommenting, as it does everywhere else (i.e. I'm used to use `M-;` as a comment/uncomment toggle). – cjauvin Apr 14 '22 at 17:14