Skip to content

Commit 88a9792

Browse files
committed
plugin/emacs: unify all plugin settings into a single library
Currently there is at least two customizeble variables per plugin, using more than one plugin at time is annoying to configure, this change generalize and centralize configuration in a single place. This commit also adds configuration for searching quick-lint-js executable, along with helper to find it under npm folder structure. Signed-off-by: wagner riffel <[email protected]>
1 parent c707b8b commit 88a9792

File tree

7 files changed

+133
-80
lines changed

7 files changed

+133
-80
lines changed

plugin/emacs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.10)
55
include(GNUInstallDirs)
66

77
install(
8+
FILES quicklintjs.el
89
FILES flycheck-quicklintjs.el
910
FILES lsp-quicklintjs.el
1011
FILES eglot-quicklintjs.el

plugin/emacs/eglot-quicklintjs.el

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,10 @@
1919
;;; Code:
2020

2121
(require 'eglot)
22+
(require 'quicklintjs)
2223

23-
(defgroup eglot-quicklintjs nil
24-
"quick-lint-js Eglot integration."
25-
:group 'eglot-quicklintjs
26-
:link '(url-link :tag "Website" "https://quick-lint-js.com"))
27-
28-
(defcustom eglot-quicklintjs-program "quick-lint-js"
29-
"Path to quick-lint-js program to run."
30-
:group 'eglot-quicklintjs
31-
:type 'stringp)
32-
33-
(defcustom eglot-quicklintjs-args nil
34-
"Arguments to quick-lint-js."
35-
:group 'eglot-quicklintjs
36-
:type '(repeat string))
37-
38-
(add-to-list 'eglot-server-programs `(js-mode . (,eglot-quicklintjs-program
39-
"--lsp-server"
40-
,@eglot-quicklintjs-args)))
41-
24+
(add-to-list 'eglot-server-programs `(js-mode . ,(quicklintjs-find-program
25+
"--lsp-server")))
4226
(provide 'eglot-quicklintjs)
4327

4428
;; quick-lint-js finds bugs in JavaScript programs.

plugin/emacs/flycheck-quicklintjs.el

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@
3535
;;; Code:
3636

3737
(require 'flycheck)
38-
39-
(defgroup flycheck-quicklintjs nil
40-
"quick-lint-js Flycheck integration."
41-
:prefix "flycheck-"
42-
:group 'flycheck
43-
:group 'quicklintjs
44-
:link '(url-link :tag "Website" "https://quick-lint-js.com"))
45-
46-
(flycheck-def-args-var flycheck-quicklintjs-args javascript-quicklintjs)
47-
(flycheck-def-executable-var javascript-quicklintjs "quick-lint-js")
38+
(require 'quicklintjs)
4839

4940
(defun flycheck-quicklintjs-parse-errors (output checker buffer)
5041
"Parse quick-lint-js alist output format from OUTPUT"
@@ -62,25 +53,22 @@
6253
:checker checker
6354
:end-pos (cdr region)))) (car (read-from-string output))))
6455

65-
(flycheck-define-checker javascript-quicklintjs
56+
(flycheck-define-command-checker 'javascript-quicklintjs
6657
"quick-lint-js finds bugs in JavaScript programs.
6758
6859
https://quick-lint-js.com"
69-
:command ("quick-lint-js"
70-
"--output-format=emacs-lisp"
71-
(eval (let ((file (buffer-file-name)))
72-
(if file
73-
`("--path-for-config-search" ,file)
74-
())))
75-
"--stdin"
76-
(eval flycheck-quicklintjs-args))
77-
:standard-input t
78-
:error-parser flycheck-quicklintjs-parse-errors
60+
:command (quicklintjs-find-program
61+
"--output-format=emacs-lisp"
62+
(let ((file (buffer-file-name)))
63+
(when file (concat "--path-for-config-search=" file)))
64+
"--stdin")
65+
:standard-input 't
66+
:error-parser 'flycheck-quicklintjs-parse-errors
7967
:error-explainer (lambda (err)
8068
(let ((error-code (flycheck-error-id err))
8169
(url "https://quick-lint-js.com/errors/#%s"))
8270
(and error-code `(url . ,(format url error-code)))))
83-
:modes js-mode)
71+
:modes 'js-mode)
8472

8573
(add-to-list 'flycheck-checkers 'javascript-quicklintjs t)
8674

plugin/emacs/flymake-quicklintjs.el

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,7 @@
2424
;;; Code:
2525

2626
(require 'flymake)
27-
28-
(defgroup flymake-quicklintjs nil
29-
"Flymake backend for quick-lint-js"
30-
:link '(url-link :tag "Website" "https://quick-lint-js.com"))
31-
32-
(defcustom flymake-quicklintjs-program "quick-lint-js"
33-
"Path to quick-lint-js program to run."
34-
:group 'flymake-quicklintjs
35-
:type 'stringp)
36-
37-
(defcustom flymake-quicklintjs-args nil
38-
"Arguments to quick-lint-js."
39-
:group 'flymake-quicklintjs
40-
:type '(repeat 'string))
27+
(require 'quicklintjs)
4128

4229
(defvar-local flymake-quicklintjs--proc nil
4330
"Internal variable for `flymake-quicklintjs'")
@@ -68,13 +55,10 @@ REPORT-FN is Flymake's callback."
6855
:connection-type 'pipe
6956
:noquery t
7057
:buffer (get-buffer-create " *flymake-quicklintjs*")
71-
:command `(,flymake-quicklintjs-program
72-
,@(let ((file (buffer-file-name)))
73-
(if file
74-
`("--path-for-config-search" ,file)
75-
()))
76-
"--stdin" "--output-format=emacs-lisp"
77-
,@flymake-quicklintjs-args)
58+
:command (quicklintjs-find-program
59+
(let ((file (buffer-file-name)))
60+
(when file (concat "--path-for-config-search=" file)))
61+
"--stdin" "--output-format=emacs-lisp")
7862
:sentinel
7963
(lambda (p _ev)
8064
(unwind-protect

plugin/emacs/lsp-quicklintjs.el

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,12 @@
1818
;;; Code:
1919

2020
(require 'lsp-mode)
21-
22-
(defgroup lsp-quicklintjs nil
23-
"quick-lint-js LSP Mode integration."
24-
:link '(url-link :tag "Website" "https://quick-lint-js.com"))
25-
26-
(defcustom lsp-quicklintjs-program "quick-lint-js"
27-
"Path to quick-lint-js program to run."
28-
:group 'lsp-quicklintjs
29-
:type 'stringp)
30-
31-
(defcustom lsp-quicklintjs-args nil
32-
"Arguments to quick-lint-js."
33-
:group 'lsp-quicklintjs
34-
:type '(repeat string))
21+
(require 'quicklintjs)
3522

3623
(lsp-register-client
3724
(make-lsp-client
38-
:new-connection (lsp-stdio-connection `(,lsp-quicklintjs-program "--lsp-server"
39-
,@lsp-quicklintjs-args))
25+
:new-connection (lsp-stdio-connection `,(quicklintjs-find-program
26+
"--lsp-server"))
4027
:major-modes '(js-mode)
4128
:server-id 'quick-lint-js))
4229

plugin/emacs/quicklintjs.el

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
;;; quicklintjs.el --- Helper functions for quicklintjs Emacs plugins -*- lexical-binding: t; -*-
2+
;;; Commentary:
3+
4+
;; Shared parts of configuration and code among others Emacs plugins.
5+
6+
;;; Code:
7+
8+
(require 'cl-seq)
9+
10+
(defgroup quicklintjs nil
11+
"quick-lint-js finds bugs in JavaScript programs."
12+
:prefix "quicklintjs-"
13+
:group 'tools
14+
:group 'processes
15+
:link '(url-link :tag "Website" "https://quick-lint-js.com"))
16+
17+
(defcustom quicklintjs-program-name "quick-lint-js"
18+
"quick-lint-js executable to use."
19+
:group 'quicklintjs
20+
:type 'string
21+
:safe 'stringp)
22+
23+
(defcustom quicklintjs-program-args nil
24+
"Arguments to `quicklintjs-program-name'."
25+
:group 'quicklintjs
26+
:type '(repeat 'string))
27+
28+
(defcustom quicklintjs-find-program-function #'quicklintjs-executable-find
29+
"Function to find quick-lint-js."
30+
:group 'quicklintjs
31+
:type '(choice (const :tag "Search quick-lint-js in `exec-path'"
32+
quicklintjs-executable-find)
33+
(const :tag "Search quick-lint-js in node-modules"
34+
quicklintjs-node-modules-executable-find)
35+
(function :tag "Function to get quick-lint-js path")))
36+
37+
;;;###autoload
38+
(defun quicklintjs-executable-find ()
39+
"Search `quicklintjs-program-name' in `exec-path'."
40+
(executable-find quicklintjs-program-name))
41+
42+
;;;###autoload
43+
(defun quicklintjs-find-program (&rest argv)
44+
"Make a list of strings by calling `quicklintjs-find-program-function',
45+
appending `quicklintjs-program-args' and `argv'.
46+
Empty strings and nil are ignored."
47+
(cl-remove-if-not (lambda (a) (and (stringp a)
48+
(> (length a) 0)))
49+
(append (list (funcall
50+
quicklintjs-find-program-function))
51+
quicklintjs-program-args argv)))
52+
53+
;;;###autoload
54+
(defun quicklintjs-node-modules-executable-find ()
55+
"Search `quicklintjs-program-name' in nearest node_modules relative to
56+
`default-directory'."
57+
(let ((node-mod-dir
58+
(locate-dominating-file default-directory
59+
(lambda (f) (directory-files
60+
f nil "^node_modules$" t)))))
61+
(when node-mod-dir
62+
(expand-file-name (concat
63+
(file-name-as-directory node-mod-dir)
64+
(file-name-as-directory "node_modules")
65+
(file-name-as-directory ".bin")
66+
quicklintjs-program-name
67+
(when (memq system-type '(windows-nt ms-dos))
68+
".cmd"))))))
69+
70+
(provide 'quicklintjs)
71+
72+
;; quick-lint-js finds bugs in JavaScript programs.
73+
;; Copyright (C) 2020 Matthew Glazar
74+
;;
75+
;; This file is part of quick-lint-js.
76+
;;
77+
;; quick-lint-js is free software: you can redistribute it and/or modify
78+
;; it under the terms of the GNU General Public License as published by
79+
;; the Free Software Foundation, either version 3 of the License, or
80+
;; (at your option) any later version.
81+
;;
82+
;; quick-lint-js is distributed in the hope that it will be useful,
83+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
84+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85+
;; GNU General Public License for more details.
86+
;;
87+
;; You should have received a copy of the GNU General Public License
88+
;; along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.
89+
90+
;;; quicklintjs.el ends here

plugin/emacs/test/quicklintjs-test.el

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
;;; Commentary:
33

44
;;; Code:
5+
(require 'cl-lib)
56
(require 'ert)
67
(require 'package)
8+
(require 'quicklintjs)
79

810
(defconst cache-dir-name (concat
911
(expand-file-name default-directory)
@@ -28,10 +30,13 @@
2830
(quicklintjs-install-deps (if (>= emacs-major-version 26)
2931
'(flycheck eglot lsp-mode)
3032
'(flycheck)))
33+
34+
(set 'quicklintjs-find-program-function (lambda () "quick-lint-js"))
3135
(def-flycheck-tests)
3236
(def-eglot-tests)
3337
(def-lsp-tests)
3438
(def-flymake-tests)
39+
(def-quicklintjs-tests)
3540
(ert-run-tests-batch-and-exit))
3641

3742
(defun def-flymake-tests ()
@@ -73,7 +78,7 @@ foobar\")((16 . 22) 2 \"E057\" \"use of undeclared variable: foobar\")(\
7378
(with-current-buffer js-buf
7479
(insert "foobar")
7580
(should (equal (call-process-region (point-min) (point-max)
76-
flymake-quicklintjs-program nil
81+
quicklintjs-program-name nil
7782
out-buf nil "--stdin"
7883
"--output-format=emacs-lisp") 0)))))
7984

@@ -84,7 +89,7 @@ foobar\")((16 . 22) 2 \"E057\" \"use of undeclared variable: foobar\")(\
8489
(insert "function")
8590
(should (equal (call-process-region
8691
(point-min) (point-max)
87-
flymake-quicklintjs-program nil
92+
quicklintjs-program-name nil
8893
out-buf nil "--stdin"
8994
"--output-format=emacs-lisp") 0))))))
9095

@@ -136,6 +141,20 @@ foobar\")((16 . 22) 2 \"E057\" \"use of undeclared variable: foobar\")(\
136141
:id "E059" :checker javascript-quicklintjs
137142
:end-line 1 :end-column 2)))))
138143

144+
(defun def-quicklintjs-tests ()
145+
(ert-deftest quicklintjs-find-program-argv ()
146+
(should (cl-every (lambda (a b) (string= a b))
147+
(quicklintjs-find-program "foo" "baz")
148+
'("quick-lint-js" "foo" "baz"))))
149+
(ert-deftest quicklintjs-find-program-removes-nil ()
150+
(should (cl-every (lambda (a b) (string= a b))
151+
(quicklintjs-find-program "foo" nil "baz")
152+
'("quick-lint-js" "foo" "baz"))))
153+
(ert-deftest quicklintjs-find-program-removes-empty ()
154+
(should (cl-every (lambda (a b) (string= a b))
155+
(quicklintjs-find-program "foo" "" "baz")
156+
'("quick-lint-js" "foo" "baz")))))
157+
139158
;; quick-lint-js finds bugs in JavaScript programs.
140159
;; Copyright (C) 2020 Matthew "strager" Glazar
141160
;;

0 commit comments

Comments
 (0)