How to integrate flowtype with spacemacs
Asked Answered
A

2

11

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.

Avirulent answered 15/6, 2016 at 19:14 Comment(0)
C
9

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.

Comate answered 22/6, 2016 at 15:10 Comment(0)
E
0

The answer by saltycrane worked fine for me. Thanks! The solution gives a very short error messages as he points out. I have improved the error messages to be more verbose and look more like the output from flow cli output.

A note to new users who wants to use this script is to make sure you edit it to use the correct mode in flycheck-define-checker at the bottom. I use this in js2-mode, and saltycrane uses react-mode. Edit it to use whatever you are using.

(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)))
                        (err2 (cadr (cdr (assoc 'message errp)))))
                    (flycheck-error-new
                     :line (cdr (assoc 'line err))
                     :column (cdr (assoc 'start err))
                     :level 'error
                     :message (concat (cdr (assoc 'descr err)) ". " (cdr (assoc 'descr err2)))
                     :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
  "Static type checking using Flow."
  :command ("flow" "--json" source-original)
  :error-parser flycheck-parse-flow
  :modes js2-mode)
(add-to-list 'flycheck-checkers 'javascript-flow)
Endres answered 1/12, 2016 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.