From 71fab635ea3d8bd9996e4c97681e2c26a5cacc9f Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sun, 16 Oct 2022 09:49:00 -0400 Subject: Eslint mode for Javascript --- init.el | 1 + lisp/json.el | 15 ++++++++++++--- lisp/web.el | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/init.el b/init.el index 56bf349..0b8051a 100644 --- a/init.el +++ b/init.el @@ -51,6 +51,7 @@ (require 'json) (require 'markdown) (require 'lisp) +(require 'json) (custom-set-variables ;; custom-set-variables was added by Custom. diff --git a/lisp/json.el b/lisp/json.el index f65b7b8..d1093cf 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -1,8 +1,17 @@ ;;; Code: -(autoload 'json-mode "json-mode" - "Use the json-mode package to provide 'json-mode on-demand." - t) +(add-to-list 'auto-mode-alist '(".eslintrc'" . json-mode)) + +(require 'flycheck) +(flycheck-add-mode 'json-jslint 'json-mode) + +(defun my-json-mode-hook () + "Turn off eglot in JSON mode." + (require 'eglot) + (flymake-mode 0) + ) + +(add-hook 'json-mode-hook 'my-json-mode-hook) (provide 'json) ;;; json.el ends here diff --git a/lisp/web.el b/lisp/web.el index 655dd03..a1d9598 100644 --- a/lisp/web.el +++ b/lisp/web.el @@ -16,24 +16,22 @@ (add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode)) ;; JavaScript -(require 'js2-mode) -(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode)) -(push '("\\.js[x]?\\'" . js2-mode) auto-mode-alist) -(add-hook 'js2-mode-hook #'js2-imenu-extras-mode) +(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode)) +(push '("\\.js[x]?\\'" . javascript-mode) auto-mode-alist) (defun setup-js2-mode() "Hooks for Javscript 2 Mode." (setq js-indent-level 2) (company-mode t) - (company-quickhelp-mode t) (smartparens-mode 1) ) -(add-hook 'js2-mode-hook 'setup-js2-mode) -(setq js2-highlight-level 3) -(setq js2-idle-timer-delay 0.1) +(add-hook 'js-mode-hook 'setup-js2-mode) +;(setq js2-highlight-level 3) +;(setq js2-idle-timer-delay 0) -;; Tide for refactoring +;; TypeScript (defun setup-typescript() + "Set up tiny things for TypeScript." (setq typescript-indent-level 2) (company-mode t) (company-quickhelp-mode t) @@ -47,8 +45,30 @@ (lambda () (when (string-equal "tsx" (file-name-extension buffer-file-name)) (setup-typescript)))) -;; enable typescript-tslint checker -(flycheck-add-mode 'typescript-tslint 'web-mode) + +;; Eslint through Flycheck. +(require 'flycheck) +(setq-default flycheck-disabled-checkers + (append flycheck-disabled-checkers + '(javascript-jshint))) +(setq-default flycheck-disabled-checkers + (append flycheck-disabled-checkers + '(json-jsonlist))) +(flycheck-add-mode 'javascript-eslint 'js-mode) +(flycheck-add-mode 'typescript-tslint 'typescript-mode) +; use local eslint from node_modules before global +; http://emacs.stackexchange.com/questions/21205/flycheck-with-file-relative-eslint-executable +(defun my-use-eslint-from-node-modules () + "Use eslint from local node_modules instead of globally." + (let* ((root (locate-dominating-file + (or (buffer-file-name) default-directory) + "node_modules")) + (eslint (and root + (expand-file-name "node_modules/eslint/bin/eslint.js" + root)))) + (when (and eslint (file-executable-p eslint)) + (setq-local flycheck-javascript-eslint-executable eslint)))) +(add-hook 'flycheck-mode-hook #'my-use-eslint-from-node-modules) ;; Package management (use-package eglot @@ -57,7 +77,7 @@ :ensure t :defer 3 :hook - ((js2-mode typescript-mode) . eglot-ensure)) + ((js-mode typescript-mode) . eglot-ensure)) :config (cl-pushnew '((js-mode typescript-mode) . ("typescript-language-server" "--stdio")) eglot-server-programs -- cgit v1.2.1