lang/javascript: major refactor

+ Adds ./node_modules/.bin to exec-path
+ Uses eslint_d globally, if available
+ Always favor local installs of eslint over global eslint or eslint_d

Relevant to #214 #185
This commit is contained in:
Henrik Lissner 2017-10-04 17:49:49 +02:00
parent 3fe9ea2b2e
commit 028fd43d75
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 32 additions and 28 deletions

View file

@ -169,7 +169,6 @@
(defun +javascript|init-screeps-mode () (defun +javascript|init-screeps-mode ()
(when (eq major-mode 'js2-mode) (when (eq major-mode 'js2-mode)
(cl-pushnew 'javascript-jshint flycheck-disabled-checkers) (push 'javascript-jshint flycheck-disabled-checkers)
(setq js2-additional-externs (append '("_") screeps-objects screeps-constants)))) (setq js2-additional-externs (append '("_") screeps-objects screeps-constants))))
(add-hook '+javascript-screeps-mode-hook #'+javascript|init-screeps-mode)

View file

@ -21,14 +21,18 @@
(sp-with-modes '(js2-mode rjsx-mode) (sp-with-modes '(js2-mode rjsx-mode)
(sp-local-pair "/* " " */" :post-handlers '(("| " "SPC")))) (sp-local-pair "/* " " */" :post-handlers '(("| " "SPC"))))
;; Favor local eslint over global, if available ;; If it's available globally, use eslint_d
(setq flycheck-javascript-eslint-executable (executable-find "eslint_d"))
(defun +javascript|init-flycheck-eslint () (defun +javascript|init-flycheck-eslint ()
"Favor local eslint over global installs and configure flycheck for eslint."
(when (derived-mode-p 'js-mode) (when (derived-mode-p 'js-mode)
(when-let ((eslint (expand-file-name "node_modules/eslint/bin/eslint.js" (when-let ((exec-path (list (doom-project-expand "node_modules/.bin")))
(doom-project-root))) (eslint (executable-find "eslint")))
(exists-p (file-exists-p eslint)) (setq-local flycheck-javascript-eslint-executable eslint))
(executable-p (file-executable-p eslint))) (when (flycheck-find-checker-executable 'javascript-eslint)
(setq-local flycheck-javascript-eslint-executable eslint) ;; Flycheck has it's own trailing command and semicolon warning that was
;; conflicting with the eslint settings.
(setq-local js2-strict-trailing-comma-warning nil) (setq-local js2-strict-trailing-comma-warning nil)
(setq-local js2-strict-missing-semi-warning nil)))) (setq-local js2-strict-missing-semi-warning nil))))
(add-hook 'flycheck-mode-hook #'+javascript|init-flycheck-eslint) (add-hook 'flycheck-mode-hook #'+javascript|init-flycheck-eslint)
@ -83,7 +87,8 @@
("Split string" :exec js2r-split-string :region nil) ("Split string" :exec js2r-split-string :region nil)
("Unwrap" :exec js2r-unwrap :region t) ("Unwrap" :exec js2r-unwrap :region t)
("Log this" :exec js2r-log-this) ("Log this" :exec js2r-log-this)
("Debug this" :exec js2r-debug-this)) ("Debug this" :exec js2r-debug-this)
("Reformat buffer (eslint_d)" :exec eslintd-fix :region nil :when (fboundp 'eslintd-fix)))
:prompt "Refactor: ")) :prompt "Refactor: "))
@ -111,8 +116,7 @@
(equal (file-name-extension buffer-file-name) "js") (equal (file-name-extension buffer-file-name) "js")
(re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)" (re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)"
magic-mode-regexp-match-limit t) magic-mode-regexp-match-limit t)
(progn (progn (goto-char (match-beginning 1))
(goto-char (match-beginning 1))
(not (sp-point-in-string-or-comment))))) (not (sp-point-in-string-or-comment)))))
(push (cons #'+javascript-jsx-file-p 'rjsx-mode) magic-mode-alist) (push (cons #'+javascript-jsx-file-p 'rjsx-mode) magic-mode-alist)
@ -141,16 +145,7 @@
(def-package! eslintd-fix (def-package! eslintd-fix
:commands :commands (eslintd-fix-mode eslintd-fix))
(eslintd-fix-mode eslintd-fix)
:init
(defun +javascript|init-eslintd-fix ()
(when (bound-and-true-p +javascript-eslintd-fix-mode)
(eslintd-fix-mode)
;; update flycheck to use eslintd for more consistent results
(when-let (eslintd-executable (executable-find "eslint_d"))
(setq flycheck-javascript-eslint-executable eslintd-executable))))
(add-hook! (js2-mode rjsx-mode) #'+javascript|init-eslintd-fix))
;; ;;
@ -189,17 +184,27 @@
;; ;;
(def-project-mode! +javascript-screeps-mode (def-project-mode! +javascript-screeps-mode
:match "/screeps/.+$" :match "/screeps\\(-ai\\)?/.+$"
:modes (+javascript-npm-mode) :modes (+javascript-npm-mode)
:init (load! +screeps)) :add-hooks (+javascript|init-screeps-mode)
:on-load (load! +screeps))
(def-project-mode! +javascript-eslintd-fix-mode
:modes (+javascript-npm-mode))
(def-project-mode! +javascript-gulp-mode (def-project-mode! +javascript-gulp-mode
:files "gulpfile.js") :files "gulpfile.js")
(def-project-mode! +javascript-npm-mode (def-project-mode! +javascript-npm-mode
:modes (html-mode css-mode web-mode js2-mode markdown-mode) :modes (html-mode css-mode web-mode js2-mode markdown-mode)
:files "package.json") :files "package.json"
:on-enter
(push (doom-project-expand "node_modules/.bin")
(if (make-local-variable 'exec-path)
exec-path)))
;;
;; Tools
;;
(def-project-mode! +javascript-eslintd-fix-mode
:add-hooks (eslintd-fix-mode))