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?

2 Answers 2

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))
                      ))
3
  • 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 github.com/emacs-mirror/emacs/blob/… 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, 2022 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, 2022 at 17:14
  • As far I know, only rjsx-mode and web-mode support uncommenting JSX region as of now. js-jsx-mode implementation of JSX comments is incomplete. It should probably reimplement comment-dwim and add a js-jsx--uncomment-region function a to fully support this functionality. Sep 22, 2023 at 7:54
0

I have written the jtsx package which addresses this issue.

Commenting and uncommenting JSX code is fully supported and some additional features are provided.

Note that it is implemented on top of js-ts-mode and tsx-ts-mode, which use tree-sitter code parser and are only available starting Emacs 29.1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.