├── LICENSE ├── README.md └── prettier-eslint.el /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Arseny Zarechnev 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 | # prettier-eslint-emacs 2 | Auto-format current buffer with [prettier-eslint](https://github.com/kentcdodds/prettier-eslint-cli) 3 | 4 | ## Usage 5 | 6 | `M-x prettier-eslint` 7 | 8 | To automatically format after saving: 9 | 10 | ```eslip 11 | (add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t))) 12 | (add-hook 'react-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t))) 13 | ``` 14 | 15 | In Spacemacs add the above to your `dotspacemacs/user-config` 16 | 17 | Otherwise: 18 | 19 | ```eslip 20 | (eval-after-load 'js2-mode 21 | '(add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t)))) 22 | ``` 23 | 24 | ## LICENSE 25 | MIT 26 | -------------------------------------------------------------------------------- /prettier-eslint.el: -------------------------------------------------------------------------------- 1 | ;;; prettier-eslint.el --- Autoformat javascript files with prettier-eslint 2 | 3 | ;; Copyright (C) 2017 Arseny Zarechnev 4 | ;; This package uses the MIT License. 5 | ;; See the LICENSE file. 6 | 7 | ;; Author: Arseny Zarechnev (twitter.com/evindor) 8 | ;; Version: 1.0 9 | ;; Package-Requires: () 10 | ;; Keywords: javascript, prettier, prettier-eslint, eslint, lint, formatting, style 11 | ;; URL: https://github.com/evindor/prettier-eslint-emacs 12 | 13 | ;;; Commentary: 14 | ;; 15 | ;; This file provides `prettier-eslint', which fixes the current file using prettier-eslint 16 | ;; https://github.com/kentcdodds/prettier-eslint-cli 17 | ;; 18 | ;; Usage: 19 | ;; M-x prettier-eslint 20 | ;; 21 | ;; To automatically format after saving: 22 | ;; 23 | ;; (add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t))) 24 | ;; (add-hook 'react-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t))) 25 | ;; 26 | ;; In Spacemacs add the above to your dotspacemacs/user-config 27 | ;; 28 | ;; Otherwise: 29 | ;; 30 | ;; (eval-after-load 'js2-mode 31 | ;; '(add-hook 'js2-mode-hook (lambda () (add-hook 'after-save-hook 'prettier-eslint nil t)))) 32 | ;; 33 | 34 | ;;; Code: 35 | ;;;###autoload 36 | 37 | (defvar prettier-eslint/single-quote t 38 | "Not nil: 39 | Use --single-quote option for prettier") 40 | 41 | (defvar prettier-eslint/trailing-comma nil 42 | "Not nil: 43 | Use --trailing-comma option for prettier") 44 | 45 | (defvar prettier-eslint/no-bracket-spacing nil 46 | "Not nil: 47 | Use --bracket-spacing=false option for prettier") 48 | 49 | (defun prettier-eslint/binary () 50 | (or 51 | ;; Try to find bin in node_modules (via 'npm install prettier-eslint-cli') 52 | (let ((root (locate-dominating-file buffer-file-name "node_modules"))) 53 | (if root 54 | (let ((prettier-binary (concat root "node_modules/.bin/prettier-eslint"))) 55 | (if (file-executable-p prettier-binary) prettier-binary)))) 56 | ;; Fall back to a globally installed binary 57 | (executable-find "prettier-eslint") 58 | ;; give up 59 | (error "Couldn't find a prettier-eslint executable"))) 60 | 61 | ;; TODO account for options 62 | (defun prettier-eslint/options () 63 | (let ((options '())) 64 | (when prettier-eslint/single-quote 65 | (push "--single-quote" options)) 66 | (when prettier-eslint/trailing-comma 67 | (push "--trailing-comma" options)) 68 | (when prettier-eslint/no-bracket-spacing 69 | (push "--bracket-spacing=false" options)) 70 | options 71 | )) 72 | 73 | (defun prettier-eslint () 74 | "Format the current file with ESLint." 75 | (interactive) 76 | (let ((options (prettier-eslint/options))) 77 | (progn (call-process 78 | (prettier-eslint/binary) 79 | nil "*Prettier-ESLint Errors*" nil 80 | buffer-file-name "--write" "--single-quote" 81 | "--trailing-comma" "--bracket-spacing=false") 82 | (revert-buffer t t t)))) 83 | 84 | 85 | (provide 'prettier-eslint) 86 | 87 | ;;; prettier-eslint.el ends here 88 | --------------------------------------------------------------------------------