Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate flowtype with spacemacs

I'm spacemacs fan. I want to use Facebook Flow but I have not idea how to integrate it with spacemacs. I'm using flow with nuclide but I need to relearn everything to be productive. There is this script on flow repository to use it with emacs. I need a guide for how to use it within spacemacs.

Thanks.

like image 738
AA. Avatar asked Jun 15 '16 19:06

AA.


1 Answers

I used Bodil's flow flycheck config here: https://github.com/bodil/emacs.d/blob/d28264cf072bb8a62459a48813d0cb30804b4f5b/bodil/bodil-js.el#L121-L154

I made it work with spacemacs's react-mode and default eslint flychecker by adding the following to my dotspacemacs/user-config (https://github.com/saltycrane/.spacemacs.d/blob/9d985ace9251529c2b8d7857e2ec9835b103084c/init.el#L383-L414):

;; Flow (JS) flycheck config (http://flowtype.org)
;; from https://github.com/bodil/emacs.d/blob/master/bodil/bodil-js.el
(require 'f)
(require 'json)
(require 'flycheck)

(defun flycheck-parse-flow (output checker buffer)
  (let ((json-array-type 'list))
    (let ((o (json-read-from-string output)))
      (mapcar #'(lambda (errp)
                  (let ((err (cadr (assoc 'message errp))))
                    (flycheck-error-new
                     :line (cdr (assoc 'line err))
                     :column (cdr (assoc 'start err))
                     :level 'error
                     :message (cdr (assoc 'descr err))
                     :filename (f-relative
                                (cdr (assoc 'path err))
                                (f-dirname (file-truename
                                            (buffer-file-name))))
                     :buffer buffer
                     :checker checker)))
              (cdr (assoc 'errors o))))))

(flycheck-define-checker javascript-flow
  "Javascript type checking using Flow."
  :command ("flow" "--json" source-original)
  :error-parser flycheck-parse-flow
  :modes react-mode
  :next-checkers ((error . javascript-eslint))
  )
(add-to-list 'flycheck-checkers 'javascript-flow)

Also be sure the Flow command line tool is installed. Install it like this:

npm install -g flow-bin

I think Bodil intended to make the messages short, but I would like to have flycheck display more verbose messages. If anyone knows how to do that, I'd appreciate it.

EDIT 2016-08-12: the original version I posted gave a Symbol's function definition is void: flycheck-define-checker error on initial load. I updated the code above to add a require 'flycheck to get rid of that error.

like image 90
saltycrane Avatar answered Oct 18 '22 20:10

saltycrane