-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheslint-fix.el
76 lines (64 loc) · 2.23 KB
/
eslint-fix.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
;;; eslint-fix.el --- Fix JavaScript files using ESLint
;; Copyright (C) 2016 Neri Marschik
;; This package uses the MIT License.
;; See the LICENSE file.
;; Author: Neri Marschik <marschik_neri@cyberagent.co.jp>
;; Version: 1.0
;; Package-Requires: ()
;; Keywords: tools, javascript, eslint, lint, formatting, style
;; URL: https://github.com/codesuki/eslint-fix
;;; Commentary:
;;
;; This file provides `eslint-fix', which fixes the current file using ESLint.
;;
;; Usage:
;; M-x eslint-fix
;;
;; To automatically format after saving:
;; (Choose depending on your favorite mode.)
;;
;; (eval-after-load 'js-mode
;; '(add-hook 'js-mode-hook (lambda () (add-hook 'after-save-hook 'eslint-fix nil t))))
;;
;; (eval-after-load 'js2-mode
;; '(add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'eslint-fix nil t))))
;;; Code:
(defgroup eslint-fix nil
"Fix JavaScript linting issues with ‘eslint-fix’."
:link '(function-link eslint-fix)
:tag "ESLint Fix"
:group 'tools)
(defcustom eslint-fix-executable "eslint"
"The ESLint executable to use."
:tag "ESLint Executable"
:type 'string)
(defcustom eslint-fix-options nil
"Additional options to pass to ESLint (e.g. “--quiet”)."
:tag "ESLint Options"
:type '(repeat string))
;;;###autoload
(defun eslint-fix ()
"Format the current file with ESLint."
(interactive)
(unless buffer-file-name
(error "ESLint requires a file-visiting buffer"))
(when (buffer-modified-p)
(if (y-or-n-p (format "Save file %s? " buffer-file-name))
(save-buffer)
(error "ESLint may only be run on an unmodified buffer")))
(let ((eslint (executable-find eslint-fix-executable))
(options (append eslint-fix-options
(list "--fix" buffer-file-name))))
(unless eslint
(error "Executable ‘%s’ not found" eslint-fix-executable))
(apply #'call-process eslint nil "*ESLint Errors*" nil options)
(revert-buffer t t t)))
;;;###autoload
(define-minor-mode eslint-fix-auto-mode
"Run `eslint-fix' after save."
:group 'eslint-fix
(if eslint-fix-auto-mode
(add-hook 'after-save-hook #'eslint-fix nil t)
(remove-hook 'after-save-hook #'eslint-fix t)))
(provide 'eslint-fix)
;;; eslint-fix.el ends here