Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Created October 18, 2016 18:55
Show Gist options
  • Save aaronjensen/5ff19f2b7ec86b8df7c8795eb6bc5511 to your computer and use it in GitHub Desktop.
Save aaronjensen/5ff19f2b7ec86b8df7c8795eb6bc5511 to your computer and use it in GitHub Desktop.
;; Runs eslint --fix after save
(defun eslint-fix ()
(interactive)
(let ((current-point (point))
(line (count-screen-lines (window-start) (point)))
(command (concat
flycheck-javascript-eslint-executable
" --stdin"
" --fix-to-stdout"
" --stdin-filename " buffer-file-name))
(buffer (current-buffer))
(text (buffer-substring-no-properties (point-min) (point-max))))
(with-temp-buffer
(insert text)
(when (eq 0
(shell-command-on-region
;; Region
(point-min)
(point-max)
;; Command
command
;; Output to current buffer
t
;; Replace buffer
t
;; Error buffer name
"*eslint-fix error*"))
(let ((fixed-text (buffer-substring-no-properties (point-min) (point-max))))
(with-current-buffer buffer
(delete-region (point-min) (point-max))
(insert fixed-text)
;; Restore point and scroll position
(goto-char current-point)
(recenter (- line 1))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment