#+TITLE: Language - JavaScript - LSP #+AUTHOR: Juan Placencia * Package Define JavaScript language LSP Integration package. #+BEGIN_SRC emacs-lisp (use-package emacs :after (uno-lang-js uno-dev-lsp projectile) :if (executable-find "node") :config (provide 'uno-lang-js-lsp)) #+END_SRC * LSP Integration #+BEGIN_SRC emacs-lisp (use-package emacs :after uno-lang-js-lsp :custom (lsp-eslint-auto-fix-on-save t) :init (require 'seq) (defun uno/lang/js/lsp () "Set up LSP for JavaScript." (require 'lsp-javascript) (make-local-variable 'lsp-disabled-clients) (when (uno/lang/js/lsp/local-provider) (plist-put lsp-deps-providers :local '(:path uno/lang/js/lsp/local-provider)) (setq-local lsp-auto-guess-root t)) (uno/lang/js/lsp/add-to-local 'typescript-language-server "typescript-language-server/lib/cli.js") (uno/lang/js/lsp/add-to-local 'typescript "typescript/bin/tsserver") (when (uno/lang/js/lsp/local-provider "eslint/bin/eslint.js") (let ((-eslint (uno/lang/js/lsp/local-provider "eslint/bin/eslint.js")) (-node-path (uno/lang/js/lsp/local-provider)) (-package-manager (uno/lang/js/lsp/local-provider-type))) (setq-local flycheck-javascript-eslint-executable -eslint lsp-eslint-node-path -node-path lsp-eslint-package-manager -package-manager)))) (defun uno/lang/js/lsp/add-to-local (name file) "Add NAME dependency from FILE to local instance." (when (uno/lang/js/lsp/local-provider file) (lsp-dependency name `(:local ,file)) t)) (defun uno/lang/js/lsp/local-provider (&optional path) "Provide path for local SDK instance relative to PATH." (seq-reduce (lambda (current sdk) (if current current (let ((-path (concat (projectile-project-root) sdk path))) (if (file-exists-p -path) -path nil)))) '(".yarn/sdks/" "node_modules/") nil)) (defun uno/lang/js/lsp/local-provider-type () "Check SDK instance type." (let ((-root (projectile-project-root))) (cond ((file-exists-p (concat -root ".yarn/")) "yarn") ((file-exists-p (concat -root "node_modules/")) "npm")))) ;; https://github.com/emacs-lsp/lsp-mode/issues/1842#issuecomment-942861229 (defun uno/lang/js/lsp/eslint-autofix () "Run lsp-eslint-apply-all-fixes before lsp--before-save." (when lsp-eslint-auto-fix-on-save (lsp-eslint-fix-all))) (advice-add 'lsp--before-save :before #'uno/lang/js/lsp/eslint-autofix) (uno/add-useless-buffer "\\*eslint\\*") (uno/add-useless-buffer "\\*eslint::.*\\*")) #+END_SRC