Skip to content

Instantly share code, notes, and snippets.

@ustun
Created January 12, 2016 23:59
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ustun/73321bfcb01a8657e5b8 to your computer and use it in GitHub Desktop.
Save ustun/73321bfcb01a8657e5b8 to your computer and use it in GitHub Desktop.
run eslint --fix on emacs file save
;;; runs eslint --fix on the current file after save
;;; alpha quality -- use at your own risk
(defun eslint-fix-file ()
(interactive)
(message "eslint --fixing the file" (buffer-file-name))
(shell-command (concat "eslint --fix " (buffer-file-name))))
(defun eslint-fix-file-and-revert ()
(interactive)
(eslint-fix-file)
(revert-buffer t t))
(add-hook 'js2-mode-hook
(lambda ()
(add-hook 'after-save-hook #'eslint-fix-file-and-revert)))
@mattdeboard
Copy link

Thanks for the snippet Ustun! Imagine my surprise when googling "emacs eslint fix" took me to this snippet. We met a few years ago @ Strange Loop iirc :)

@rpatterson
Copy link

I ended up integrating this into flycheck so that eslint fixes things continuously, without saving.

@ustun
Copy link
Author

ustun commented Jan 24, 2020

Hi @mattdeboard I must have missed your message. Glad it was useful.

@rpatterson Thanks, looks great to have it interested.

FWIW, I have been using prettier-mode recently and using eslint at the last minute. I also use the tide-mode in js to do some checks.

@Hettomei
Copy link

Hettomei commented Apr 3, 2020

Hi every one,
thanks a lot for the snippet.

If, like me, you call it manually and you get an annoyoing buffer that display shell message,
replace shell-command with call-process-shell-command (grabbed at https://stackoverflow.com/questions/11613974/how-can-the-shell-command-output-buffer-be-kept-in-the-background)

(defun tim-eslint-fix-file ()
  (interactive)
  (message "eslint --fix the file" (buffer-file-name))
  (call-process-shell-command
   (concat "yarn eslint --fix " (buffer-file-name))
   nil "*Shell Command Output*" t)
  (revert-buffer t t))

@ustun
Copy link
Author

ustun commented Apr 3, 2020

@dil-bfleischman
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment