Make package-lint happy (#1629)

master
Roman Coedo 2017-05-21 17:11:11 +02:00 committed by Christopher Chedeau
parent c32d567512
commit 83fe8d3d18
1 changed files with 34 additions and 31 deletions

View File

@ -1,5 +1,7 @@
;;; prettier-js.el --- utility functions to format reason code ;;; prettier-js.el --- utility functions to format reason code
;; Version: 0.1.0
;; Copyright (c) 2014 The go-mode Authors. All rights reserved. ;; Copyright (c) 2014 The go-mode Authors. All rights reserved.
;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved. ;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
@ -41,23 +43,23 @@
;;; Code: ;;; Code:
(defcustom prettier-command "prettier" (defcustom prettier-js--prettier-command "prettier"
"The 'prettier' command." "The 'prettier' command."
:type 'string :type 'string
:group 'prettier) :group 'prettier)
(defcustom prettier-args '() (defcustom prettier-js--prettier-args '()
"List of args to send to prettier command" "List of args to send to prettier command."
:type 'list :type 'list
:group 'prettier) :group 'prettier)
(defcustom prettier-target-mode (defcustom prettier-js--prettier-target-mode
"js-mode" "js-mode"
"Name of the major mode to be used by 'prettier-before-save'." "Name of the major mode to be used by 'prettier-before-save'."
:type 'string :type 'string
:group 'prettier) :group 'prettier)
(defcustom prettier-show-errors 'buffer (defcustom prettier-js--prettier-show-errors 'buffer
"Where to display prettier error output. "Where to display prettier error output.
It can either be displayed in its own buffer, in the echo area, or not at all. It can either be displayed in its own buffer, in the echo area, or not at all.
Please note that Emacs outputs to the echo area when writing Please note that Emacs outputs to the echo area when writing
@ -69,7 +71,7 @@ a `before-save-hook'."
(const :tag "None" nil)) (const :tag "None" nil))
:group 'prettier) :group 'prettier)
(defcustom prettier-width-mode nil (defcustom prettier-js--prettier-width-mode nil
"Specify width when formatting buffer contents." "Specify width when formatting buffer contents."
:type '(choice :type '(choice
(const :tag "Window width" window) (const :tag "Window width" window)
@ -79,16 +81,16 @@ a `before-save-hook'."
;;;###autoload ;;;###autoload
(defun prettier-before-save () (defun prettier-before-save ()
"Add this to .emacs to run prettier on the current buffer when saving: "Add this to .emacs to run prettier on the current buffer when saving: (add-hook 'before-save-hook 'prettier-before-save)."
(add-hook 'before-save-hook 'prettier-before-save)."
(interactive) (interactive)
(when (string-equal (symbol-name major-mode) prettier-target-mode) (prettier))) (when (string-equal (symbol-name major-mode) prettier-js--prettier-target-mode) (prettier)))
(defun prettier--goto-line (line) (defun prettier-js--goto-line (line)
"Move cursor to line LINE."
(goto-char (point-min)) (goto-char (point-min))
(forward-line (1- line))) (forward-line (1- line)))
(defun prettier--delete-whole-line (&optional arg) (defun prettier-js--delete-whole-line (&optional arg)
"Delete the current line without putting it in the `kill-ring'. "Delete the current line without putting it in the `kill-ring'.
Derived from function `kill-whole-line'. ARG is defined as for that Derived from function `kill-whole-line'. ARG is defined as for that
function." function."
@ -114,7 +116,7 @@ function."
(delete-region (progn (forward-visible-line 0) (point)) (delete-region (progn (forward-visible-line 0) (point))
(progn (forward-visible-line arg) (point)))))) (progn (forward-visible-line arg) (point))))))
(defun prettier--apply-rcs-patch (patch-buffer) (defun prettier-js--apply-rcs-patch (patch-buffer)
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer." "Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer."
(let ((target-buffer (current-buffer)) (let ((target-buffer (current-buffer))
;; Relative offset between buffer line numbers and line numbers ;; Relative offset between buffer line numbers and line numbers
@ -133,7 +135,7 @@ function."
(goto-char (point-min)) (goto-char (point-min))
(while (not (eobp)) (while (not (eobp))
(unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)") (unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)")
(error "invalid rcs patch or internal error in prettier--apply-rcs-patch")) (error "Invalid rcs patch or internal error in prettier-js--apply-rcs-patch"))
(forward-line) (forward-line)
(let ((action (match-string 1)) (let ((action (match-string 1))
(from (string-to-number (match-string 2))) (from (string-to-number (match-string 2)))
@ -150,18 +152,19 @@ function."
(insert text))))) (insert text)))))
((equal action "d") ((equal action "d")
(with-current-buffer target-buffer (with-current-buffer target-buffer
(prettier--goto-line (- from line-offset)) (prettier-js--goto-line (- from line-offset))
(setq line-offset (+ line-offset len)) (setq line-offset (+ line-offset len))
(prettier--delete-whole-line len))) (prettier-js--delete-whole-line len)))
(t (t
(error "invalid rcs patch or internal error in prettier--apply-rcs-patch"))))))))) (error "Invalid rcs patch or internal error in prettier-js--apply-rcs-patch")))))))))
(defun prettier--process-errors (filename tmpfile errorfile errbuf) (defun prettier-js--process-errors (filename tmpfile errorfile errbuf)
"Process errors for FILENAME, using a TMPFILE an ERRORFILE and display the output in ERRBUF."
(with-current-buffer errbuf (with-current-buffer errbuf
(if (eq prettier-show-errors 'echo) (if (eq prettier-js--prettier-show-errors 'echo)
(progn (progn
(message "%s" (buffer-string)) (message "%s" (buffer-string))
(prettier--kill-error-buffer errbuf)) (prettier-js--kill-error-buffer errbuf))
(insert-file-contents errorfile nil nil nil) (insert-file-contents errorfile nil nil nil)
;; Convert the prettier stderr to something understood by the compilation mode. ;; Convert the prettier stderr to something understood by the compilation mode.
(goto-char (point-min)) (goto-char (point-min))
@ -171,7 +174,8 @@ function."
(compilation-mode) (compilation-mode)
(display-buffer errbuf)))) (display-buffer errbuf))))
(defun prettier--kill-error-buffer (errbuf) (defun prettier-js--kill-error-buffer (errbuf)
"Kill buffer ERRBUF."
(let ((win (get-buffer-window errbuf))) (let ((win (get-buffer-window errbuf)))
(if win (if win
(quit-window t win) (quit-window t win)
@ -186,15 +190,15 @@ function."
(bufferfile (make-temp-file "prettier" nil ext)) (bufferfile (make-temp-file "prettier" nil ext))
(outputfile (make-temp-file "prettier" nil ext)) (outputfile (make-temp-file "prettier" nil ext))
(errorfile (make-temp-file "prettier" nil ext)) (errorfile (make-temp-file "prettier" nil ext))
(errbuf (if prettier-show-errors (get-buffer-create "*prettier errors*"))) (errbuf (if prettier-js--prettier-show-errors (get-buffer-create "*prettier errors*")))
(patchbuf (get-buffer-create "*prettier patch*")) (patchbuf (get-buffer-create "*prettier patch*"))
(coding-system-for-read 'utf-8) (coding-system-for-read 'utf-8)
(coding-system-for-write 'utf-8) (coding-system-for-write 'utf-8)
(width-args (width-args
(cond (cond
((equal prettier-width-mode 'window) ((equal prettier-js--prettier-width-mode 'window)
(list "--print-width" (number-to-string (window-body-width)))) (list "--print-width" (number-to-string (window-body-width))))
((equal prettier-width-mode 'fill) ((equal prettier-js--prettier-width-mode 'fill)
(list "--print-width" (number-to-string fill-column))) (list "--print-width" (number-to-string fill-column)))
(t (t
'())))) '()))))
@ -209,17 +213,17 @@ function."
(with-current-buffer patchbuf (with-current-buffer patchbuf
(erase-buffer)) (erase-buffer))
(if (zerop (apply 'call-process (if (zerop (apply 'call-process
prettier-command nil (list (list :file outputfile) errorfile) prettier-js--prettier-command nil (list (list :file outputfile) errorfile)
nil (append (append prettier-args width-args) (list bufferfile)))) nil (append (append prettier-js--prettier-args width-args) (list bufferfile))))
(progn (progn
(call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-"
outputfile) outputfile)
(prettier--apply-rcs-patch patchbuf) (prettier-js--apply-rcs-patch patchbuf)
(message "Applied prettier with args `%s'" prettier-args) (message "Applied prettier with args `%s'" prettier-js--prettier-args)
(if errbuf (prettier--kill-error-buffer errbuf))) (if errbuf (prettier-js--kill-error-buffer errbuf)))
(message "Could not apply prettier") (message "Could not apply prettier")
(if errbuf (if errbuf
(prettier--process-errors (buffer-file-name) bufferfile errorfile errbuf)) (prettier-js--process-errors (buffer-file-name) bufferfile errorfile errbuf))
))) )))
(kill-buffer patchbuf) (kill-buffer patchbuf)
(delete-file errorfile) (delete-file errorfile)
@ -236,5 +240,4 @@ function."
(remove-hook 'before-save-hook 'prettier 'local))) (remove-hook 'before-save-hook 'prettier 'local)))
(provide 'prettier-js) (provide 'prettier-js)
;;; prettier-js.el ends here
;; prettier-js.el ends here