From 028fd43d751660c726d545b95c55baca4aa5fcac Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 4 Oct 2017 17:49:49 +0200 Subject: [PATCH] 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 --- modules/lang/javascript/+screeps.el | 3 +- modules/lang/javascript/config.el | 57 ++++++++++++++++------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/modules/lang/javascript/+screeps.el b/modules/lang/javascript/+screeps.el index 9daaf2f3c..179ebb54b 100644 --- a/modules/lang/javascript/+screeps.el +++ b/modules/lang/javascript/+screeps.el @@ -169,7 +169,6 @@ (defun +javascript|init-screeps-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)))) -(add-hook '+javascript-screeps-mode-hook #'+javascript|init-screeps-mode) diff --git a/modules/lang/javascript/config.el b/modules/lang/javascript/config.el index 2c97f5936..077cfc00e 100644 --- a/modules/lang/javascript/config.el +++ b/modules/lang/javascript/config.el @@ -21,14 +21,18 @@ (sp-with-modes '(js2-mode rjsx-mode) (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 () + "Favor local eslint over global installs and configure flycheck for eslint." (when (derived-mode-p 'js-mode) - (when-let ((eslint (expand-file-name "node_modules/eslint/bin/eslint.js" - (doom-project-root))) - (exists-p (file-exists-p eslint)) - (executable-p (file-executable-p eslint))) - (setq-local flycheck-javascript-eslint-executable eslint) + (when-let ((exec-path (list (doom-project-expand "node_modules/.bin"))) + (eslint (executable-find "eslint"))) + (setq-local flycheck-javascript-eslint-executable eslint)) + (when (flycheck-find-checker-executable 'javascript-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-missing-semi-warning nil)))) (add-hook 'flycheck-mode-hook #'+javascript|init-flycheck-eslint) @@ -83,7 +87,8 @@ ("Split string" :exec js2r-split-string :region nil) ("Unwrap" :exec js2r-unwrap :region t) ("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: ")) @@ -111,9 +116,8 @@ (equal (file-name-extension buffer-file-name) "js") (re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)" magic-mode-regexp-match-limit t) - (progn - (goto-char (match-beginning 1)) - (not (sp-point-in-string-or-comment))))) + (progn (goto-char (match-beginning 1)) + (not (sp-point-in-string-or-comment))))) (push (cons #'+javascript-jsx-file-p 'rjsx-mode) magic-mode-alist) @@ -141,16 +145,7 @@ (def-package! eslintd-fix - :commands - (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)) + :commands (eslintd-fix-mode eslintd-fix)) ;; @@ -189,17 +184,27 @@ ;; (def-project-mode! +javascript-screeps-mode - :match "/screeps/.+$" + :match "/screeps\\(-ai\\)?/.+$" :modes (+javascript-npm-mode) - :init (load! +screeps)) - -(def-project-mode! +javascript-eslintd-fix-mode - :modes (+javascript-npm-mode)) + :add-hooks (+javascript|init-screeps-mode) + :on-load (load! +screeps)) (def-project-mode! +javascript-gulp-mode :files "gulpfile.js") (def-project-mode! +javascript-npm-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))