-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provide rjsx-minor-mode #66
Conversation
Thanks for the PR! I like the idea, but would rather leverage the existing (defun rjsx-parse-xml-initializer (orig-fun)
"Dispatch the xml parser based on variable `rjsx-mode' being active or not.
This function is used to advise `js2-parse-xml-initializer' (ORIG-FUN) using
the `:around' combinator. JS2-PARSER is the original XML parser."
(if rjsx-jsx-enabled ;; <------------{ CHANGE THIS }------------
(rjsx-parse-top-xml)
(apply orig-fun nil))) Then simply enabling I'd also like to include a test for this before merging. An easy way to do this is to use |
If I got it right, should be something like:
then in my mode hook I can call Seems to work, but only if rjsx-mode was initialized before: is there a way to initialize it without something ugly like switching to rjsx-mode and then switch back to my-mode? |
I actuall meant putting the (if (or (eq major-mode 'rjsx-mode) rjsx-jsx-enabled-p)
(rjsx-parse-top-xml)
(apply orig-fun nil))
|
got it, a last minor concern is that currently the only autoload is the provided (rjsx-mode) that will actually enable the major mode. In the js2-minor-mode case I do not want to call that function of course, but at the same time would like to autoload only the needed part for jsx parsing so that can be deferred with use-package. Do you think that it would be possible to do it? maybe wrapping the advice-add part in a function? |
Regardless of what gets an autoload cookie, the entire file is loaded when the autoload is (defun rjsx-mode (&rest args)
(require 'rjsx-mode "path/to/rjsx-mode.el")
;; The require call overrides the defintion of `rjsx-mode'
(apply 'rjsx-mode args)) If you call require manually, you can forget about all the autload stuff. As long as you don't (That said, since
I'm not familiar with (defun my-enable-jsx-parsing ()
(interactive)
(js2-minor-mode 1)
(require 'rjsx-mode)
(setq rjsx-jsx-enabled t))
(defun my-disable-jsx-parsing ()
(interactive)
(js2-minor-mode -1)
(setq rjsx-jsx-enabled nil)) Or if you know you'll always want JSX parsing, even simpler: (with-eval-after-load 'js2-mode
(require 'rjsx)
(setq rjsx-jsx-enabled nil)) |
- rely on bool rjsx-jsx-enabled-p - autoload rjsx-enable-parser to allow defer package loading
basically with my latest commit I am able to load it like this:
with
this guarantees that the package gets loaded only when my-mode is activated, deferring it to the very moment (rjsx-enable-parser) is called. without the autoload it doesn't work with defer t and it will be required directly in the init. I'm not sure of the real performance benefit here, but I think the best practice would be to always allow the autoload (before was possible for rjsx-mode, but now we need a custom one only for the parsing or we are forced to require the entire thing in a blocking way). But I'm also not that much into the topic and may be missing something. |
in the end I think wouldn't change much as the require will happen in the hook and autoload will read the whole file anyway, I guess it is only a possible way to make it cleaner for refactors (e.g. separate one autoloaded function in a smaller file?) or to make it more explicit that you are requiring a specific function and not just blindly requiring the whole file. In this case it is pretty trivial, so if you want I can remove the autoload part and require it in the hook, or we can find a better function to export (e.g. having the autoloaded function to also set the boolean directly to have a single instruction before enabling js2-minor-mode?). I'm not familiar with elisp testing, can I just clone the whole repo and evaluate the test file buffer? |
@felipeochoa I'm not sure to get what do you want to test: nothing is new on parsing side, but should be tested that using js2-minor-mode when rjsx-jsx-enabled-p is true doesn't break any test and returns the same AST. from the test what I get is that it is just calling js2-mode and js2-reparse, while in the actual rjsx-tests.el it is doing:
should we add something like this?
and then use it in some tests or all of them again? |
@felipeochoa any news? I would like to use the new feature to enhance the react layer on spacemacs |
Sorry I thought I'd responded to this earlier. Just a test that does something like (I'm on my phone so can't look up the exact calls, but let me know if you run into issues) |
Looking forward to this one! 👏 |
any news on this? |
@felipeochoa I never used ert, but i tried to duplicate the js2 test helper for rjsx minor mode and seems to work, probably everything could be simplified... |
@axyz Also, thanks for this PR! This is great functionality to add -- so sorry it took so long to merge |
I'm not sure if this is the cleanest way to do it or if there may be unexpected side effect.
The idea is to provide a minor-mode similar to js2-minor-mode in order to allow the usage of packages depending on the js2/rjsx AST to work in other major modes (e.g. using js2-refactor in web-mode)
I tired it locally and seems to work with js2-refactor