├── .gitignore ├── LICENSE ├── README.md └── eslint-fix.el /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled 2 | *.elc 3 | 4 | # Packaging 5 | .cask 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Neri Marschik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-fix 2 | Fix current file using `ESLint --fix` 3 | 4 | ## Usage 5 | `M-x eslint-fix` 6 | 7 | To automatically format after saving: 8 | 9 | (Choose depending on your favorite mode.) 10 | 11 | ``` 12 | (add-hook 'js-mode-hook #'eslint-fix-auto-mode) 13 | 14 | (add-hook 'js2-mode-hook #'eslint-fix-auto-mode) 15 | ``` 16 | -------------------------------------------------------------------------------- /eslint-fix.el: -------------------------------------------------------------------------------- 1 | ;;; eslint-fix.el --- Fix JavaScript files using ESLint 2 | 3 | ;; Copyright (C) 2016 Neri Marschik 4 | ;; This package uses the MIT License. 5 | ;; See the LICENSE file. 6 | 7 | ;; Author: Neri Marschik 8 | ;; Version: 1.0 9 | ;; Package-Requires: () 10 | ;; Keywords: tools, javascript, eslint, lint, formatting, style 11 | ;; URL: https://github.com/codesuki/eslint-fix 12 | 13 | ;;; Commentary: 14 | ;; 15 | ;; This file provides `eslint-fix', which fixes the current file using ESLint. 16 | ;; 17 | ;; Usage: 18 | ;; M-x eslint-fix 19 | ;; 20 | ;; To automatically format after saving: 21 | ;; (Choose depending on your favorite mode.) 22 | ;; 23 | ;; (eval-after-load 'js-mode 24 | ;; '(add-hook 'js-mode-hook (lambda () (add-hook 'after-save-hook 'eslint-fix nil t)))) 25 | ;; 26 | ;; (eval-after-load 'js2-mode 27 | ;; '(add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'eslint-fix nil t)))) 28 | 29 | ;;; Code: 30 | 31 | (defgroup eslint-fix nil 32 | "Fix JavaScript linting issues with ‘eslint-fix’." 33 | :link '(function-link eslint-fix) 34 | :tag "ESLint Fix" 35 | :group 'tools) 36 | 37 | (defcustom eslint-fix-executable "eslint" 38 | "The ESLint executable to use." 39 | :tag "ESLint Executable" 40 | :type 'string) 41 | 42 | (defcustom eslint-fix-options nil 43 | "Additional options to pass to ESLint (e.g. “--quiet”)." 44 | :tag "ESLint Options" 45 | :type '(repeat string)) 46 | 47 | ;;;###autoload 48 | (defun eslint-fix () 49 | "Format the current file with ESLint." 50 | (interactive) 51 | (unless buffer-file-name 52 | (error "ESLint requires a file-visiting buffer")) 53 | (when (buffer-modified-p) 54 | (if (y-or-n-p (format "Save file %s? " buffer-file-name)) 55 | (save-buffer) 56 | (error "ESLint may only be run on an unmodified buffer"))) 57 | 58 | (let ((eslint (executable-find eslint-fix-executable)) 59 | (options (append eslint-fix-options 60 | (list "--fix" buffer-file-name)))) 61 | (unless eslint 62 | (error "Executable ‘%s’ not found" eslint-fix-executable)) 63 | (apply #'call-process eslint nil "*ESLint Errors*" nil options) 64 | (revert-buffer t t t))) 65 | 66 | ;;;###autoload 67 | (define-minor-mode eslint-fix-auto-mode 68 | "Run `eslint-fix' after save." 69 | :group 'eslint-fix 70 | (if eslint-fix-auto-mode 71 | (add-hook 'after-save-hook #'eslint-fix nil t) 72 | (remove-hook 'after-save-hook #'eslint-fix t))) 73 | 74 | (provide 'eslint-fix) 75 | 76 | ;;; eslint-fix.el ends here 77 | --------------------------------------------------------------------------------