(require 'json)
(require 'bytecomp)
(require 'checkdoc)
(require 'macroexp)
(defun elsmith--point (position)
(if (number-or-marker-p position) (max 0 (1- position)) 0))
(defun elsmith--finding (source start end message level)
`((source . ,source) (start . ,start) (end . ,(or end :null))
(severity . ,(if (memq level '(:error error)) 1 2))
(message . ,message)))
(defun elsmith--byte-compile (file)
(let* (findings
(bytecomp--inhibit-lexical-cookie-warning t)
(byte-compile-dest-file-function #'ignore)
(byte-compile-log-warning-function
(lambda (text &optional position _fill level)
(let ((start (elsmith--point position)))
(push (elsmith--finding "elsmith-bytecomp" start nil text level)
findings)))))
(byte-compile-file file)
(nreverse findings)))
(defun elsmith--checkdoc ()
(let* (findings
(checkdoc-autofix-flag 'never)
(checkdoc-spellcheck-documentation-flag nil)
(checkdoc-create-error-function
(lambda (text start end &optional _unfixable)
(push (elsmith--finding "elsmith-checkdoc"
(elsmith--point start) (elsmith--point end)
text 'warning)
findings)
nil)))
(checkdoc-current-buffer t)
(nreverse findings)))
(defun elsmith--package-lint ()
(if (not (locate-library "package-lint"))
'((status . "missing") (findings . []))
(condition-case err
(progn
(require 'package-lint)
(let (findings)
(dolist (finding (package-lint-buffer))
(pcase-let ((`(,line ,column ,level ,text) finding))
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
(move-to-column column)
(push (elsmith--finding "elsmith-package-lint"
(elsmith--point (point)) nil text level)
findings))))
`((status . "ok") (findings . ,(vconcat (nreverse findings))))))
(error `((status . "failed") (findings . [])
(message . ,(error-message-string err)))))))
(defun elsmith--diagnostics (file)
(let ((byte-compile (elsmith--byte-compile file)))
(with-temp-buffer
(insert-file-contents file)
(setq buffer-file-name file)
(delay-mode-hooks (emacs-lisp-mode))
`((byteCompile . ,(vconcat byte-compile))
(checkdoc . ,(vconcat (elsmith--checkdoc)))
(packageLint . ,(elsmith--package-lint))))))
(defun elsmith--expand (file form)
(load file nil t t)
(let* ((input (read form))
(macro (car-safe input)))
(unless (and (symbolp macro) (macrop macro))
(error "The selected form does not call a macro"))
`((once . ,(prin1-to-string (macroexpand-1 input)))
(all . ,(prin1-to-string (macroexpand-all input))))))
(let* ((enable-local-variables nil)
(enable-local-eval nil)
(coding-system-for-read 'utf-8-unix)
(coding-system-for-write 'utf-8-unix)
(load-prefer-newer t)
(request (with-temp-buffer
(insert-file-contents "request.json")
(json-parse-buffer :object-type 'alist :array-type 'list)))
(file (alist-get 'file request))
(expanded (alist-get 'form request))
(print-circle t)
(result
(condition-case err
(let ((load-path (append (alist-get 'loadPaths request) load-path)))
(if expanded (elsmith--expand file expanded) (elsmith--diagnostics file)))
(error `((error . ,(error-message-string err)))))))
(with-temp-file "result.json"
(insert (json-serialize result))))