Flycheck で使う実行ファイルに node_modules/.bin のものを自動で使うようにするヤツ

More than 1 year has passed since last update.

今開いているファイルの親ディレクトリをどんどん辿っていって node_modules ディレクトリを探して、その中の node_modules/.bin/eslintnode_modules/.bin/jshint が存在するなら、それを flycheck の時に使うようにする。

(defun kui/traverse-parents-for (filename &optional dirname)
  "Find FILENAME from parent directories of the current buffer file or DIRNAME"
  (if dirname
      (let ((path (concat (file-name-as-directory dirname) filename)))
        (if (file-exists-p path)
          (if (string= "/" dirname)
            (kui/traverse-parents-for filename
                                       (directory-file-name dirname))))))
    (kui/traverse-parents-for filename
                              (file-name-directory buffer-file-name))))

(defun kui/find-node-modules-bin (binname)
  "Find executable file named BINNAME from the node_modules directory"
  (let* ((moddir (kui/traverse-parents-for "node_modules"))
         (bin (if moddir (format "%s/.bin/%s" moddir binname))))
    (if (file-executable-p bin) bin)))

(defun kui/flycheck-set-node-modules-bin (checker binname)
  (let ((bin (kui/find-node-modules-bin binname)))
    (when bin
      (message "auto-detect %s: %s" binname bin)
      (flycheck-set-checker-executable checker bin))))

(defun kui/flycheck-set-checker-executable-from-node-modules ()
  (kui/flycheck-set-node-modules-bin 'javascript-jshint "jshint")
  (kui/flycheck-set-node-modules-bin 'javascript-eslint "eslint"))

(add-hook 'js-mode-hook
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.