Skip to content

Instantly share code, notes, and snippets.

@manandearth
Last active August 22, 2019 16:26
Show Gist options
  • Save manandearth/5a5a5052878fa3849ec2e8da7cbcfc0a to your computer and use it in GitHub Desktop.
Save manandearth/5a5a5052878fa3849ec2e8da7cbcfc0a to your computer and use it in GitHub Desktop.
eslint shell-command hook for js modes
(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)
(when (or(eq major-mode 'js2-mode)
(eq major-mode 'rjsx-mode)
(eq major-mode 'js2-jsx-mode))
(eslint-fix-file)
;;the third t argument is for preserving the modes on revert.
(revert-buffer t t t)
;;closing any returned output from shell
(other-window 1)
(if (equal (buffer-name) "*Shell Command Output*")
(progn (kill-buffer) (message "linted with output"))
(message "linted"))
(other-window 1)
))
(add-hook 'js2-mode-hook
(lambda ()
(add-hook 'after-save-hook #'eslint-fix-file-and-revert)))
@manandearth
Copy link
Author

Purpose: Runs shell command eslint on the buffer's file on save and reverts the buffer.

a few issues

  1. the when condition I added because I didn't manage to get this hook to prompt exclusively in js2-mode.
    I rather not have it.
  2. the value is returned from the shell-command to other-buffer. I didn't manage to make it into a buffer that can be killed with a q ( read only doesn't do it ) so I got rid of it all together in a kind of an ugly way.. Is there a better way?

@manandearth
Copy link
Author

I've updated the kill-buffer to depend on buffer-name so it now works in all cases

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