-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint-flymake.el
121 lines (103 loc) · 5.01 KB
/
eslint-flymake.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
;;; eslint-flymake.el --- An ESLint backend for Flymake. -*- lexical-binding: t -*-
;; Copyright (C) 2019 Javier Olaechea
;; Author: Javier Olaechea <[email protected]>
;; Version: 0.2
;; Package-Requires: ((emacs "29.1") (flymake "1.2.2"))
;; Keywords: javascript, languages, flymake
;; URL: http://github.com/emacs-pe/eslint-flymake
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package provides an ESLint backend for Flymake.
;; To enable it add the following to your init.el
;;
;; (add-hook 'js-mode-hook 'eslint-flymake-setup-backend)
;;
;; Ideas for further development
;; - 'eslint-flymake-explain-diagnostic' to open rule explanation in
;; the users browser.
;; - Parse error w/o regexp.
;;; Code:
(require 'cl-lib)
(require 'flymake)
(defgroup eslint-flymake nil
"Flymake backend for ESLint"
:group 'programming
:prefix "eslint-flymake-")
(defvar-local eslint-flymake-proc nil)
(defcustom eslint-flymake-command '("eslint" "--no-color" "--stdin")
"The `eslint' command along with the arguments it should be called with."
:type '(repeat string)
:group 'eslint-flymake)
;; 1 both and line:col
;; 2 line
;; 3 column
;; 4 severity
;; 5 msg
;; 6 rule
(defvar eslint-flymake-regexp
(rx-to-string
'(seq (group (group (+ digit)) ":" (group (+ digit)))
(+ " ") (group (or "error" "warning"))
(group (1+ any))
blank
(group (1+ any))
eol)))
(defun eslint-flymake (report-fn &rest _args)
(when (process-live-p eslint-flymake-proc)
(kill-process eslint-flymake-proc))
(let ((source-buffer (current-buffer)))
(save-restriction
(widen)
(setq eslint-flymake-proc
(make-process :name "eslint-flymake"
:noquery t
:connection-type 'pipe
:buffer (generate-new-buffer "*eslint-flymake*")
:command eslint-flymake-command
:sentinel (lambda (proc _event)
(when (memq (process-status proc) '(signal exit))
(unwind-protect
(if (with-current-buffer source-buffer (eq proc eslint-flymake-proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp eslint-flymake-regexp nil t)
for (beg . end) = (flymake-diag-region source-buffer
(string-to-number (match-string 2))
(string-to-number (match-string 3)))
for type = (pcase (match-string 4)
("warning" :warning)
("error" :error)
(_ :note))
for msg = (match-string 5)
collect (flymake-make-diagnostic source-buffer
beg end
type msg)
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc)))))))
(process-send-region eslint-flymake-proc (point-min) (point-max))
(process-send-eof eslint-flymake-proc))))
;;;###autoload
(defun eslint-flymake-setup ()
(add-hook 'flymake-diagnostic-functions 'eslint-flymake nil t))
(define-obsolete-function-alias 'eslint-flymake-setup-backend
#'eslint-flymake-setup "2024-10-10")
(provide 'eslint-flymake)
;;; eslint-flymake.el ends here