emacsstandardjs

How can I add StandardJs to format-all on save on Doom Emacs?


I see that in the current version of format-all they have support for standard-js but unfortunately this is not the case for doom emacs, where only prettier is available.

I have tried grabbing the definition on their source code and modifying it to fit on doom's modification as:

(define-format-all-formatter standard
  (:executable "standard")
  (:install "npm install --global standard")
  (:modes
   (angular-html-mode "angular")
   ((js-mode js2-mode js3-mode)
    (if (and (boundp 'flow-minor-mode)
             (not (null (symbol-value 'flow-minor-mode))))
        "flow"
      "babel"))
   ((js2-jsx-mode jsx-mode rjsx-mode react-mode) "babel")
   ((typescript-mode typescript-tsx-mode) "typescript")
   (json-mode "json")
   (vue-mode "vue")
   (css-mode "css")
   (graphql-mode "graphql")
   ((gfm-mode markdown-mode) "markdown")
   (php-mode "php")
   (solidity-mode "solidity-parse")
   (web-mode
    (let ((ct (symbol-value 'web-mode-content-type))
          (en (symbol-value 'web-mode-engine)))
      (cond ((equal ct "css") "css")
            ((or (equal ct "javascript") (equal ct "jsx"))
             (if (format-all--buffer-extension-p "ts" "tsx")
                 "typescript"
               "babel"))
            ((equal ct "json") "json")
            ((equal ct "html")
             (cond ((equal en "angular") "angular")
                   ((equal en "vue") "vue")
                   ;; TODO: Use html-tidy instead of prettier for
                   ;; plain HTML. Enable prettier's HTML support once
                   ;; we have multi-formatter support.
                   ;;
                   ;; ((equal en "none") "html")
                   (t nil)))
            (t nil))))
   (yaml-mode "yaml"))
  (:format
   ;; `standard --stdin` properly uses zero vs non-zero exit codes to
   ;; indicate success vs error. However, it checks for quite a broad
   ;; range of errors, all the way up to undeclared identifiers and
   ;; such. To catch only syntax errors, we need to look specifically
   ;; for the text "Parsing error:".
   (format-all--buffer-hard
    '(0 1) ".*?:.*?:[0-9]+:[0-9]+: Parsing error:" nil
    executable "--fix" "--stdin")))

But to no results. I have also tried defining the formatter as advised on Doom Emacs' documentation but with same result:

(set-formatter! 'standard "standard --fix" :modes '(js-jsx-mode rjsx-mode js2-mode js3-mode web-react-mode js-mode))

And yes, I reload the config.el and Emacs on each change but it simply doesn't work.


Solution

  • I fixed it by adding the hooks on config.el and defining the formatter on format-all.el as explained above.

    ;; Use standard-js
    (setq-hook! 'js2-jsx-mode-hook +format-with 'standard)
    (setq-hook! 'js-jsx-mode-hook +format-with 'standard)
    (setq-hook! 'rjsx-mode-hook +format-with 'standard)
    (setq-hook! 'js-mode-hook +format-with 'standard)